test_runner: use module.registerHooks in module mocks · nodejs/node@3d89331 · GitHub
Skip to content

Commit 3d89331

Browse files
joyeecheungaduh95
authored andcommitted
test_runner: use module.registerHooks in module mocks
Migrate away from module.register(). This no longer needs to deal with the worker synchronization. PR-URL: #60326 Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent 063fbd8 commit 3d89331

5 files changed

Lines changed: 54 additions & 190 deletions

File tree

lib/internal/test_runner/coverage.js

Lines changed: 1 addition & 1 deletion

lib/internal/test_runner/mock/loader.js

Lines changed: 20 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,24 @@
11
'use strict';
22
const {
3-
AtomicsNotify,
4-
AtomicsStore,
53
JSONStringify,
64
SafeMap,
75
} = primordials;
8-
const {
9-
kBadExportsMessage,
10-
kMockSearchParam,
11-
kMockSuccess,
12-
kMockExists,
13-
kMockUnknownMessage,
14-
} = require('internal/test_runner/mock/mock');
6+
7+
const kMockSearchParam = 'node-test-mock';
8+
const kBadExportsMessage = 'Cannot create mock because named exports ' +
9+
'cannot be applied to the provided default export.';
10+
1511
const { URL, URLParse } = require('internal/url');
1612
let debug = require('internal/util/debuglog').debuglog('test_runner', (fn) => {
1713
debug = fn;
1814
});
1915

20-
// TODO(cjihrig): The mocks need to be thread aware because the exports are
21-
// evaluated on the thread that creates the mock. Before marking this API as
22-
// stable, one of the following issues needs to be implemented:
23-
// https://github.com/nodejs/node/issues/49472
24-
// or https://github.com/nodejs/node/issues/52219
25-
2616
const mocks = new SafeMap();
2717

28-
async function initialize(data) {
29-
data?.port.on('message', ({ type, payload }) => {
30-
debug('mock loader received message type "%s" with payload %o', type, payload);
31-
32-
if (type === 'node:test:register') {
33-
const { baseURL } = payload;
34-
const mock = mocks.get(baseURL);
35-
36-
if (mock?.active) {
37-
debug('already mocking "%s"', baseURL);
38-
sendAck(payload.ack, kMockExists);
39-
return;
40-
}
41-
42-
const localVersion = mock?.localVersion ?? 0;
43-
44-
debug('new mock version %d for "%s"', localVersion, baseURL);
45-
mocks.set(baseURL, {
46-
__proto__: null,
47-
active: true,
48-
cache: payload.cache,
49-
exportNames: payload.exportNames,
50-
format: payload.format,
51-
hasDefaultExport: payload.hasDefaultExport,
52-
localVersion,
53-
url: baseURL,
54-
});
55-
sendAck(payload.ack);
56-
} else if (type === 'node:test:unregister') {
57-
const mock = mocks.get(payload.baseURL);
58-
59-
if (mock !== undefined) {
60-
mock.active = false;
61-
mock.localVersion++;
62-
}
63-
64-
sendAck(payload.ack);
65-
} else {
66-
sendAck(payload.ack, kMockUnknownMessage);
67-
}
68-
});
69-
}
70-
71-
async function resolve(specifier, context, nextResolve) {
18+
function resolve(specifier, context, nextResolve) {
7219
debug('resolve hook entry, specifier = "%s", context = %o', specifier, context);
7320

74-
const nextResolveResult = await nextResolve(specifier, context);
21+
const nextResolveResult = nextResolve(specifier, context);
7522
const mockSpecifier = nextResolveResult.url;
7623

7724
const mock = mocks.get(mockSpecifier);
@@ -95,7 +42,7 @@ async function resolve(specifier, context, nextResolve) {
9542
return { __proto__: null, url: href, format: nextResolveResult.format };
9643
}
9744

98-
async function load(url, context, nextLoad) {
45+
function load(url, context, nextLoad) {
9946
debug('load hook entry, url = "%s", context = %o', url, context);
10047
const parsedURL = URLParse(url);
10148
if (parsedURL) {
@@ -105,7 +52,7 @@ async function load(url, context, nextLoad) {
10552
const baseURL = parsedURL ? parsedURL.href : url;
10653
const mock = mocks.get(baseURL);
10754

108-
const original = await nextLoad(url, context);
55+
const original = nextLoad(url, context);
10956
debug('load hook, mock = %o', mock);
11057
if (mock?.active !== true) {
11158
return original;
@@ -130,14 +77,14 @@ async function load(url, context, nextLoad) {
13077
__proto__: null,
13178
format,
13279
shortCircuit: true,
133-
source: await createSourceFromMock(mock, format),
80+
source: createSourceFromMock(mock, format),
13481
};
13582

13683
debug('load hook finished, result = %o', result);
13784
return result;
13885
}
13986

140-
async function createSourceFromMock(mock, format) {
87+
function createSourceFromMock(mock, format) {
14188
// Create mock implementation from provided exports.
14289
const { exportNames, hasDefaultExport, url } = mock;
14390
const useESM = format === 'module' || format === 'module-typescript';
@@ -196,9 +143,12 @@ if (module.exports === null || typeof module.exports !== 'object') {
196143
return source;
197144
}
198145

199-
function sendAck(buf, status = kMockSuccess) {
200-
AtomicsStore(buf, 0, status);
201-
AtomicsNotify(buf, 0);
202-
}
203-
204-
module.exports = { initialize, load, resolve };
146+
module.exports = {
147+
hooks: { __proto__: null, load, resolve },
148+
mocks,
149+
constants: {
150+
__proto__: null,
151+
kBadExportsMessage,
152+
kMockSearchParam,
153+
},
154+
};

lib/internal/test_runner/mock/mock.js

Lines changed: 33 additions & 59 deletions

test/parallel/test-permission-dc-worker-threads.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)