Skip to content

Commit e2020cf

Browse files
Executing test files in parallel, for ~5x better performance
1 parent 35b9684 commit e2020cf

3 files changed

Lines changed: 88 additions & 9 deletions

File tree

package-lock.json

Lines changed: 50 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"compile:watch": "tsc --outDir dist --watch",
2929
"format": "npm run compile && node dist/bin.js --write src test",
3030
"prepublishOnly": "npm run compile && npm run test",
31-
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --runInBand --rootDir test/__tests__",
31+
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --rootDir test/__tests__ --testTimeout 60000",
3232
"typecheck": "tsc --noEmit"
3333
},
3434
"peerDependencies": {
@@ -72,6 +72,7 @@
7272
"prettier": "3.3.3",
7373
"radix64-encoding": "^2.0.1",
7474
"tiny-dirname": "^1.0.1",
75-
"typescript": "^5.4.5"
75+
"typescript": "^5.4.5",
76+
"zeptoid": "^1.0.2"
7677
}
7778
}

test/utils.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import once from "function-once";
22
import * as Archive from "json-archive";
33
import exec from "nanoexec";
44
import fs from "node:fs/promises";
5+
import os from "node:os";
56
import path from "node:path";
67
import process from "node:process";
7-
import { setTimeout as delay } from "node:timers/promises";
88
import Base64 from "radix64-encoding";
9+
import zeptoid from "zeptoid";
910

1011
import { expect, test } from "@jest/globals";
1112
import serializerRaw from "jest-snapshot-serializer-raw";
@@ -17,12 +18,12 @@ expect.addSnapshotSerializer(serializerAnsi);
1718
const ROOT_PATH = process.cwd();
1819
const BIN_PATH = path.join(ROOT_PATH, "dist", "bin.js");
1920
const FIXTURES_PATH = path.join(ROOT_PATH, "test", "__fixtures__");
21+
const TESTS_PATH = path.join(ROOT_PATH, "test");
2022

2123
async function getArchive(folderPath) {
2224
const packPrev = await Archive.pack(folderPath);
2325
const archive = {
24-
getPack: once(async () => {
25-
await delay(100); // Giving a little time for the FS to settle
26+
getPack: once(() => {
2627
return Archive.pack(folderPath);
2728
}),
2829
getChanged: once(async () => {
@@ -65,6 +66,27 @@ function getFixturesPath(dir) {
6566
return path.join(FIXTURES_PATH, dir);
6667
}
6768

69+
async function getIsolatedFixtures(dir) {
70+
const [rootPart, ...subParts] = dir.split("/");
71+
const fixturesPath = getFixturesPath(rootPart);
72+
const tempPath = await getTempPath(`prettier-${rootPart}`);
73+
const tempGitPath = path.join(tempPath, ".git");
74+
const isolatedPath = path.join(tempPath, ...subParts);
75+
76+
await fs.mkdir(tempPath, { recursive: true });
77+
await fs.mkdir(tempGitPath, { recursive: true });
78+
await fs.cp(fixturesPath, tempPath, { recursive: true });
79+
80+
const dispose = () => {
81+
return fs.rm(tempPath, { recursive: true, force: true });
82+
};
83+
84+
return {
85+
path: isolatedPath,
86+
dispose,
87+
};
88+
}
89+
6890
function getNormalizedOutput(output, options) {
6991
// \r is trimmed from jest snapshots by default;
7092
// manually replacing this character with /*CR*/ to test its true presence
@@ -74,9 +96,16 @@ function getNormalizedOutput(output, options) {
7496
return output;
7597
}
7698

99+
async function getTempPath(prefix) {
100+
const rootPath = await fs.realpath(os.tmpdir());
101+
const tempPath = path.join(rootPath, `${prefix}-${zeptoid()}`);
102+
return tempPath;
103+
}
104+
77105
async function runCommand(dir, args, options) {
78-
const cwd = getFixturesPath(dir);
79-
const archive = dir ? await getArchive(cwd) : undefined;
106+
const fixtures = dir ? await getIsolatedFixtures(dir) : undefined;
107+
const archive = fixtures ? await getArchive(fixtures.path) : undefined;
108+
const cwd = fixtures ? fixtures.path : TESTS_PATH;
80109
const argsWithReplacements = args.map((arg) => arg.replaceAll("$CWD", cwd));
81110
const result = exec("node", [BIN_PATH, ...argsWithReplacements], { cwd, stdio: "pipe" });
82111

@@ -90,7 +119,7 @@ async function runCommand(dir, args, options) {
90119
const stderr = getNormalizedOutput((await result.stderr).toString());
91120
const write = (await archive?.getDiff()) || [];
92121

93-
await archive?.reset();
122+
await fixtures?.dispose();
94123

95124
return { status, stdout, stderr, write };
96125
}

0 commit comments

Comments
 (0)