From 1b4c7d1729169941d12350a1149262c45e86678d Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 5 Mar 2023 21:59:22 +0100 Subject: [PATCH 1/3] feat(jest-worker): support passing a URL as path to worker --- CHANGELOG.md | 1 + packages/jest-worker/README.md | 4 ++-- .../jest-worker/src/__tests__/index.test.ts | 24 +++++++++++++++++++ packages/jest-worker/src/index.ts | 11 +++++++-- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f98b97eb773b..5ace39d18586 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - `[jest-runtime, @jest/transform]` Allow V8 coverage provider to collect coverage from files which were not loaded explicitly ([#13974](https://github.com/facebook/jest/pull/13974)) - `[jest-snapshot]` Add support to `cts` and `mts` TypeScript files to inline snapshots ([#13975](https://github.com/facebook/jest/pull/13975)) - `[jest-worker]` Add `start` method to worker farms ([#13937](https://github.com/facebook/jest/pull/13937)) +- `[jest-worker]` Support passing a URL as path to worker ### Fixes diff --git a/packages/jest-worker/README.md b/packages/jest-worker/README.md index f796d27889b5..bf14f9fe7e5d 100644 --- a/packages/jest-worker/README.md +++ b/packages/jest-worker/README.md @@ -49,9 +49,9 @@ To use `worker_threads` instead of default `child_process` you have to pass `ena The `Worker` export is a constructor that is initialized by passing the worker path, plus an options object. -### `workerPath: string` (required) +### `workerPath: string | URL` (required) -Node module name or absolute path of the file to be loaded in the child processes. Use `require.resolve` to transform a relative path into an absolute one. +Node module name or absolute path or file URL of the file to be loaded in the child processes. You can use `require.resolve` to transform a relative path into an absolute one. ### `options: Object` (optional) diff --git a/packages/jest-worker/src/__tests__/index.test.ts b/packages/jest-worker/src/__tests__/index.test.ts index 35acffc3c197..4a53882b3da1 100644 --- a/packages/jest-worker/src/__tests__/index.test.ts +++ b/packages/jest-worker/src/__tests__/index.test.ts @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ +import {pathToFileURL} from 'url'; import type {JestWorkerFarm, Worker, WorkerFarmOptions} from '../'; import type FarmClass from '../Farm'; import type WorkerPoolClass from '../WorkerPool'; @@ -71,6 +72,29 @@ it('makes a non-existing relative worker throw', () => { }).toThrow("'workerPath' must be absolute"); }); +it('supports URLs', () => { + const absoluteWorkerPath = '/tmp/baz.js'; + + const workerPathUrl = pathToFileURL(absoluteWorkerPath); + + // eslint-disable-next-line no-new + new WorkerFarm(workerPathUrl, {exposedMethods: ['foo', 'bar']}); + // eslint-disable-next-line no-new + new WorkerFarm(workerPathUrl.href, {exposedMethods: ['foo', 'bar']}); + + expect(WorkerPool).toHaveBeenCalledTimes(2); + expect(WorkerPool).toHaveBeenNthCalledWith( + 1, + absoluteWorkerPath, + expect.anything(), + ); + expect(WorkerPool).toHaveBeenNthCalledWith( + 2, + absoluteWorkerPath, + expect.anything(), + ); +}); + it('exposes the right API using default working', () => { const farm = new WorkerFarm('/tmp/baz.js', { exposedMethods: ['foo', 'bar'], diff --git a/packages/jest-worker/src/index.ts b/packages/jest-worker/src/index.ts index c5f5b46bf524..16afcc351525 100644 --- a/packages/jest-worker/src/index.ts +++ b/packages/jest-worker/src/index.ts @@ -11,6 +11,7 @@ import { cpus, } from 'os'; import {isAbsolute} from 'path'; +import {fileURLToPath} from 'url'; import Farm from './Farm'; import WorkerPool from './WorkerPool'; import type { @@ -95,11 +96,17 @@ export class Worker { private readonly _options: WorkerFarmOptions; private readonly _workerPool: WorkerPoolInterface; - constructor(workerPath: string, options?: WorkerFarmOptions) { + constructor(workerPath: string | URL, options?: WorkerFarmOptions) { this._options = {...options}; this._ending = false; - if (!isAbsolute(workerPath)) { + if (typeof workerPath !== 'string') { + workerPath = workerPath.href; + } + + if (workerPath.startsWith('file:')) { + workerPath = fileURLToPath(workerPath); + } else if (!isAbsolute(workerPath)) { throw new Error(`'workerPath' must be absolute, got '${workerPath}'`); } From a2c2428cfddb7b3884dd30bc2dd50bb795610012 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 5 Mar 2023 22:00:50 +0100 Subject: [PATCH 2/3] link in changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ace39d18586..a8aa33f482eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ - `[jest-runtime, @jest/transform]` Allow V8 coverage provider to collect coverage from files which were not loaded explicitly ([#13974](https://github.com/facebook/jest/pull/13974)) - `[jest-snapshot]` Add support to `cts` and `mts` TypeScript files to inline snapshots ([#13975](https://github.com/facebook/jest/pull/13975)) - `[jest-worker]` Add `start` method to worker farms ([#13937](https://github.com/facebook/jest/pull/13937)) -- `[jest-worker]` Support passing a URL as path to worker +- `[jest-worker]` Support passing a URL as path to worker ([#13982](https://github.com/facebook/jest/pull/13982)) ### Fixes From 364b227530b4bfaf63872979330f14f3901b9043 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 5 Mar 2023 22:16:31 +0100 Subject: [PATCH 3/3] fix test on windows --- packages/jest-worker/src/__tests__/index.test.ts | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/packages/jest-worker/src/__tests__/index.test.ts b/packages/jest-worker/src/__tests__/index.test.ts index 4a53882b3da1..51af067997a6 100644 --- a/packages/jest-worker/src/__tests__/index.test.ts +++ b/packages/jest-worker/src/__tests__/index.test.ts @@ -73,9 +73,7 @@ it('makes a non-existing relative worker throw', () => { }); it('supports URLs', () => { - const absoluteWorkerPath = '/tmp/baz.js'; - - const workerPathUrl = pathToFileURL(absoluteWorkerPath); + const workerPathUrl = pathToFileURL(__filename); // eslint-disable-next-line no-new new WorkerFarm(workerPathUrl, {exposedMethods: ['foo', 'bar']}); @@ -83,16 +81,8 @@ it('supports URLs', () => { new WorkerFarm(workerPathUrl.href, {exposedMethods: ['foo', 'bar']}); expect(WorkerPool).toHaveBeenCalledTimes(2); - expect(WorkerPool).toHaveBeenNthCalledWith( - 1, - absoluteWorkerPath, - expect.anything(), - ); - expect(WorkerPool).toHaveBeenNthCalledWith( - 2, - absoluteWorkerPath, - expect.anything(), - ); + expect(WorkerPool).toHaveBeenNthCalledWith(1, __filename, expect.anything()); + expect(WorkerPool).toHaveBeenNthCalledWith(2, __filename, expect.anything()); }); it('exposes the right API using default working', () => {