Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
fix: use command-options.cwd to load jest config
Browse files Browse the repository at this point in the history
We should use that configuration instead of the cwd of the current process. That enables to spawn jest commands from another directory than the `rootDir` of jest.
  • Loading branch information
lachrist committed Mar 10, 2023
1 parent e4a287a commit 8b8a70c
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ rules:
# complexity
# computed-property-spacing: [error, never] >> Prettier
consistent-return: error
consistent-this: [error, self]
consistent-this: [error, that]
# constructor-super: error >> Class are disabled by no-restricted-syntax
curly: [error, all]
# default-case >> SwitchStatement disabled by no-restricted-syntax
Expand Down
6 changes: 3 additions & 3 deletions components/command/node/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export const compileConfigurationCommandAsync = async (configuration, env) => {
const {
"recursive-process-recording": recursive,
recorder,
agent: { directory },
agent: { directory: self },
command: { tokens },
"command-options": options,
} = configuration;
Expand All @@ -125,13 +125,13 @@ export const compileConfigurationCommandAsync = async (configuration, env) => {
(recorder_recursive === null || recorder_recursive === recursive) &&
name === recorder,
);
const [exec, ...argv] = await hookCommandAsync(tokens, directory);
const [exec, ...argv] = await hookCommandAsync(tokens, self, options.cwd);
return {
exec,
argv,
options: {
...options,
env: hookEnvironment(env, directory),
env: hookEnvironment(env, self, options.cwd),
},
};
};
21 changes: 7 additions & 14 deletions components/command/node/jest.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { cwd } from "node:process";
import { coalesce } from "../../util/index.mjs";
import {
convertFileUrlToPath,
convertPathToFileUrl,
} from "../../path/index.mjs";
import { toAbsoluteUrl, toDirectoryUrl } from "../../url/index.mjs";
import { convertFileUrlToPath } from "../../path/index.mjs";
import { toAbsoluteUrl } from "../../url/index.mjs";
import { escapeNodeOption } from "./escape.mjs";
import { sniffTokens, splitTokens } from "./package.mjs";
import { hookJestArgvAsync } from "./jest-argv.mjs";
Expand All @@ -14,26 +10,23 @@ export const recursive = null;

export const doesSupport = (tokens) => sniffTokens(tokens, "jest");

