test_runner: align behavior of it and test

PR-URL: https://github.com/nodejs/node/pull/46889
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Moshe Atlow 2023-02-28 21:58:49 +02:00
parent 0b9cf6fbec
commit ca033c16fe
No known key found for this signature in database
GPG Key ID: CB1159A696865149
6 changed files with 37 additions and 44 deletions

View File

@ -156,8 +156,7 @@ test('skip() method with message', (t) => {
Running tests can also be done using `describe` to declare a suite
and `it` to declare a test.
A suite is used to organize and group related tests together.
`it` is an alias for `test`, except there is no test context passed,
since nesting is done using suites.
`it` is a shorthand for [`test()`][].
```js
describe('A thing', () => {
@ -851,17 +850,19 @@ Shorthand for marking a suite as `only`, same as
## `it([name][, options][, fn])`
* `name` {string} The name of the test, which is displayed when reporting test
results. **Default:** The `name` property of `fn`, or `'<anonymous>'` if `fn`
does not have a name.
* `options` {Object} Configuration options for the suite.
supports the same options as `test([name][, options][, fn])`.
* `fn` {Function|AsyncFunction} The function under test.
If the test uses callbacks, the callback function is passed as an argument.
**Default:** A no-op function.
* Returns: `undefined`.
<!-- YAML
added:
- v18.6.0
- v16.17.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/46889
description: Calling `it()` is now equivalent to calling `test()`.
-->
The `it()` function is the value imported from the `node:test` module.
Shorthand for [`test()`][].
The `it()` function is imported from the `node:test` module.
## `it.skip([name][, options][, fn])`

View File

@ -17,7 +17,7 @@ const {
const { exitCodes: { kGenericUserError } } = internalBinding('errors');
const { kEmptyObject } = require('internal/util');
const { kCancelledByParent, Test, ItTest, Suite } = require('internal/test_runner/test');
const { kCancelledByParent, Test, Suite } = require('internal/test_runner/test');
const {
kAsyncBootstrapFailure,
parseCommandLine,
@ -221,7 +221,7 @@ module.exports = {
createTestTree,
test: runInParentContext(Test, false),
describe: runInParentContext(Suite),
it: runInParentContext(ItTest),
it: runInParentContext(Test),
before: hook('before'),
after: hook('after'),
beforeEach: hook('beforeEach'),

View File

@ -745,13 +745,6 @@ class TestHook extends Test {
}
}
class ItTest extends Test {
constructor(opt) { super(opt); } // eslint-disable-line no-useless-constructor
getRunArgs() {
return { ctx: { signal: this.signal, name: this.name }, args: [] };
}
}
class Suite extends Test {
constructor(options) {
super(options);
@ -824,7 +817,6 @@ class Suite extends Test {
}
module.exports = {
ItTest,
kCancelledByParent,
kSubtestsFailed,
kTestCodeFailure,

View File

@ -9,7 +9,7 @@ const { describe, it } = require('node:test');
describe('ESM: REPL runs', { concurrency: true }, () => {
it((done) => {
it((t, done) => {
const child = spawn(execPath, [
'--interactive',
], {

View File

@ -47,7 +47,7 @@ it('async throw fail', async () => {
throw new Error('thrown from async throw fail');
});
it('async skip fail', async (t) => {
it('async skip fail', async (t, done) => {
t.skip();
throw new Error('thrown from async throw fail');
});
@ -206,61 +206,61 @@ it('escaped skip message', { skip: '#skip' });
// A test whose todo message needs to be escaped.
it('escaped todo message', { todo: '#todo' });
it('callback pass', (done) => {
it('callback pass', (t, done) => {
setImmediate(done);
});
it('callback fail', (done) => {
it('callback fail', (t, done) => {
setImmediate(() => {
done(new Error('callback failure'));
});
});
it('sync t is this in test', function() {
assert.deepStrictEqual(this, { signal: this.signal, name: this.name });
it('sync t is this in test', function(t) {
assert.strictEqual(this, t);
});
it('async t is this in test', async function() {
assert.deepStrictEqual(this, { signal: this.signal, name: this.name });
it('async t is this in test', async function(t) {
assert.strictEqual(this, t);
});
it('callback t is this in test', function(done) {
assert.deepStrictEqual(this, { signal: this.signal, name: this.name });
it('callback t is this in test', function(t, done) {
assert.strictEqual(this, t);
done();
});
it('callback also returns a Promise', async (done) => {
it('callback also returns a Promise', async (t, done) => {
throw new Error('thrown from callback also returns a Promise');
});
it('callback throw', (done) => {
it('callback throw', (t, done) => {
throw new Error('thrown from callback throw');
});
it('callback called twice', (done) => {
it('callback called twice', (t, done) => {
done();
done();
});
it('callback called twice in different ticks', (done) => {
it('callback called twice in different ticks', (t, done) => {
setImmediate(done);
done();
});
it('callback called twice in future tick', (done) => {
it('callback called twice in future tick', (t, done) => {
setImmediate(() => {
done();
done();
});
});
it('callback async throw', (done) => {
it('callback async throw', (t, done) => {
setImmediate(() => {
throw new Error('thrown from callback async throw');
});
});
it('callback async throw after done', (done) => {
it('callback async throw after done', (t, done) => {
setImmediate(() => {
throw new Error('thrown from callback async throw after done');
});
@ -316,7 +316,7 @@ describe('timeouts', () => {
});
});
it('timed out callback test', { timeout: 5 }, (done) => {
it('timed out callback test', { timeout: 5 }, (t, done) => {
setTimeout(done, 100);
});
@ -327,7 +327,7 @@ describe('timeouts', () => {
});
});
it('large timeout callback test is ok', { timeout: 30_000_000 }, (done) => {
it('large timeout callback test is ok', { timeout: 30_000_000 }, (t, done) => {
setTimeout(done, 10);
});
});

View File

@ -104,7 +104,7 @@ not ok 12 - async throw fail
*
...
# Subtest: async skip fail
not ok 13 - async skip fail
not ok 13 - async skip fail # SKIP
---
duration_ms: *
failureType: 'callbackAndPromisePresent'
@ -645,8 +645,8 @@ not ok 60 - invalid subtest fail
# Warning: Test "callback async throw after done" generated asynchronous activity after the test ended. This activity created the error "Error: thrown from callback async throw after done" and would have caused the test to fail, but instead triggered an uncaughtException event.
# tests 60
# pass 23
# fail 23
# fail 22
# cancelled 0
# skipped 9
# skipped 10
# todo 5
# duration_ms *