From cda6e1fa89a777badc0ab0080e0481705d58d17f Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 5 Mar 2023 21:30:32 +0100 Subject: [PATCH 1/2] feat: support file URLs --- README.md | 2 +- lib/createJestRunner.ts | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9cb581c..c08cb89 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ module.exports = createJestRunner(require.resolve('./run')); #### createJestRunner(pathToRunFile, config?: { getExtraOptions }) -- `pathToRunFile`: path to your run file. +- `pathToRunFile`: path to your run file. This must be an absolute path or a `file://` URL. - `config`: Optional argument for configuring the runner. - `getExtraOptions`: `() => object` used for passing extra options to the runner. It needs to be a serializable object because it will be send to a different Node process. diff --git a/lib/createJestRunner.ts b/lib/createJestRunner.ts index 301e8db..708eb84 100644 --- a/lib/createJestRunner.ts +++ b/lib/createJestRunner.ts @@ -1,3 +1,5 @@ +import { isAbsolute } from 'path'; +import { fileURLToPath } from 'url'; import type { TestResult } from '@jest/test-result'; import type { CallbackTestRunnerInterface, @@ -30,12 +32,30 @@ class CancelRun extends Error { type TestRunner = (runTestOptions: RunTestOptions) => TestResult; +function getRunnerPath(runPath: string | URL): string { + let path = runPath; + + if (typeof path !== 'string') { + path = path.href; + } + + if (path.startsWith('file://')) { + path = fileURLToPath(path); + } else if (!isAbsolute(path)) { + throw new Error(`Path must be absolute - got ${path}`); + } + + return path; +} + export default function createRunner< ExtraOptions extends Record, >( - runPath: string, + runPathStringOrUrl: string | URL, { getExtraOptions }: CreateRunnerOptions = {}, ) { + const runPath = getRunnerPath(runPathStringOrUrl); + return class BaseTestRunner implements CallbackTestRunnerInterface { #globalConfig: Config.GlobalConfig; From 4063bd412dce81e472d3d0109d4bff4ac204ec99 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 6 Mar 2023 14:40:58 +0100 Subject: [PATCH 2/2] just update jest-worker --- lib/createJestRunner.ts | 24 ++---------------------- package.json | 2 +- yarn.lock | 30 +++++++++++++++--------------- 3 files changed, 18 insertions(+), 38 deletions(-) diff --git a/lib/createJestRunner.ts b/lib/createJestRunner.ts index 708eb84..38324fe 100644 --- a/lib/createJestRunner.ts +++ b/lib/createJestRunner.ts @@ -1,5 +1,3 @@ -import { isAbsolute } from 'path'; -import { fileURLToPath } from 'url'; import type { TestResult } from '@jest/test-result'; import type { CallbackTestRunnerInterface, @@ -32,30 +30,12 @@ class CancelRun extends Error { type TestRunner = (runTestOptions: RunTestOptions) => TestResult; -function getRunnerPath(runPath: string | URL): string { - let path = runPath; - - if (typeof path !== 'string') { - path = path.href; - } - - if (path.startsWith('file://')) { - path = fileURLToPath(path); - } else if (!isAbsolute(path)) { - throw new Error(`Path must be absolute - got ${path}`); - } - - return path; -} - export default function createRunner< ExtraOptions extends Record, >( - runPathStringOrUrl: string | URL, + runPath: string | URL, { getExtraOptions }: CreateRunnerOptions = {}, ) { - const runPath = getRunnerPath(runPathStringOrUrl); - return class BaseTestRunner implements CallbackTestRunnerInterface { #globalConfig: Config.GlobalConfig; @@ -98,7 +78,7 @@ export default function createRunner< onFailure: OnTestFailure, options: TestRunnerOptions, ): Promise { - const runner: TestRunner = (await import(runPath)).default; + const runner: TestRunner = (await import(runPath.toString())).default; const mutex = pLimit(1); return tests.reduce( diff --git a/package.json b/package.json index 7b5d96e..91849ea 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ }, "dependencies": { "chalk": "^4.1.0", - "jest-worker": "^29.0.0", + "jest-worker": "^29.5.0", "p-limit": "^3.1.0" }, "devDependencies": { diff --git a/yarn.lock b/yarn.lock index 31e59da..cfefb27 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1659,9 +1659,9 @@ __metadata: languageName: node linkType: hard -"@jest/types@npm:^29.4.3": - version: 29.4.3 - resolution: "@jest/types@npm:29.4.3" +"@jest/types@npm:^29.4.3, @jest/types@npm:^29.5.0": + version: 29.5.0 + resolution: "@jest/types@npm:29.5.0" dependencies: "@jest/schemas": ^29.4.3 "@types/istanbul-lib-coverage": ^2.0.0 @@ -1669,7 +1669,7 @@ __metadata: "@types/node": "*" "@types/yargs": ^17.0.8 chalk: ^4.0.0 - checksum: 1756f4149d360f98567f56f434144f7af23ed49a2c42889261a314df6b6654c2de70af618fb2ee0ee39cadaf10835b885845557184509503646c9cb9dcc02bac + checksum: 1811f94b19cf8a9460a289c4f056796cfc373480e0492692a6125a553cd1a63824bd846d7bb78820b7b6f758f6dd3c2d4558293bb676d541b2fa59c70fdf9d39 languageName: node linkType: hard @@ -2738,7 +2738,7 @@ __metadata: execa: ^5.0.0 jest: ^29.0.0 jest-runner: ^29.0.0 - jest-worker: ^29.0.0 + jest-worker: ^29.5.0 p-limit: ^3.1.0 prettier: ^2.0.5 strip-ansi: ^6.0.0 @@ -4532,17 +4532,17 @@ __metadata: languageName: node linkType: hard -"jest-util@npm:^29.4.3": - version: 29.4.3 - resolution: "jest-util@npm:29.4.3" +"jest-util@npm:^29.4.3, jest-util@npm:^29.5.0": + version: 29.5.0 + resolution: "jest-util@npm:29.5.0" dependencies: - "@jest/types": ^29.4.3 + "@jest/types": ^29.5.0 "@types/node": "*" chalk: ^4.0.0 ci-info: ^3.2.0 graceful-fs: ^4.2.9 picomatch: ^2.2.3 - checksum: 606b3e6077895baf8fb4ad4d08c134f37a6b81d5ba77ae654c942b1ae4b7294ab3b5a0eb93db34f129407b367970cf3b76bf5c80897b30f215f2bc8bf20a5f3f + checksum: fd9212950d34d2ecad8c990dda0d8ea59a8a554b0c188b53ea5d6c4a0829a64f2e1d49e6e85e812014933d17426d7136da4785f9cf76fff1799de51b88bc85d3 languageName: node linkType: hard @@ -4576,15 +4576,15 @@ __metadata: languageName: node linkType: hard -"jest-worker@npm:^29.0.0, jest-worker@npm:^29.4.3": - version: 29.4.3 - resolution: "jest-worker@npm:29.4.3" +"jest-worker@npm:^29.4.3, jest-worker@npm:^29.5.0": + version: 29.5.0 + resolution: "jest-worker@npm:29.5.0" dependencies: "@types/node": "*" - jest-util: ^29.4.3 + jest-util: ^29.5.0 merge-stream: ^2.0.0 supports-color: ^8.0.0 - checksum: c99ae66f257564613e72c5797c3a68f21a22e1c1fb5f30d14695ff5b508a0d2405f22748f13a3df8d1015b5e16abb130170f81f047ff68f58b6b1d2ff6ebc51b + checksum: 1151a1ae3602b1ea7c42a8f1efe2b5a7bf927039deaa0827bf978880169899b705744e288f80a63603fb3fc2985e0071234986af7dc2c21c7a64333d8777c7c9 languageName: node linkType: hard