export const hookCommandAsync = async (tokens, base) => {
export const hookCommandAsync = async (tokens, self, base) => {
const { exec, argv } = splitTokens(tokens);
return [
...exec,
...(await hookJestArgvAsync(
argv,
toDirectoryUrl(convertPathToFileUrl(cwd())),
)),
...(await hookJestArgvAsync(argv, base)),
"--setupFilesAfterEnv",
convertFileUrlToPath(toAbsoluteUrl("lib/node/recorder.mjs", base)),
convertFileUrlToPath(toAbsoluteUrl("lib/node/recorder.mjs", self)),
];
};

export const hookEnvironment = (env, base) => ({
export const hookEnvironment = (env, self, _base) => ({
...env,
NODE_OPTIONS: `${coalesce(
env,
"NODE_OPTIONS",
"",
)} --experimental-vm-modules --experimental-loader=${escapeNodeOption(
toAbsoluteUrl("lib/node/loader-esm.mjs", base),
toAbsoluteUrl("lib/node/loader-esm.mjs", self),
)}`,
});
9 changes: 5 additions & 4 deletions components/command/node/jest.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ const {
JSON: { stringify: stringifyJSON },
} = globalThis;

const self = "file:///A:/self/";
const base = "file:///A:/base/";
const recorder_path = convertFileUrlToPath(
"file:///A:/base/lib/node/recorder.mjs",
"file:///A:/self/lib/node/recorder.mjs",
);
const transformer_path = convertFileUrlToPath(
toAbsoluteUrl("lib/node/transformer-jest.mjs", self_directory),
);
const loader_url = "file:///A:/base/lib/node/loader-esm.mjs";
const loader_url = "file:///A:/self/lib/node/loader-esm.mjs";

//////////////////
// mocha --argv //
//////////////////

assertEqual(doesSupport(["jest", "--argv"]), true);

assertDeepEqual(await hookCommandAsync(["jest", "--argv"], base), [
assertDeepEqual(await hookCommandAsync(["jest", "--argv"], self, base), [
"jest",
"--argv",
"--transform",
Expand All @@ -44,7 +45,7 @@ assertDeepEqual(await hookCommandAsync(["jest", "--argv"], base), [
/////////////////////

assertDeepEqual(
hookEnvironment({ FOO: "bar", NODE_OPTIONS: "options" }, base),
hookEnvironment({ FOO: "bar", NODE_OPTIONS: "options" }, self, base),
{
FOO: "bar",
NODE_OPTIONS: `options --experimental-vm-modules --experimental-loader=${loader_url}`,
Expand Down
8 changes: 4 additions & 4 deletions components/command/node/mocha.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ export const recursive = null;

export const doesSupport = (tokens) => sniffTokens(tokens, "mocha");

export const hookCommandAsync = (tokens, base) => {
export const hookCommandAsync = (tokens, self, _base) => {
const { exec, argv } = splitTokens(tokens);
return [
...exec,
"--require",
convertFileUrlToPath(toAbsoluteUrl("lib/node/mocha-hook.mjs", base)),
convertFileUrlToPath(toAbsoluteUrl("lib/node/mocha-hook.mjs", self)),
...argv,
];
};

export const hookEnvironment = (env, base) => ({
export const hookEnvironment = (env, self, _base) => ({
...env,
NODE_OPTIONS: `${coalesce(
env,
"NODE_OPTIONS",
"",
)} --experimental-loader=${escapeNodeOption(
toAbsoluteUrl("lib/node/recorder.mjs", base),
toAbsoluteUrl("lib/node/recorder.mjs", self),
)}`,
});
9 changes: 5 additions & 4 deletions components/command/node/mocha.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import { assertDeepEqual, assertEqual } from "../../__fixture__.mjs";
import { fileURLToPath } from "node:url";
import { doesSupport, hookCommandAsync, hookEnvironment } from "./mocha.mjs";

const self = "file:///A:/self/";
const base = "file:///A:/base/";
const hook_path = fileURLToPath("file:///A:/base/lib/node/mocha-hook.mjs");
const recorder_url = "file:///A:/base/lib/node/recorder.mjs";
const hook_path = fileURLToPath("file:///A:/self/lib/node/mocha-hook.mjs");
const recorder_url = "file:///A:/self/lib/node/recorder.mjs";

//////////////////
// mocha --argv //
//////////////////

assertEqual(doesSupport(["mocha", "--argv"]), true);
assertDeepEqual(await hookCommandAsync(["mocha", "--argv"], base), [
assertDeepEqual(await hookCommandAsync(["mocha", "--argv"], self, base), [
"mocha",
"--require",
hook_path,
Expand All @@ -23,7 +24,7 @@ assertDeepEqual(await hookCommandAsync(["mocha", "--argv"], base), [
/////////////////////

assertDeepEqual(
hookEnvironment({ FOO: "bar", NODE_OPTIONS: "options" }, base),
hookEnvironment({ FOO: "bar", NODE_OPTIONS: "options" }, self, base),
{
FOO: "bar",
NODE_OPTIONS: `options --experimental-loader=${recorder_url}`,
Expand Down
6 changes: 3 additions & 3 deletions components/command/node/node-recursive.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ export const generateNodeRecorder = (recorder) => ({
name: recorder,
recursive: true,
doesSupport,
hookCommandAsync: (tokens, _base) => tokens,
hookEnvironment: (env, base) => ({
hookCommandAsync: (tokens, _self, _base) => tokens,
hookEnvironment: (env, self, _base) => ({
...env,
NODE_OPTIONS: `${coalesce(
env,
"NODE_OPTIONS",
"",
)} --experimental-loader=${escapeNodeOption(
toAbsoluteUrl(`lib/node/recorder.mjs`, base),
toAbsoluteUrl(`lib/node/recorder.mjs`, self),
)}`,
}),
});
7 changes: 4 additions & 3 deletions components/command/node/node-recursive.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ const { name, recursive, doesSupport, hookCommandAsync, hookEnvironment } =
generateNodeRecorder("process");

const base = "file:///A:/base/";
const recorder_url = "file:///A:/base/lib/node/recorder.mjs";
const self = "file:///A:/self/";
const recorder_url = "file:///A:/self/lib/node/recorder.mjs";

assertEqual(name, "process");

assertEqual(recursive, true);

assertEqual(doesSupport(["token"]), true);

assertDeepEqual(await hookCommandAsync(["token"], base), ["token"]);
assertDeepEqual(await hookCommandAsync(["token"], self, base), ["token"]);

assertDeepEqual(
hookEnvironment({ FOO: "bar", NODE_OPTIONS: "options" }, base),
hookEnvironment({ FOO: "bar", NODE_OPTIONS: "options" }, self, base),
{
FOO: "bar",
NODE_OPTIONS: `options --experimental-loader=${recorder_url}`,
Expand Down
6 changes: 3 additions & 3 deletions components/command/node/node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export const generateNodeRecorder = (recorder) => ({
doesSupport,
recursive: false,
name: recorder,
hookCommandAsync: (tokens, base) => {
hookCommandAsync: (tokens, self, _base) => {
const { exec, argv } = splitNodeCommand(tokens);
return [
...exec,
"--experimental-loader",
toAbsoluteUrl(`lib/node/recorder.mjs`, base),
toAbsoluteUrl(`lib/node/recorder.mjs`, self),
...argv,
];
},
hookEnvironment: (env, _base) => env,
hookEnvironment: (env, _self, _base) => env,
});
7 changes: 4 additions & 3 deletions components/command/node/node.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import { generateNodeRecorder } from "./node.mjs";
const { name, recursive, doesSupport, hookCommandAsync, hookEnvironment } =
generateNodeRecorder("process");

const self = "file:///A:/self/";
const base = "file:///A:/base/";
const recorder_url = "file:///A:/base/lib/node/recorder.mjs";
const recorder_url = "file:///A:/self/lib/node/recorder.mjs";

assertEqual(name, "process");
assertEqual(recursive, false);

assertEqual(doesSupport(["node.ext", "main.mjs"]), true);
assertDeepEqual(await hookCommandAsync(["node.ext", "main.mjs"], base), [
assertDeepEqual(await hookCommandAsync(["node.ext", "main.mjs"], self, base), [
"node.ext",
"--experimental-loader",
recorder_url,
"main.mjs",
]);

assertDeepEqual(hookEnvironment({ FOO: "bar" }, base), { FOO: "bar" });
assertDeepEqual(hookEnvironment({ FOO: "bar" }, self, base), { FOO: "bar" });

0 comments on commit 8b8a70c

Please sign in to comment.