D
Deno

help

How to assert object with a field of a given type?

DDrPotat10/1/2023
Hi, I'm writing an integration test that needs to assert an object response, where the ID field can be of type string:
const json = await res.json();
assertObjectMatch(json, {
data: {
object: "VerificationRequest",
id: expect.any(String)
},
});
const json = await res.json();
assertObjectMatch(json, {
data: {
object: "VerificationRequest",
id: expect.any(String)
},
});
The expect.any(String) line doesn't work because that syntax is from Jest: https://jestjs.io/docs/expect#expectanyconstructor Is there any way to do this in Deno? I'm trying to assert that the data object has an id field of type String , while also asserting the remaining fields with assertObjectMatch I couldn't find anything similar to Jest's expect.any(String) in the Deno testing docs
DAHDeno AI Helper10/1/2023
Based on the documentation, Deno's assertObjectMatch function checks if an object matches a subset of the properties of another object. However, it doesn't provide a way to check the type of a property. It only checks if the properties and their values match. Here's an example of how assertObjectMatch is used:
assertObjectMatch(
{ foo: true, bar: false },
{
foo: true,
},
);
assertObjectMatch(
{ foo: true, bar: false },
{
foo: true,
},
);
In this case, it checks if the object { foo: true, bar: false } has a property foo with a value of true. To check the type of a property in an object, you might need to write a separate assertion. For example:
import { assert, assertObjectMatch } from "https://deno.land/std@0.201.0/assert/mod.ts";

const json = await res.json();

// Check if the object structure matches
assertObjectMatch(json, {
data: {
object: "VerificationRequest",
},
});

// Check if 'id' is a string
assert(typeof json.data.id === "string");
import { assert, assertObjectMatch } from "https://deno.land/std@0.201.0/assert/mod.ts";

const json = await res.json();

// Check if the object structure matches
assertObjectMatch(json, {
data: {
object: "VerificationRequest",
},
});

// Check if 'id' is a string
assert(typeof json.data.id === "string");
In this example, assertObjectMatch is used to check the structure of the object, and assert is used to check the type of the id property. Please note that this information is not explicitly stated in the documentation, and this is a general advice based on the available methods in Deno's standard library.

Looking for more? Join the community!