chore(demo): add demo code of node --test

This commit is contained in:
kbwo 2024-10-16 23:58:41 +09:00
parent 2bc6a5acf3
commit d275c28e88
3 changed files with 486 additions and 0 deletions

View file

@ -0,0 +1,167 @@
const test = require("node:test");
const { describe, it } = require("node:test");
// # Basic example
test("synchronous passing test", (t) => {
// This test passes because it does not throw an exception.
assert.strictEqual(1, 1);
});
test("synchronous failing test", (t) => {
// This test fails because it throws an exception.
assert.strictEqual(1, 2);
});
test("asynchronous passing test", async (t) => {
// This test passes because the Promise returned by the async
// function is settled and not rejected.
assert.strictEqual(1, 1);
});
test("asynchronous failing test", async (t) => {
// This test fails because the Promise returned by the async
// function is rejected.
assert.strictEqual(1, 2);
});
test("failing test using Promises", (t) => {
// Promises can be used directly as well.
return new Promise((resolve, reject) => {
setImmediate(() => {
reject(new Error("this will cause the test to fail"));
});
});
});
test("callback passing test", (t, done) => {
// done() is the callback function. When the setImmediate() runs, it invokes
// done() with no arguments.
setImmediate(done);
});
test("callback failing test", (t, done) => {
// When the setImmediate() runs, done() is invoked with an Error object and
// the test fails.
setImmediate(() => {
done(new Error("callback failure"));
});
});
// # Subtests
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);
});
});
// # Skipping tests
// The skip option is used, but no message is provided.
test("skip option", { skip: true }, (t) => {
// This code is never executed.
});
// The skip option is used, and a message is provided.
test("skip option with message", { skip: "this is skipped" }, (t) => {
// This code is never executed.
});
test("skip() method", (t) => {
// Make sure to return here as well if the test contains additional logic.
t.skip();
});
test("skip() method with message", (t) => {
// Make sure to return here as well if the test contains additional logic.
t.skip("this is skipped");
});
// # TODO tests
// The todo option is used, but no message is provided.
test("todo option", { todo: true }, (t) => {
// This code is executed, but not treated as a failure.
throw new Error("this does not fail the test");
});
// The todo option is used, and a message is provided.
test("todo option with message", { todo: "this is a todo test" }, (t) => {
// This code is executed.
});
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() and it() aliases
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);
});
});
});
// # only tests
// Assume Node.js is run with the --test-only command-line option.
// The suite's 'only' option is set, so these tests are run.
test("this test is run", { only: true }, async (t) => {
// Within this test, all subtests are run by default.
await t.test("running subtest");
// The test context can be updated to run subtests with the 'only' option.
t.runOnly(true);
await t.test("this subtest is now skipped");
await t.test("this subtest is run", { only: true });
// Switch the context back to execute all tests.
t.runOnly(false);
await t.test("this subtest is now run");
// Explicitly do not run these tests.
await t.test("skipped subtest 3", { only: false });
await t.test("skipped subtest 4", { skip: true });
});
// The 'only' option is not set, so this test is skipped.
test("this test is not run", () => {
// This code is not run.
throw new Error("fail");
});
describe("a suite", () => {
// The 'only' option is set, so this test is run.
it("this test is run", { only: true }, () => {
// This code is run.
});
it("this test is not run", () => {
// This code is not run.
throw new Error("fail");
});
});
describe.only("a suite", () => {
// The 'only' option is set, so this test is run.
it("this test is run", () => {
// This code is run.
});
it("this test is run", () => {
// This code is run.
});
});

307
demo/node-test/output.xml Normal file
View file

