Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jest-worker): support passing a URL as path to worker #13982

Merged
merged 3 commits into from Mar 5, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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 ([#13982](https://github.com/facebook/jest/pull/13982))

### Fixes

Expand Down
4 changes: 2 additions & 2 deletions packages/jest-worker/README.md
Expand Up @@ -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)

Expand Down
14 changes: 14 additions & 0 deletions packages/jest-worker/src/__tests__/index.test.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -71,6 +72,19 @@ it('makes a non-existing relative worker throw', () => {
}).toThrow("'workerPath' must be absolute");
});

it('supports URLs', () => {
const workerPathUrl = pathToFileURL(__filename);

// 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, __filename, expect.anything());
expect(WorkerPool).toHaveBeenNthCalledWith(2, __filename, expect.anything());
});

it('exposes the right API using default working', () => {
const farm = new WorkerFarm('/tmp/baz.js', {
exposedMethods: ['foo', 'bar'],
Expand Down
11 changes: 9 additions & 2 deletions packages/jest-worker/src/index.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}'`);
}

Expand Down