Skip to content

Commit

Permalink
Add setupFilesAfterEnv; run setupFiles before the env setup (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo authored Apr 26, 2022
1 parent 90488cd commit 4caa3d3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 34 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ on:
jobs:
prettier:
name: Prettier
strategy:
matrix:
additional-options: ["", "--runInBand"]
runs-on: ubuntu-latest
steps:
- name: Setup Node.js
Expand All @@ -31,7 +34,7 @@ jobs:
yarn config set ignore-engines true
yarn install --frozen-lockfile
yarn upgrade jest-light-runner@file:../jest-light-runner
yarn test:format
yarn test:format ${{ matrix.additional-options }}
working-directory: prettier

babel:
Expand Down Expand Up @@ -66,6 +69,11 @@ jobs:
yarn up jest-light-runner@file:../jest-light-runner
working-directory: babel

- name: Disable Babel's setupFilesAfterEnv
run: |
echo "" > test/testSetupFile.js
working-directory: babel

- name: Test
run: |
yarn jest babel-core babel-preset-env ${{ matrix.additional-options }}
Expand Down
86 changes: 53 additions & 33 deletions src/worker-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,56 @@ import * as circus from "jest-circus";
import { inspect } from "util";
import { isWorkerThread } from "piscina";

import "./global-setup.js";

/** @typedef {{ failures: number, passes: number, pending: number, start: number, end: number }} Stats */
/** @typedef {{ ancestors: string[], title: string, duration: number, errors: Error[], skipped: boolean }} InternalTestResult */

// 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);
};
}

export default async function ({
test,
updateSnapshot,
testNamePattern,
port,
}) {
port.postMessage("start");

const { setupFiles, snapshotSerializers, snapshotFormat } =
test.context.config;
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);
};
}

// https://github.com/facebook/jest/issues/11038
for (const setupFile of setupFiles) {
for (const setupFile of projectConfig.setupFiles) {
const { default: setup } = await import(pathToFileURL(setupFile));

if (typeof setup === "function") {
await setup();
}
// https://github.com/facebook/jest/issues/11038
if (typeof setup === "function") await setup();
}

for (const snapshotSerializer of [...snapshotSerializers].reverse()) {
await import("./global-setup.js");

for (const snapshotSerializer of projectConfig.snapshotSerializers
.slice()
.reverse()) {
const { default: serializer } = await import(
pathToFileURL(snapshotSerializer)
);
snapshot.addSerializer(serializer);
}

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,
updateSnapshot,
testNamePattern,
port,
}) {
await initialSetup(test.context.config);

port.postMessage("start");

const testNamePatternRE =
testNamePattern != null ? new RegExp(testNamePattern, "i") : null;

Expand All @@ -64,7 +69,11 @@ export default async function ({

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

Expand Down Expand Up @@ -278,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 4caa3d3

Please sign in to comment.