@ -0,0 +1,307 @@
<?xml version="1.0" encoding="utf-8"?>
<testsuites>
<testcase name="synchronous passing test" time="0.000709" classname="test" failure="assert is not defined">
<failure type="testCodeFailure" message="assert is not defined">
[Error [ERR_TEST_FAILURE]: assert is not defined] {
failureType: 'testCodeFailure',
cause: ReferenceError [Error]: assert is not defined
at TestContext.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:6:3)
at Test.runInAsyncScope (node:async_hooks:203:9)
at Test.run (node:internal/test_runner/test:631:25)
at Test.start (node:internal/test_runner/test:542:17)
at startSubtest (node:internal/test_runner/harness:214:17),
code: 'ERR_TEST_FAILURE'
}
</failure>
</testcase>
<testcase name="synchronous failing test" time="0.000069" classname="test" failure="assert is not defined">
<failure type="testCodeFailure" message="assert is not defined">
[Error [ERR_TEST_FAILURE]: assert is not defined] {
failureType: 'testCodeFailure',
cause: ReferenceError [Error]: assert is not defined
at TestContext.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:11:3)
at Test.runInAsyncScope (node:async_hooks:203:9)
at Test.run (node:internal/test_runner/test:631:25)
at Test.processPendingSubtests (node:internal/test_runner/test:374:18)
at Test.postRun (node:internal/test_runner/test:715:19)
at Test.run (node:internal/test_runner/test:673:12)
at async startSubtest (node:internal/test_runner/harness:214:3),
code: 'ERR_TEST_FAILURE'
}
</failure>
</testcase>
<testcase name="asynchronous passing test" time="0.000199" classname="test" failure="assert is not defined">
<failure type="testCodeFailure" message="assert is not defined">
[Error [ERR_TEST_FAILURE]: assert is not defined] {
failureType: 'testCodeFailure',
cause: ReferenceError [Error]: assert is not defined
at TestContext.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:17:3)
at Test.runInAsyncScope (node:async_hooks:203:9)
at Test.run (node:internal/test_runner/test:631:25)
at Test.processPendingSubtests (node:internal/test_runner/test:374:18)
at Test.postRun (node:internal/test_runner/test:715:19)
at Test.run (node:internal/test_runner/test:673:12)
at async Test.processPendingSubtests (node:internal/test_runner/test:374:7),
code: 'ERR_TEST_FAILURE'
}
</failure>
</testcase>
<testcase name="asynchronous failing test" time="0.000082" classname="test" failure="assert is not defined">
<failure type="testCodeFailure" message="assert is not defined">
[Error [ERR_TEST_FAILURE]: assert is not defined] {
failureType: 'testCodeFailure',
cause: ReferenceError [Error]: assert is not defined
at TestContext.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:23:3)
at Test.runInAsyncScope (node:async_hooks:203:9)
at Test.run (node:internal/test_runner/test:631:25)
at Test.processPendingSubtests (node:internal/test_runner/test:374:18)
at Test.postRun (node:internal/test_runner/test:715:19)
at Test.run (node:internal/test_runner/test:673:12)
at async Test.processPendingSubtests (node:internal/test_runner/test:374:7),
code: 'ERR_TEST_FAILURE'
}
</failure>
</testcase>
<testcase name="failing test using Promises" time="0.005495" classname="test" failure="this will cause the test to fail">
<failure type="testCodeFailure" message="this will cause the test to fail">
[Error [ERR_TEST_FAILURE]: this will cause the test to fail] {
failureType: 'testCodeFailure',
cause: Error: this will cause the test to fail
at Immediate.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:30:14)
at process.processImmediate (node:internal/timers:476:21),
code: 'ERR_TEST_FAILURE'
}
</failure>
</testcase>
<testcase name="callback passing test" time="0.000422" classname="test"/>
<testcase name="callback failing test" time="0.000314" classname="test" failure="callback failure">
<failure type="testCodeFailure" message="callback failure">
[Error [ERR_TEST_FAILURE]: callback failure] {
failureType: 'testCodeFailure',
cause: Error: callback failure
at Immediate.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:45:10)
at process.processImmediate (node:internal/timers:476:21),
code: 'ERR_TEST_FAILURE'
}
</failure>
</testcase>
<testsuite name="top level test" time="0.000546" disabled="0" errors="0" tests="2" failures="2" skipped="0" hostname="test-user-xxx">
<testcase name="subtest 1" time="0.000082" classname="test" failure="assert is not defined">
<failure type="testCodeFailure" message="assert is not defined">
Error [ERR_TEST_FAILURE]: assert is not defined
at async TestContext.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:51:3) {
failureType: 'testCodeFailure',
cause: ReferenceError [Error]: assert is not defined
at TestContext.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:52:5)
at Test.runInAsyncScope (node:async_hooks:203:9)
at Test.run (node:internal/test_runner/test:631:25)
at Test.start (node:internal/test_runner/test:542:17)
at TestContext.test (node:internal/test_runner/test:167:20)
at TestContext.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:51:11)
at Test.runInAsyncScope (node:async_hooks:203:9)
at Test.run (node:internal/test_runner/test:631:25)
at Test.processPendingSubtests (node:internal/test_runner/test:374:18)
at Test.postRun (node:internal/test_runner/test:715:19),
code: 'ERR_TEST_FAILURE'
}
</failure>
</testcase>
<testcase name="subtest 2" time="0.000112" classname="test" failure="assert is not defined">
<failure type="testCodeFailure" message="assert is not defined">
Error [ERR_TEST_FAILURE]: assert is not defined
at async TestContext.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:55:3) {
failureType: 'testCodeFailure',
cause: ReferenceError [Error]: assert is not defined
at TestContext.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:56:5)
at Test.runInAsyncScope (node:async_hooks:203:9)
at Test.run (node:internal/test_runner/test:631:25)
at Test.start (node:internal/test_runner/test:542:17)
at TestContext.test (node:internal/test_runner/test:167:20)
at TestContext.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:55:11)
at async Test.run (node:internal/test_runner/test:632:9)
at async Test.processPendingSubtests (node:internal/test_runner/test:374:7),
code: 'ERR_TEST_FAILURE'
}
</failure>
</testcase>
</testsuite>
<testcase name="skip option" time="0.000047" classname="test">
<skipped type="skipped" message="true"/>
</testcase>
<testcase name="skip option with message" time="0.000034" classname="test">
<skipped type="skipped" message="this is skipped"/>
</testcase>
<testcase name="skip() method" time="0.000052" classname="test">
<skipped type="skipped" message="true"/>
</testcase>
<testcase name="skip() method with message" time="0.000082" classname="test">
<skipped type="skipped" message="this is skipped"/>
</testcase>
<testcase name="todo option" time="0.000052" classname="test" failure="this does not fail the test">
<skipped type="todo" message="true"/>
<failure type="testCodeFailure" message="this does not fail the test">
[Error [ERR_TEST_FAILURE]: this does not fail the test] {
failureType: 'testCodeFailure',
cause: Error: this does not fail the test
at TestContext.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:85:9)
at Test.runInAsyncScope (node:async_hooks:203:9)
at Test.run (node:internal/test_runner/test:631:25)
at Test.processPendingSubtests (node:internal/test_runner/test:374:18)
at Test.postRun (node:internal/test_runner/test:715:19)
at Test.run (node:internal/test_runner/test:673:12)
at async Test.processPendingSubtests (node:internal/test_runner/test:374:7),
code: 'ERR_TEST_FAILURE'
}
</failure>
</testcase>
<testcase name="todo option with message" time="0.000093" classname="test">
<skipped type="todo" message="this is a todo test"/>
</testcase>
<testcase name="todo() method" time="0.000090" classname="test">
<skipped type="todo" message="true"/>
</testcase>
<testcase name="todo() method with message" time="0.000054" classname="test" failure="this does not fail the test">
<skipped type="todo" message="this is a todo test and is not treated as a failure"/>
<failure type="testCodeFailure" message="this does not fail the test">
[Error [ERR_TEST_FAILURE]: this does not fail the test] {
failureType: 'testCodeFailure',
cause: Error: this does not fail the test
at TestContext.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:99:9)
at Test.runInAsyncScope (node:async_hooks:203:9)
at Test.run (node:internal/test_runner/test:631:25)
at Test.processPendingSubtests (node:internal/test_runner/test:374:18)
at Test.postRun (node:internal/test_runner/test:715:19)
at Test.run (node:internal/test_runner/test:673:12)
at async Test.processPendingSubtests (node:internal/test_runner/test:374:7),
code: 'ERR_TEST_FAILURE'
}
</failure>
</testcase>
<testsuite name="A thing" time="0.000522" disabled="0" errors="0" tests="3" failures="3" skipped="1" hostname="test-user-xxx">
<testcase name="should work" time="0.000110" classname="test" failure="assert is not defined">
<failure type="testCodeFailure" message="assert is not defined">
Error [ERR_TEST_FAILURE]: assert is not defined
at async Promise.all (index 0) {
failureType: 'testCodeFailure',
cause: ReferenceError [Error]: assert is not defined
at TestContext.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:105:5)
at Test.runInAsyncScope (node:async_hooks:203:9)
at Test.run (node:internal/test_runner/test:631:25)
at Test.start (node:internal/test_runner/test:542:17)
at node:internal/test_runner/test:946:71
at node:internal/per_context/primordials:487:82
at new Promise (&lt;anonymous>)
at new SafePromise (node:internal/per_context/primordials:455:29)
at node:internal/per_context/primordials:487:9
at Array.map (&lt;anonymous>),
code: 'ERR_TEST_FAILURE'
}
</failure>
</testcase>
<testcase name="should be ok" time="0.000053" classname="test" failure="assert is not defined">
<failure type="testCodeFailure" message="assert is not defined">
[Error [ERR_TEST_FAILURE]: assert is not defined] {
failureType: 'testCodeFailure',
cause: ReferenceError [Error]: assert is not defined
at TestContext.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:109:5)
at Test.runInAsyncScope (node:async_hooks:203:9)
at Test.run (node:internal/test_runner/test:631:25)
at Suite.processPendingSubtests (node:internal/test_runner/test:374:18)
at Test.postRun (node:internal/test_runner/test:715:19)
at Test.run (node:internal/test_runner/test:673:12)
at async Promise.all (index 0)
at async Suite.run (node:internal/test_runner/test:948:7)
at async Test.processPendingSubtests (node:internal/test_runner/test:374:7),
code: 'ERR_TEST_FAILURE'
}
</failure>
</testcase>
<testsuite name="a nested thing" time="0.000125" disabled="0" errors="0" tests="1" failures="1" skipped="0" hostname="test-user-xxx">
<testcase name="should work" time="0.000054" classname="test" failure="assert is not defined">
<failure type="testCodeFailure" message="assert is not defined">
Error [ERR_TEST_FAILURE]: assert is not defined
at async Promise.all (index 0) {
failureType: 'testCodeFailure',
cause: ReferenceError [Error]: assert is not defined
at TestContext.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:114:7)
at Test.runInAsyncScope (node:async_hooks:203:9)
at Test.run (node:internal/test_runner/test:631:25)
at Test.start (node:internal/test_runner/test:542:17)
at node:internal/test_runner/test:946:71
at node:internal/per_context/primordials:487:82
at new Promise (&lt;anonymous>)
at new SafePromise (node:internal/per_context/primordials:455:29)
at node:internal/per_context/primordials:487:9
at Array.map (&lt;anonymous>),
code: 'ERR_TEST_FAILURE'
}
</failure>
</testcase>
</testsuite>
</testsuite>
<testsuite name="this test is run" time="0.000471" disabled="0" errors="0" tests="6" failures="0" skipped="1" hostname="test-user-xxx">
<testcase name="running subtest" time="0.000043" classname="test"/>
<testcase name="this subtest is now skipped" time="0.000063" classname="test"/>
<!-- 'only' and 'runOnly' require the &#45;&#45;test-only command-line option. -->
<testcase name="this subtest is run" time="0.000026" classname="test"/>
<!-- 'only' and 'runOnly' require the &#45;&#45;test-only command-line option. -->
<testcase name="this subtest is now run" time="0.000019" classname="test"/>
<testcase name="skipped subtest 3" time="0.000018" classname="test"/>
<!-- 'only' and 'runOnly' require the &#45;&#45;test-only command-line option. -->
<testcase name="skipped subtest 4" time="0.000017" classname="test">
<skipped type="skipped" message="true"/>
</testcase>
</testsuite>
<!-- 'only' and 'runOnly' require the &#45;&#45;test-only command-line option. -->
<testcase name="this test is not run" time="0.000048" classname="test" failure="fail">
<failure type="testCodeFailure" message="fail">
[Error [ERR_TEST_FAILURE]: fail] {
failureType: 'testCodeFailure',
cause: Error: fail
at TestContext.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:143:9)
at Test.runInAsyncScope (node:async_hooks:203:9)
at Test.run (node:internal/test_runner/test:631:25)
at Test.processPendingSubtests (node:internal/test_runner/test:374:18)
at Test.postRun (node:internal/test_runner/test:715:19)
at Test.run (node:internal/test_runner/test:673:12)
at async Test.processPendingSubtests (node:internal/test_runner/test:374:7),
code: 'ERR_TEST_FAILURE'
}
</failure>
</testcase>
<testsuite name="a suite" time="0.000180" disabled="0" errors="0" tests="2" failures="1" skipped="0" hostname="test-user-xxx">
<testcase name="this test is run" time="0.000038" classname="test"/>
<!-- 'only' and 'runOnly' require the &#45;&#45;test-only command-line option. -->
<testcase name="this test is not run" time="0.000033" classname="test" failure="fail">
<failure type="testCodeFailure" message="fail">
[Error [ERR_TEST_FAILURE]: fail] {
failureType: 'testCodeFailure',
cause: Error: fail
at TestContext.&lt;anonymous> (/home/test-user/projects/testing-language-server/demo/node-test/index.test.js:154:11)
at Test.runInAsyncScope (node:async_hooks:203:9)
at Test.run (node:internal/test_runner/test:631:25)
at Suite.processPendingSubtests (node:internal/test_runner/test:374:18)
at Test.postRun (node:internal/test_runner/test:715:19)
at Test.run (node:internal/test_runner/test:673:12)
at async Promise.all (index 0)
at async Suite.run (node:internal/test_runner/test:948:7)
at async Test.processPendingSubtests (node:internal/test_runner/test:374:7),
code: 'ERR_TEST_FAILURE'
}
</failure>
</testcase>
</testsuite>
<testsuite name="a suite" time="0.000118" disabled="0" errors="0" tests="2" failures="0" skipped="0" hostname="test-user-xxx">
<testcase name="this test is run" time="0.000038" classname="test"/>
<testcase name="this test is run" time="0.000023" classname="test"/>
</testsuite>
<!-- 'only' and 'runOnly' require the &#45;&#45;test-only command-line option. -->
<!-- tests 33 -->
<!-- suites 4 -->
<!-- pass 10 -->
<!-- fail 14 -->
<!-- cancelled 0 -->
<!-- skipped 5 -->
<!-- todo 4 -->
<!-- duration_ms 57.894356 -->
</testsuites>

View file

@ -0,0 +1,12 @@
{
"name": "test",
"version": "1.0.0",
"main": "index.test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": ""
}