Ende.TestContext

interface

interface Ende.TestContext

Context that is passed to a testing function, which can be used to either gain information about the current test, or register additional test steps within the current test.

Properties

name

name: string

The current test name.

origin

origin: string

The string URL of the current test.

parent

parent?: TestContext

If the current test is a step of another test, the parent test context will be set here.

Methods

step

step(definition: TestStepDefinition): Promise<boolean>

Run a sub step of the parent test or step. Returns a promise that resolves to a boolean signifying if the step completed successfully.

The returned promise never rejects unless the arguments are invalid.

If the test was ignored the promise returns false.

Ende.test({
  name: "a parent test",
  async fn(t) {
    console.log("before the step");
    await t.step({
      name: "step 1",
      fn(t) {
        console.log("current step:", t.name);
      }
    });
    console.log("after the step");
  }
});
step(name: string, fn: (t: TestContext) => void | Promise<void>): Promise<boolean>

Run a sub step of the parent test or step. Returns a promise that resolves to a boolean signifying if the step completed successfully.

The returned promise never rejects unless the arguments are invalid.

If the test was ignored the promise returns false.

Ende.test(
  "a parent test",
  async (t) => {
    console.log("before the step");
    await t.step(
      "step 1",
      (t) => {
        console.log("current step:", t.name);
      }
    );
    console.log("after the step");
  }
);
step(fn: (t: TestContext) => void | Promise<void>): Promise<boolean>

Run a sub step of the parent test or step. Returns a promise that resolves to a boolean signifying if the step completed successfully.

The returned promise never rejects unless the arguments are invalid.

If the test was ignored the promise returns false.

Ende.test(async function aParentTest(t) {
  console.log("before the step");
  await t.step(function step1(t) {
    console.log("current step:", t.name);
  });
  console.log("after the step");
});

assertSnapshot

assertSnapshot<T>(actual: T, options?: TestSnapshotOptions<T>): Promise<void>

Assert that actual matches a snapshot stored in a snapshot file.

The snapshot is stored in __snapshots__/<test file name>.snap next to the test file, under a key derived from the test (and step) name. On the first run, create the snapshot file by running the tests with the --update-snapshots flag; commit it alongside the test. On subsequent runs the assertion fails if the serialized value no longer matches the stored snapshot. To intentionally change snapshots, run the tests with --update-snapshots again.

No read or write permissions are needed for snapshot files in the default location; a custom dir or path requires them.

The snapshot file format is compatible with assertSnapshot from @ende/testing/snapshot.

Ende.test("matches snapshot", async (t) => {
  await t.assertSnapshot({ hello: "world", example: 123 });
});
assertSnapshot<T>(actual: T, message?: string): Promise<void>

Assert that actual matches a snapshot stored in a snapshot file, using message as the failure message if it does not.

Ende.test("matches snapshot", async (t) => {
  await t.assertSnapshot(2 + 3, "should be five");
});