const test = require("node:test");
const { describe, it } = require("node:test");
const assert = require("node:assert");
const { throwError } = require("./util.js");
test("synchronous passing test", (t) => {
assert.strictEqual(1, 1);
});
test("synchronous failing test", (t) => {
assert.strictEqual(1, 2);
});
test("asynchronous passing test", async (t) => {
assert.strictEqual(1, 1);
});
test("asynchronous failing test", async (t) => {
assert.strictEqual(1, 2);
});
test("failing test using Promises", (t) => {
return new Promise((resolve, reject) => {
setImmediate(() => {
reject(new Error("this will cause the test to fail"));
});
});
});
test("callback passing test", (t, done) => {
setImmediate(done);
});
test("callback failing test", (t, done) => {
setImmediate(() => {
done(new Error("callback failure"));
});
});
test("top level test", async (t) => {
await t.test("subtest 1", (t) => {
assert.strictEqual(1, 1);
});
await t.test("subtest 2", (t) => {
assert.strictEqual(2, 2);
});
});
test("skip option", { skip: true }, (t) => {
});
test("skip option with message", { skip: "this is skipped" }, (t) => {
});
test("skip() method", (t) => {
t.skip();
});
test("skip() method with message", (t) => {
t.skip("this is skipped");
});
test("todo option", { todo: true }, (t) => {
throw new Error("this does not fail the test");
});
test("todo option with message", { todo: "this is a todo test" }, (t) => {
});
test("todo() method", (t) => {
t.todo();
});
test("todo() method with message", (t) => {
t.todo("this is a todo test and is not treated as a failure");
throw new Error("this does not fail the test");
});
describe("A thing", () => {
it("should work", () => {
assert.strictEqual(1, 1);
});
it("should be ok", () => {
assert.strictEqual(2, 2);
});
describe("a nested thing", () => {
it("should work", () => {
assert.strictEqual(3, 3);
});
});
});
test("only: this test is run", { only: true }, async (t) => {
await t.test("running subtest");
t.runOnly(true);
await t.test("this subtest is now skipped");
await t.test("this subtest is run", { only: true });
t.runOnly(false);
await t.test("this subtest is now run");
await t.test("skipped subtest 3", { only: false });
await t.test("skipped subtest 4", { skip: true });
});
test("only: this test is not run", () => {
throw new Error("fail");
});
describe("A suite", () => {
it("this test is run A ", { only: true }, () => {
});
it("this test is not run B", () => {
throw new Error("fail");
});
});
describe.only("B suite", () => {
it("this test is run C", () => {
});
it("this test is run D", () => {
});
});
test("import from external file. this must be fail", () => {
throwError();
});