Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions workspaces/adventure-pack/jest.config.js

This file was deleted.

44 changes: 44 additions & 0 deletions workspaces/adventure-pack/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Config } from "jest";

const config: Config = {
preset: "ts-jest",
testEnvironment: "node",
};

export default config;

// Hack to make this config runnable as a script, since the combination of
// Jest, ESM, and a TypeScript config has been painful to get working otherwise.
import("node:process").then(async ({ default: process }) => {
if (import.meta.filename !== process.argv[1]) {
return;
}

try {
const { spawnWithSafeStdio } = await import(
"@code-chronicles/util/spawnWithSafeStdio"
);
await spawnWithSafeStdio(
"jest",
["--color", "-c", JSON.stringify(config), ...process.argv.slice(2)],
{
stdio: "inherit",
env: {
...process.env,
NODE_OPTIONS: [
"--experimental-vm-modules",
process.env.NODE_OPTIONS?.trim(),
]
.filter(Boolean)
.join(" "),
},
},
);
} catch (err) {
console.error(
(err as Record<string, unknown> | null | undefined)?.message ?? err,
);
// eslint-disable-next-line require-atomic-updates -- Updating `process.exitCode` on error is logical.
process.exitCode = 1;
}
});
4 changes: 2 additions & 2 deletions workspaces/adventure-pack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
"goodies:python3:test": "bash goodies/python3/test.sh",
"goodies:typescript:format": "prettier --color --write goodies/typescript",
"goodies:typescript:install": "yarn",
"goodies:typescript:test": "jest --color \"/goodies/typescript/\"",
"goodies:typescript:test": "tsx ./jest.config.ts \"/goodies/typescript/\"",
"build-app": "tsx src/scripts/build/main.ts",
"build-chrome-extension": "tsx src/scripts/build/buildChromeExtension.ts",
"package-goodies:test": "cross-env NODE_OPTIONS=\"--experimental-vm-modules\" jest --color --testPathIgnorePatterns=\"<rootDir>/goodies/\"",
"package-goodies:test": "tsx ./jest.config.ts --testPathIgnorePatterns=\"<rootDir>/goodies/\"",
"format": "yarn goodies:java:format && yarn goodies:kotlin:format && yarn goodies:python3:format && yarn goodies:typescript:format && prettier --color --write .",
"lint": "eslint --color --max-warnings=0 .",
"postinstall": "yarn goodies:java:install && yarn goodies:kotlin:install && yarn goodies:python3:install",
Expand Down
44 changes: 44 additions & 0 deletions workspaces/util/jest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Config } from "jest";

const config: Config = {
preset: "ts-jest",
testEnvironment: "node",
};

export default config;

// Hack to make this config runnable as a script, since the combination of
// Jest, ESM, and a TypeScript config has been painful to get working otherwise.
import("node:process").then(async ({ default: process }) => {
if (import.meta.filename !== process.argv[1]) {
return;
}

try {
const { spawnWithSafeStdio } = await import(
"@code-chronicles/util/spawnWithSafeStdio"
);
await spawnWithSafeStdio(
"jest",
["--color", "-c", JSON.stringify(config), ...process.argv.slice(2)],
{
stdio: "inherit",
env: {
...process.env,
NODE_OPTIONS: [
"--experimental-vm-modules",
process.env.NODE_OPTIONS?.trim(),
]
.filter(Boolean)
.join(" "),
},
},
);
} catch (err) {
console.error(
(err as Record<string, unknown> | null | undefined)?.message ?? err,
);
// eslint-disable-next-line require-atomic-updates -- Updating `process.exitCode` on error is logical.
process.exitCode = 1;
}
});
8 changes: 0 additions & 8 deletions workspaces/util/jest.config.ts

This file was deleted.

4 changes: 2 additions & 2 deletions workspaces/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"scripts": {
"format": "prettier --color --write .",
"lint": "eslint --color --max-warnings=0 .",
"test": "jest --color .",
"test": "tsx ./jest.config.mts",
"typecheck": "tsc --pretty --project ."
},
"dependencies": {
Expand All @@ -35,7 +35,7 @@
"jest": "29.7.0",
"prettier": "3.3.3",
"ts-jest": "29.2.5",
"ts-node": "10.9.2",
"tsx": "4.19.1",
"type-fest": "4.26.1",
"typescript": "5.6.2"
}
Expand Down
16 changes: 9 additions & 7 deletions workspaces/util/src/spawnWithSafeStdio.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import process from "node:process";
import { spawn, type SpawnOptions } from "node:child_process";

// TODO: audit callsites since the behavior is changing

export function spawnWithSafeStdio(
command: string,
args: readonly string[],
options?: Omit<SpawnOptions, "stdio">,
options?: SpawnOptions,
): Promise<void> {
return new Promise((resolve, reject) => {
const childProcess = spawn(command, args, {
...options,
// When using "inherit" mode, it sometimes broke the interaction with
// the actions/github-script@v7 GitHub Action, so we default to piping.
stdio: ["ignore", "pipe", "pipe"],

// Without a shell specified, many comands seem to fail to spawn on
// Windows. I verified that it's not a PATH issue. It seems like it's
// probably https://github.com/nodejs/node-v0.x-archive/issues/5841
...(process.platform === "win32" && { shell: options?.shell ?? "bash" }),

// When using "inherit" mode, it sometimes broke the interaction with
// the actions/github-script@v7 GitHub Action...
stdio: ["ignore", "pipe", "pipe"],
...options,
});

childProcess.stdout.pipe(process.stdout);
childProcess.stderr.pipe(process.stderr);
childProcess.stdout?.pipe(process.stdout);
childProcess.stderr?.pipe(process.stderr);

childProcess.on("error", reject);
childProcess.on("exit", (exitCode) => {
Expand Down
Loading