mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
test_runner: mark snapshot testing as stable
This commit marks the test runner's snapshot testing API as stable. PR-URL: https://github.com/nodejs/node/pull/55897 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
This commit is contained in:
parent
01b9a54a58
commit
c921676512
@ -1121,16 +1121,6 @@ added:
|
||||
|
||||
Enable module mocking in the test runner.
|
||||
|
||||
### `--experimental-test-snapshots`
|
||||
|
||||
<!-- YAML
|
||||
added: v22.3.0
|
||||
-->
|
||||
|
||||
> Stability: 1.0 - Early development
|
||||
|
||||
Enable [snapshot testing][] in the test runner.
|
||||
|
||||
### `--experimental-vm-modules`
|
||||
|
||||
<!-- YAML
|
||||
@ -2472,13 +2462,13 @@ subtests inherit this value from their parent. The default value is `Infinity`.
|
||||
|
||||
<!-- YAML
|
||||
added: v22.3.0
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/55897
|
||||
description: Snapsnot testing is no longer experimental.
|
||||
-->
|
||||
|
||||
> Stability: 1.0 - Early development
|
||||
|
||||
Regenerates the snapshot files 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`
|
||||
|
||||
|
@ -937,8 +937,7 @@ compared against a set of known good values. The known good values are known as
|
||||
snapshots, and are stored in a snapshot file. Snapshot files are managed by the
|
||||
test runner, but are designed to be human readable to aid in debugging. Best
|
||||
practice is for snapshot files to be checked into source control along with your
|
||||
test files. In order to enable snapshot testing, Node.js must be started with
|
||||
the [`--experimental-test-snapshots`][] command-line flag.
|
||||
test files.
|
||||
|
||||
Snapshot files are generated by starting Node.js with the
|
||||
[`--test-update-snapshots`][] command-line flag. A separate snapshot file is
|
||||
@ -3593,7 +3592,6 @@ Can be used to abort test subtasks when the test has been aborted.
|
||||
[`--experimental-strip-types`]: cli.md#--experimental-strip-types
|
||||
[`--experimental-test-coverage`]: cli.md#--experimental-test-coverage
|
||||
[`--experimental-test-module-mocks`]: cli.md#--experimental-test-module-mocks
|
||||
[`--experimental-test-snapshots`]: cli.md#--experimental-test-snapshots
|
||||
[`--import`]: cli.md#--importmodule
|
||||
[`--test-concurrency`]: cli.md#--test-concurrency
|
||||
[`--test-coverage-include`]: cli.md#--test-coverage-include
|
||||
|
@ -191,9 +191,6 @@ Configures the type of test isolation used in the test runner.
|
||||
.It Fl -experimental-test-module-mocks
|
||||
Enable module mocking in the test runner.
|
||||
.
|
||||
.It Fl -experimental-test-snapshots
|
||||
Enable snapshot testing in the test runner.
|
||||
.
|
||||
.It Fl -experimental-strip-types
|
||||
Enable experimental type-stripping for TypeScript files.
|
||||
.
|
||||
|
@ -104,6 +104,7 @@ function lazyAssertObject(harness) {
|
||||
if (assertObj === undefined) {
|
||||
assertObj = new SafeMap();
|
||||
const assert = require('assert');
|
||||
const { SnapshotManager } = require('internal/test_runner/snapshot');
|
||||
const methodsToCopy = [
|
||||
'deepEqual',
|
||||
'deepStrictEqual',
|
||||
@ -126,12 +127,8 @@ function lazyAssertObject(harness) {
|
||||
assertObj.set(methodsToCopy[i], assert[methodsToCopy[i]]);
|
||||
}
|
||||
|
||||
const { getOptionValue } = require('internal/options');
|
||||
if (getOptionValue('--experimental-test-snapshots')) {
|
||||
const { SnapshotManager } = require('internal/test_runner/snapshot');
|
||||
harness.snapshotManager = new SnapshotManager(harness.config.updateSnapshots);
|
||||
assertObj.set('snapshot', harness.snapshotManager.createAssert());
|
||||
}
|
||||
harness.snapshotManager = new SnapshotManager(harness.config.updateSnapshots);
|
||||
assertObj.set('snapshot', harness.snapshotManager.createAssert());
|
||||
}
|
||||
return assertObj;
|
||||
}
|
||||
|
43
lib/test.js
43
lib/test.js
@ -7,7 +7,6 @@ const {
|
||||
|
||||
const { test, suite, before, after, beforeEach, afterEach } = require('internal/test_runner/harness');
|
||||
const { run } = require('internal/test_runner/runner');
|
||||
const { getOptionValue } = require('internal/options');
|
||||
|
||||
module.exports = test;
|
||||
ObjectAssign(module.exports, {
|
||||
@ -39,28 +38,26 @@ ObjectDefineProperty(module.exports, 'mock', {
|
||||
},
|
||||
});
|
||||
|
||||
if (getOptionValue('--experimental-test-snapshots')) {
|
||||
let lazySnapshot;
|
||||
let lazySnapshot;
|
||||
|
||||
ObjectDefineProperty(module.exports, 'snapshot', {
|
||||
__proto__: null,
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
get() {
|
||||
if (lazySnapshot === undefined) {
|
||||
const {
|
||||
setDefaultSnapshotSerializers,
|
||||
setResolveSnapshotPath,
|
||||
} = require('internal/test_runner/snapshot');
|
||||
ObjectDefineProperty(module.exports, 'snapshot', {
|
||||
__proto__: null,
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
get() {
|
||||
if (lazySnapshot === undefined) {
|
||||
const {
|
||||
setDefaultSnapshotSerializers,
|
||||
setResolveSnapshotPath,
|
||||
} = require('internal/test_runner/snapshot');
|
||||
|
||||
lazySnapshot = {
|
||||
__proto__: null,
|
||||
setDefaultSnapshotSerializers,
|
||||
setResolveSnapshotPath,
|
||||
};
|
||||
}
|
||||
lazySnapshot = {
|
||||
__proto__: null,
|
||||
setDefaultSnapshotSerializers,
|
||||
setResolveSnapshotPath,
|
||||
};
|
||||
}
|
||||
|
||||
return lazySnapshot;
|
||||
},
|
||||
});
|
||||
}
|
||||
return lazySnapshot;
|
||||
},
|
||||
});
|
||||
|
@ -692,9 +692,7 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
|
||||
AddOption("--experimental-test-module-mocks",
|
||||
"enable module mocking in the test runner",
|
||||
&EnvironmentOptions::test_runner_module_mocks);
|
||||
AddOption("--experimental-test-snapshots",
|
||||
"enable snapshot testing in the test runner",
|
||||
&EnvironmentOptions::test_runner_snapshots);
|
||||
AddOption("--experimental-test-snapshots", "", NoOp{});
|
||||
AddOption("--test-name-pattern",
|
||||
"run tests whose name matches this regular expression",
|
||||
&EnvironmentOptions::test_name_pattern,
|
||||
|
@ -189,7 +189,6 @@ class EnvironmentOptions : public Options {
|
||||
uint64_t test_coverage_functions = 0;
|
||||
uint64_t test_coverage_lines = 0;
|
||||
bool test_runner_module_mocks = false;
|
||||
bool test_runner_snapshots = false;
|
||||
bool test_runner_update_snapshots = false;
|
||||
std::vector<std::string> test_name_pattern;
|
||||
std::vector<std::string> test_reporter;
|
||||
|
@ -3,13 +3,14 @@ require('../common');
|
||||
const assert = require('node:assert');
|
||||
const test = require('node:test');
|
||||
|
||||
const uncopiedKeys = [
|
||||
'AssertionError',
|
||||
'CallTracker',
|
||||
'strict',
|
||||
];
|
||||
test('only methods from node:assert are on t.assert', (t) => {
|
||||
const expectedKeys = Object.keys(assert).filter((key) => !uncopiedKeys.includes(key)).sort();
|
||||
test('expected methods are on t.assert', (t) => {
|
||||
const uncopiedKeys = [
|
||||
'AssertionError',
|
||||
'CallTracker',
|
||||
'strict',
|
||||
];
|
||||
const assertKeys = Object.keys(assert).filter((key) => !uncopiedKeys.includes(key));
|
||||
const expectedKeys = ['snapshot'].concat(assertKeys).sort();
|
||||
assert.deepStrictEqual(Object.keys(t.assert).sort(), expectedKeys);
|
||||
});
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Flags: --expose-internals --experimental-test-snapshots
|
||||
// Flags: --expose-internals
|
||||
/* eslint-disable no-template-curly-in-string */
|
||||
'use strict';
|
||||
const common = require('../common');
|
||||
@ -299,7 +299,7 @@ test('t.assert.snapshot()', async (t) => {
|
||||
await t.test('fails prior to snapshot generation', async (t) => {
|
||||
const child = await common.spawnPromisified(
|
||||
process.execPath,
|
||||
['--experimental-test-snapshots', fixture],
|
||||
[fixture],
|
||||
{ cwd: tmpdir.path },
|
||||
);
|
||||
|
||||
@ -314,7 +314,7 @@ test('t.assert.snapshot()', async (t) => {
|
||||
await t.test('passes when regenerating snapshots', async (t) => {
|
||||
const child = await common.spawnPromisified(
|
||||
process.execPath,
|
||||
['--test-update-snapshots', '--experimental-test-snapshots', fixture],
|
||||
['--test-update-snapshots', fixture],
|
||||
{ cwd: tmpdir.path },
|
||||
);
|
||||
|
||||
@ -328,7 +328,7 @@ test('t.assert.snapshot()', async (t) => {
|
||||
await t.test('passes when snapshots exist', async (t) => {
|
||||
const child = await common.spawnPromisified(
|
||||
process.execPath,
|
||||
['--experimental-test-snapshots', fixture],
|
||||
[fixture],
|
||||
{ cwd: tmpdir.path },
|
||||
);
|
||||
|
||||
@ -350,7 +350,6 @@ test('snapshots from multiple files (isolation=none)', async (t) => {
|
||||
const args = [
|
||||
'--test',
|
||||
'--experimental-test-isolation=none',
|
||||
'--experimental-test-snapshots',
|
||||
fixture,
|
||||
fixture2,
|
||||
];
|
||||
@ -372,7 +371,6 @@ test('snapshots from multiple files (isolation=none)', async (t) => {
|
||||
const args = [
|
||||
'--test',
|
||||
'--experimental-test-isolation=none',
|
||||
'--experimental-test-snapshots',
|
||||
'--test-update-snapshots',
|
||||
fixture,
|
||||
fixture2,
|
||||
@ -394,7 +392,6 @@ test('snapshots from multiple files (isolation=none)', async (t) => {
|
||||
const args = [
|
||||
'--test',
|
||||
'--experimental-test-isolation=none',
|
||||
'--experimental-test-snapshots',
|
||||
fixture,
|
||||
fixture2,
|
||||
];
|
||||
|
Loading…
Reference in New Issue
Block a user