Skip to content

Commit 73c4cf1

Browse files
committed
test_runner: deprecate legacy mock.module options
Emit runtime deprecation warnings for defaultExport and namedExports when mock.module() is called with legacy option names. Switch output fixtures to options.exports to keep output snapshots stable and update the TypeScript coverage snapshot percentage. Refs: #58443
1 parent fa99ada commit 73c4cf1

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

lib/internal/test_runner/mock/mock.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ const kSupportedFormats = [
6161
'module',
6262
];
6363
let sharedModuleState;
64+
let warnedLegacyDefaultExport;
65+
let warnedLegacyNamedExports;
6466
const {
6567
hooks: mockHooks,
6668
mocks,
@@ -828,10 +830,12 @@ function normalizeModuleMockOptions(options) {
828830

829831
if ('namedExports' in options) {
830832
validateObject(options.namedExports, 'options.namedExports');
833+
emitLegacyMockOptionWarning('namedExports');
831834
copyOwnProperties(options.namedExports, moduleExports);
832835
}
833836

834837
if ('defaultExport' in options) {
838+
emitLegacyMockOptionWarning('defaultExport');
835839
moduleExports.default = options.defaultExport;
836840
}
837841

@@ -858,6 +862,33 @@ function normalizeModuleMockOptions(options) {
858862
};
859863
}
860864

865+
function emitLegacyMockOptionWarning(option) {
866+
switch (option) {
867+
case 'defaultExport':
868+
if (warnedLegacyDefaultExport === true) {
869+
return;
870+
}
871+
warnedLegacyDefaultExport = true;
872+
process.emitWarning(
873+
'mock.module(): options.defaultExport is deprecated. ' +
874+
'Use options.exports.default instead.',
875+
'DeprecationWarning',
876+
);
877+
break;
878+
case 'namedExports':
879+
if (warnedLegacyNamedExports === true) {
880+
return;
881+
}
882+
warnedLegacyNamedExports = true;
883+
process.emitWarning(
884+
'mock.module(): options.namedExports is deprecated. ' +
885+
'Use options.exports instead.',
886+
'DeprecationWarning',
887+
);
888+
break;
889+
}
890+
}
891+
861892
function copyOwnProperties(from, to) {
862893
const keys = ObjectKeys(from);
863894

test/fixtures/test-runner/output/coverage-with-mock.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, it, mock } from 'node:test';
22

33
describe('module test with mock', async () => {
44
mock.module('../coverage-with-mock/sum.js', {
5-
namedExports: {
5+
exports: {
66
sum: (a, b) => 1,
77
getData: () => ({}),
88
},

test/fixtures/test-runner/output/typescript-coverage.mts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ describe('foo', { concurrency: true }, () => {
1010
.then(({ default: _, ...rest }) => rest);
1111

1212
mock.module('../coverage/bar.mts', {
13-
defaultExport: barMock,
14-
namedExports: barNamedExports,
13+
exports: {
14+
...barNamedExports,
15+
default: barMock,
16+
},
1517
});
1618

1719
({ foo } = await import('../coverage/foo.mts'));

test/fixtures/test-runner/output/typescript-coverage.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ ok 1 - foo
3434
# output | | | |
3535
# typescript-coverage.mts | 100.00 | 100.00 | 100.00 |
3636
# ----------------------------------------------------------------------------
37-
# all files | 93.55 | 100.00 | 85.71 |
37+
# all files | 93.94 | 100.00 | 85.71 |
3838
# ----------------------------------------------------------------------------
3939
# end of coverage report

0 commit comments

Comments
 (0)