-
Notifications
You must be signed in to change notification settings - Fork 0
Assertions
Queue them on the builder (they replay in order when awaited) or call them on a
resolved response. Each returns the response and throws AssertionError on
failure, with messages like <METHOD> <URL> -> <what went wrong>.
Status: expectStatus(code), expectStatusIn(...codes), expectOk(),
expectClientError(), expectServerError(), expectRedirect(),
expectCreated(), expectAccepted(), expectNoContent(), expectBadRequest(),
expectUnauthorized(), expectForbidden(), expectNotFound().
Headers and cookies: expectHeader(name, matcher?),
expectHeaderContains(name, substr), expectHeaderAbsent(name),
expectContentType(type), expectCookie(name, matcher?).
Body and JSON: expectJson(path, expected?), expectJsonLength(path, n),
expectArrayLength(path, n), expectJsonContains(path, value),
expectJsonSchema(schema), expectSorted(path, options?), expectBody(matcher),
expectBodyContains(substr), expectEmpty(), expectNotEmpty(),
expectTimeBelow(ms), check(label, fn), expectValue(path).
const res = await api.get("/users").expectOk();
res.expectValue("data[0].id").toBeGreaterThan(0);
res.expectJsonContains("users", { id: 2 });
res.expectSorted("data", { key: "id", order: "asc" });expectHeader, expectJson, and expectBody accept a flexible matcher: a
RegExp (tested against the string), a function (predicate), an object or array
(deep equality), or a primitive (strict ===).
expect(value) works on anything, with .not and .resolves / .rejects.
import { expect } from "two-go";
expect(2 + 2).toBe(4);
expect({ a: 1, b: 2 }).toMatchObject({ a: 1 });
expect([1, 2, 3]).toContain(2);
expect("hello").toMatch(/ell/);
expect(value).not.toBeNull();
await expect(Promise.resolve(5)).resolves.toBe(5);Matchers: toBe, toEqual, toStrictEqual, toBeTruthy/Falsy,
toBeNull/Undefined/Defined/NaN, toBeGreaterThan(OrEqual),
toBeLessThan(OrEqual), toBeCloseTo, toContain, toContainEqual, toMatch,
toMatchObject, toHaveLength, toHaveProperty, toBeInstanceOf, toBeType,
toBeOneOf, toThrow, toSatisfy, toBeEmpty.
Collect every failure and report them together:
import { softly } from "two-go";
await softly((expect) => {
expect(res.status).toBe(200);
expect(res.get("data")).toHaveLength(2);
});import { validate } from "two-go/schema";
import { inferSchema } from "two-go";
validate({ id: 1 }, { type: "object", required: ["id"], properties: { id: { type: "integer" } } });
const res = await api.get("/users");
await api.get("/users").expectJsonSchema(res.toSchema());