Skip to content

Commit

Permalink
Fix node 12 support
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Apr 26, 2022
1 parent d4055bf commit dd6e80d
Showing 1 changed file with 48 additions and 38 deletions.
86 changes: 48 additions & 38 deletions src/worker-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,42 @@ import { isWorkerThread } from "piscina";
/** @typedef {{ failures: number, passes: number, pending: number, start: number, end: number }} Stats */
/** @typedef {{ ancestors: string[], title: string, duration: number, errors: Error[], skipped: boolean }} InternalTestResult */

let initialSetupP;
function initialSetup(projectConfig) {
initialSetupP ||= (async () => {
// Node.js workers (worker_threads) don't support
// process.chdir, that we use multiple times in our tests.
// We can "polyfill" it for process.cwd() usage, but it
// won't affect path.* and fs.* functions.
if (isWorkerThread) {
const startCwd = process.cwd();
let cwd = startCwd;
process.cwd = () => cwd;
process.chdir = dir => {
cwd = path.resolve(cwd, dir);
};
}

for (const setupFile of projectConfig.setupFiles) {
const { default: setup } = await import(pathToFileURL(setupFile));
// https://github.com/facebook/jest/issues/11038
if (typeof setup === "function") await setup();
}
const initialSetup = once(async (projectConfig) => {
// Node.js workers (worker_threads) don't support
// process.chdir, that we use multiple times in our tests.
// We can "polyfill" it for process.cwd() usage, but it
// won't affect path.* and fs.* functions.
if (isWorkerThread) {
const startCwd = process.cwd();
let cwd = startCwd;
process.cwd = () => cwd;
process.chdir = dir => {
cwd = path.resolve(cwd, dir);
};
}

await import("./global-setup.js");
for (const setupFile of projectConfig.setupFiles) {
const { default: setup } = await import(pathToFileURL(setupFile));
// https://github.com/facebook/jest/issues/11038
if (typeof setup === "function") await setup();
}

for (const snapshotSerializer of projectConfig.snapshotSerializers
.slice()
.reverse()) {
const { default: serializer } = await import(
pathToFileURL(snapshotSerializer)
);
snapshot.addSerializer(serializer);
}
await import("./global-setup.js");

for (const setupFile of projectConfig.setupFilesAfterEnv) {
const { default: setup } = await import(pathToFileURL(setupFile));
if (typeof setup === "function") await setup();
}
})();
for (const snapshotSerializer of projectConfig.snapshotSerializers
.slice()
.reverse()) {
const { default: serializer } = await import(
pathToFileURL(snapshotSerializer)
);
snapshot.addSerializer(serializer);
}

return initialSetupP;
}
for (const setupFile of projectConfig.setupFilesAfterEnv) {
const { default: setup } = await import(pathToFileURL(setupFile));
if (typeof setup === "function") await setup();
}
});

export default async function run({
test,
Expand All @@ -74,7 +69,11 @@ export default async function run({

const snapshotState = new snapshot.SnapshotState(
`${path.dirname(test.path)}/__snapshots__/${path.basename(test.path)}.snap`,
{ prettierPath: "prettier", updateSnapshot, snapshotFormat: test.context.config.snapshotFormat }
{
prettierPath: "prettier",
updateSnapshot,
snapshotFormat: test.context.config.snapshotFormat,
}
);
expect.setState({ snapshotState });

Expand Down Expand Up @@ -288,3 +287,14 @@ function failureToString(test) {
"\n"
);
}

function once(fn) {
let called = false;
let result;
return function () {
if (called) return result;
called = true;
result = fn.apply(this, arguments);
return result;
};
}

0 comments on commit dd6e80d

Please sign in to comment.