Files
node/test/parallel/test-runner-option-precedence.js
Christopher Hiller cd78d08026 doc: formalize fn/name as part of TestOptions API
`TestOptions` as provided to `node:test`'s `test`/`it` supports both
`name` and `fn` as options per its implementation.

I'd like to formalize this as part of the public, documented API.

### Motivation

I have a use-case for consuming both fields.  I'd like to be able to
return the result of a function to `test`/`it` without needing to spread
 the parameters; e.g.:

```js
const testOptionsFactory = (opts = {}) => {
  return {
    fn: () => { /* .. */ },
    name: opts.name
  };
};

test(testOptionsFactory({name: 'foo'}));
```

If I cannot rely on this behavior, then I would need to instead return
an array of parameters and spread them:

```js
const testParamsFactory = (opts = {}) => {
  return opts.name !== undefined
    ? [opts.name, () => { /* .. */ }] : [() => { /* .. */ }];
};

test(...testParamsFactory({name: 'foo'}));
```

I don't think it's too terribly controversial that the former is more
ergonomic than the latter.

### Next Steps

Once this lands, I plan to propose the addition of these fields to
`@types/node`. Since the fields are not currently publicly documented, I
can't justify such a change.

Signed-off-by: Christopher Hiller <boneskull@boneskull.com>
PR-URL: https://github.com/nodejs/node/pull/64946
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruy Adorno <ruy@vlt.sh>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
2026-08-04 01:45:07 +00:00

42 lines
743 B
JavaScript

'use strict';
require('../common');
const { test, suite } = require('node:test');
suite('test runner option precedence', () => {
test(
'overridden test name',
{ name: 'options.name overrides test name', plan: 1 },
(t) => {
t.assert.strictEqual(t.name, 'options.name overrides test name');
},
);
test(
'options.fn overrides test function',
{
fn: (t) => {
t.assert.ok(true);
},
plan: 1,
},
(t) => {
t.assert.fail('should not be called');
},
);
test('options.fn only', {
plan: 1,
fn: (t) => {
t.assert.ok(true);
},
});
test({
name: 'single parameter options',
plan: 1,
fn: (t) => {
t.assert.ok(true);
},
});
});