Skip to content

Checking the Response ​

Checks (called assertions in the editor) decide whether a suite passes. After the route answers, every check runs. The suite passes only if all of them hold.

A suite with no checks passes as long as the route runs without crashing.

Simple checks ​

Open the Assertions tab, press Add assertion, and pick a target:

TargetWhat it looks atExtra field
Status codeThe HTTP status, for example 200 or 404—
Response bodyThe whole body, or one value inside itProperty path (optional)
HeaderOne response headerHeader name
Duration (ms)How long the route took, in milliseconds—
Custom JSAnything you like, written in JavaScriptSee below

Then pick an operator and, if it needs one, an expected value:

OperatorPasses when the value…Works with
equalsis exactly the expected valueall
not equalsis anything but the expected valueall
less thanis smaller than the expected numberStatus code, Duration
greater thanis bigger than the expected numberStatus code, Duration
containshas the expected text somewhere in itResponse body, Header
is true / is falseis true / falseResponse body, Header
existsis present (not missing and not null)Response body, Header
does not existis missing or nullResponse body, Header

Property paths ​

A property path picks one value out of the body. Use dots for objects and [n] for list positions (counting from 0).

For this body:

json
{ "user": { "name": "Ada", "tags": ["admin", "beta"] } }
Property pathValue
(empty)the whole body
user.name"Ada"
user.tags[1]"beta"
user.tags["admin","beta"]

Custom JS checks ​

When a simple check is not enough, choose Custom JS and write checks in JavaScript with t.expect:

js
t.expect(fluxify.response.status).toBe(201);
t.expect(fluxify.response.body.user.email).toContain("@");
t.expect(fluxify.response.body.items).toHaveLength(3);

Each t.expect(...) line shows up as its own line in the results, so a failure tells you exactly what went wrong:

text
✗ expected 500 to be 201

Give a check a name as the second argument to make results easier to read:

js
t.expect(fluxify.response.status, "status").toBe(201);
// ✗ status: expected 500 to be 201

At least one check

A Custom JS assertion must call t.expect at least once. One that does not fails with "Custom JS made no t.expect(...) checks". Returning true or false does nothing.

A failed check does not stop your code: every t.expect line runs and is reported. If your code throws an error instead, the assertion fails with that error.

What you can read ​

NameWhat it is
fluxify.response.statusThe status code
fluxify.response.bodyThe body the route returned
fluxify.response.headersThe response headers (names in lower case)
fluxify.request.pathThe path that was called, with path values filled in
fluxify.request.paramsThe path values, for example { id: "42" }
fluxify.request.queryThe query values
fluxify.request.headersThe request headers
fluxify.request.bodyThe request body
t.setupWhat the suite's setup block returned, if it has one
t.zodThe zod library, to check the shape of a value (see below)

t.expect checks ​

CheckPasses when the value…Example
.toBe(x)is exactly x (same number, text, true/false)t.expect(status).toBe(200)
.toEqual(x)has the same content as x, including inside objects and listst.expect(body).toEqual({ ok: true })
.toBeTruthy()counts as true (not 0, "", null, undefined, false)t.expect(body.id).toBeTruthy()
.toBeFalsy()counts as falset.expect(body.error).toBeFalsy()
.toBeNull()is null
.toBeUndefined()is undefined
.toBeDefined()is not undefined
.toContain(x)is text containing x, or a list containing xt.expect(body.tags).toContain("admin")
.toHaveLength(n)has length n (text or list)t.expect(body.items).toHaveLength(3)
.toHaveProperty(path)has a value at patht.expect(body).toHaveProperty("user.name")
.toHaveProperty(path, x)has x at patht.expect(body).toHaveProperty("user.name", "Ada")
.toMatch(x)is text that contains x, or matches the pattern xt.expect(body.email).toMatch(/@example\.com$/)
.toBeGreaterThan(n)is a number bigger than n
.toBeLessThan(n)is a number smaller than n

Put .not in front of any check to flip it:

js
t.expect(fluxify.response.body.password).not.toBeDefined();
t.expect(fluxify.response.status).not.toBe(500);

toBe or toEqual?

Use toBe for single values such as numbers and text. Use toEqual for objects and lists: two objects with the same content are "equal" but not the "same".

Checking the shape of data ​

t.zod is the zod library. Use it to check that data has the right shape, without listing every value:

js
const User = t.zod.object({
  id: t.zod.number(),
  email: t.zod.email(),
  tags: t.zod.array(t.zod.string()),
});

t.expect(User.safeParse(fluxify.response.body).success, "user shape").toBe(true);

The editor suggests names as you type fluxify., t. and t.zod..

Released under the Apache License 2.0. Enterprise features are under the Fluxify Enterprise Edition License.