Skip to content

Commit

Permalink
fix or skip tests that are not fit for running on worker threads
Browse files Browse the repository at this point in the history
  • Loading branch information
dygabo committed May 7, 2024
1 parent c014520 commit e8708bd
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 22 deletions.
2 changes: 2 additions & 0 deletions test/common/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const {
skipIfDumbTerminal,
skipIfEslintMissing,
skipIfInspectorDisabled,
skipIfWorker,
spawnPromisified,
} = common;

Expand Down Expand Up @@ -104,5 +105,6 @@ export {
skipIfDumbTerminal,
skipIfEslintMissing,
skipIfInspectorDisabled,
skipIfWorker,
spawnPromisified,
};
7 changes: 6 additions & 1 deletion test/es-module/test-esm-loader-mock.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import '../common/index.mjs';
import { skipIfWorker } from '../common/index.mjs';
import assert from 'node:assert/strict';
import { mock } from '../fixtures/es-module-loaders/mock.mjs';
// Importing mock.mjs above will call `register` to modify the loaders chain.
// Modifying the loader chain is not supported currently when running from a worker thread.
// Relevant PR: https://github.com/nodejs/node/pull/52706
// See comment: https://github.com/nodejs/node/pull/52706/files#r1585144580
skipIfWorker();

mock('node:events', {
EventEmitter: 'This is mocked!'
Expand Down
4 changes: 3 additions & 1 deletion test/es-module/test-esm-named-exports.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Flags: --import ./test/fixtures/es-module-loaders/builtin-named-exports.mjs
'use strict';

require('../common');
const common = require('../common');
common.skipIfWorker();

const { readFile, __fromLoader } = require('fs');
const assert = require('assert');

Expand Down
9 changes: 5 additions & 4 deletions test/es-module/test-esm-named-exports.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Flags: --import ./test/fixtures/es-module-loaders/builtin-named-exports.mjs
import '../common/index.mjs';
import { readFile, __fromLoader } from 'fs';
import { skipIfWorker } from '../common/index.mjs';
import * as fs from 'fs';
import assert from 'assert';
import ok from '../fixtures/es-modules/test-esm-ok.mjs';
skipIfWorker();

assert(ok);
assert(readFile);
assert(__fromLoader);
assert(fs.readFile);
assert(fs.__fromLoader);
3 changes: 2 additions & 1 deletion test/es-module/test-esm-virtual-json.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import '../common/index.mjs';
import { skipIfWorker } from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import { register } from 'node:module';
import assert from 'node:assert';
skipIfWorker();

async function resolve(referrer, context, next) {
const result = await next(referrer, context);
Expand Down
13 changes: 8 additions & 5 deletions test/fixtures/es-module-loaders/builtin-named-exports.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isMainThread } from '../../common/index.mjs';
import * as fixtures from '../../common/fixtures.mjs';
import { createRequire, register } from 'node:module';

Expand All @@ -10,8 +11,10 @@ Object.defineProperty(globalThis, GET_BUILTIN, {
configurable: false,
});

register(fixtures.fileURL('es-module-loaders/builtin-named-exports-loader.mjs'), {
data: {
GET_BUILTIN,
},
});
if (isMainThread) {
register(fixtures.fileURL('es-module-loaders/builtin-named-exports-loader.mjs'), {
data: {
GET_BUILTIN,
},
});
}
17 changes: 7 additions & 10 deletions test/fixtures/es-module-loaders/not-found-assert-loader.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import assert from 'node:assert';

// A loader that asserts that the defaultResolve will throw "not found"
// (skipping the top-level main of course, and the built-in ones needed for run-worker).
let mainLoad = true;
export async function resolve(specifier, { importAttributes }, next) {
if (mainLoad || specifier === 'path' || specifier === 'worker_threads') {
mainLoad = false;
return next(specifier);
if (specifier.startsWith('./not-found')) {
await assert.rejects(next(specifier), { code: 'ERR_MODULE_NOT_FOUND' });
return {
url: 'node:fs',
importAttributes,
};
}
await assert.rejects(next(specifier), { code: 'ERR_MODULE_NOT_FOUND' });
return {
url: 'node:fs',
importAttributes,
};
return next(specifier);
}

0 comments on commit e8708bd

Please sign in to comment.