From 90e81aaad7198e1bd6536b5b33b5c439786a8595 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sun, 26 May 2024 12:36:46 -0400 Subject: [PATCH 1/3] test_runner: add context.fullName This commit adds a fullName getter to the TestContext and SuiteContext classes. This is similar to the existing name getter, but also includes the name of all ancestor tests/suites. PR-URL: https://github.com/nodejs/node/pull/53169 Reviewed-By: Moshe Atlow Reviewed-By: Benjamin Gruenbaum Reviewed-By: Geoffrey Booth Co-authored-by: Jacob Smith --- doc/api/test.md | 8 ++++++++ lib/internal/test_runner/test.js | 18 ++++++++++++++++ test/parallel/test-runner-test-fullname.js | 24 ++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 test/parallel/test-runner-test-fullname.js diff --git a/doc/api/test.md b/doc/api/test.md index 7069dbc38ed9a8..5ce1d8f3d2286f 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -3044,6 +3044,14 @@ test('top level test', (t) => { }); ``` +### `context.fullName` + + + +The name of the test and each of its ancestors, separated by `>`. + ### `context.name` + +An object containing assertion methods bound to `context`. The top-level +functions from the `node:assert` module are exposed here for the purpose of +creating test plans. + +```js +test('test', (t) => { + t.plan(1); + t.assert.strictEqual(true, true); +}); +``` + ### `context.diagnostic(message)` + +> Stability: 1.0 - Early development + +Enable [snapshot testing][] in the test runner. + ### `--experimental-vm-modules` + +> Stability: 1.0 - Early development + +Regenerates the snapshot file used by the test runner for [snapshot testing][]. +Node.js must be started with the `--experimental-test-snapshots` flag in order +to use this functionality. + ### `--throw-deprecation` + +> Stability: 1.0 - Early development + +An object whose methods are used to configure default snapshot settings in the +current process. It is possible to apply the same configuration to all files by +placing common configuration code in a module preloaded with `--require` or +`--import`. + +### `snapshot.setDefaultSnapshotSerializers(serializers)` + + + +> Stability: 1.0 - Early development + +* `serializers` {Array} An array of synchronous functions used as the default + serializers for snapshot tests. + +This function is used to customize the default serialization mechanism used by +the test runner. By default, the test runner performs serialization by calling +`JSON.stringify(value, null, 2)` on the provided value. `JSON.stringify()` does +have limitations regarding circular structures and supported data types. If a +more robust serialization mechanism is required, this function should be used. + +### `snapshot.setResolveSnapshotPath(fn)` + + + +> Stability: 1.0 - Early development + +* `fn` {Function} A function used to compute the location of the snapshot file. + The function receives the path of the test file as its only argument. If the + `process.argv[1]` is not associated with a file (for example in the REPL), + the input is undefined. `fn()` must return a string specifying the location of + the snapshot file. + +This function is used to customize the location of the snapshot file used for +snapshot testing. By default, the snapshot filename is the same as the entry +point filename with a `.snapshot` file extension. + ## Class: `MockFunctionContext` + +> Stability: 1.0 - Early development + +* `value` {any} A value to serialize to a string. If Node.js was started with + the [`--test-update-snapshots`][] flag, the serialized value is written to + the snapshot file. Otherwise, the serialized value is compared to the + corresponding value in the existing snapshot file. +* `options` {Object} Optional configuration options. The following properties + are supported: + * `serializers` {Array} An array of synchronous functions used to serialize + `value` into a string. `value` is passed as the only argument to the first + serializer function. The return value of each serializer is passed as input + to the next serializer. Once all serializers have run, the resulting value + is coerced to a string. **Default:** If no serializers are provided, the + test runner's default serializers are used. + +This function implements assertions for snapshot testing. + +```js +test('snapshot test with default serialization', (t) => { + t.assert.snapshot({ value1: 1, value2: 2 }); +}); + +test('snapshot test with custom serialization', (t) => { + t.assert.snapshot({ value3: 3, value4: 4 }, { + serializers: [(value) => JSON.stringify(value)] + }); +}); +``` + ### `context.diagnostic(message)`