Skip to content

Commit 331814a

Browse files
authored
👷(worker) Move to Vitest (#5347)
**Description** <!-- Please provide a short description and potentially linked issues justifying the need for this PR --> Change our test runner to Vitest. Jest is great but it's partial support for ESM is putting us in troubles. As such we passed fast-check to Vitest a while ago and want to unify all others so that we preserve a unique test runner in our CI. <!-- * Your PR is fixing a bug or regression? Check for existing issues related to this bug and link them --> <!-- * Your PR is adding a new feature? Make sure there is a related issue or discussion attached to it --> <!-- You can provide any additional context to help into understanding what's this PR is attempting to solve: reproduction of a bug, code snippets... --> **Checklist** — _Don't delete this checklist and make sure you do the following before opening the PR_ - [x] The name of my PR follows [gitmoji](https://gitmoji.dev/) specification - [x] My PR references one of several related issues (if any) - [x] New features or breaking changes must come with an associated Issue or Discussion - [x] My PR does not add any new dependency without an associated Issue or Discussion - [x] My PR includes bumps details, please run `yarn bump` and flag the impacts properly - [x] My PR adds relevant tests and they would have failed without my PR (when applicable) <!-- More about contributing at https://github.com/dubzzz/fast-check/blob/main/CONTRIBUTING.md --> **Advanced** <!-- How to fill the advanced section is detailed below! --> - [x] Category: 👷 Build tools & CI - [x] Impacts: Test runner <!-- [Category] Please use one of the categories below, it will help us into better understanding the urgency of the PR --> <!-- * ✨ Introduce new features --> <!-- * 📝 Add or update documentation --> <!-- * ✅ Add or update tests --> <!-- * 🐛 Fix a bug --> <!-- * 🏷️ Add or update types --> <!-- * ⚡️ Improve performance --> <!-- * _Other(s):_ ... --> <!-- [Impacts] Please provide a comma separated list of the potential impacts that might be introduced by this change --> <!-- * Generated values: Can your change impact any of the existing generators in terms of generated values, if so which ones? when? --> <!-- * Shrink values: Can your change impact any of the existing generators in terms of shrink values, if so which ones? when? --> <!-- * Performance: Can it require some typings changes on user side? Please give more details --> <!-- * Typings: Is there a potential performance impact? In which cases? -->
1 parent 64bdcb5 commit 331814a

20 files changed

+74
-61
lines changed

.changeset/empty-beers-tie.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fast-check/worker": patch
3+
---
4+
5+
👷(worker) Move to Vitest

packages/worker/jest.config.js

-4
This file was deleted.

packages/worker/package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"build:publish-types": "tsc -p tsconfig.publish.types.json && tsc -p tsconfig.publish.types.json --outDir lib/cjs",
2929
"build:publish-cjs": "tsc -p tsconfig.publish.json --outDir lib/cjs && cp package.cjs-template.json lib/cjs/package.json",
3030
"build:publish-esm": "tsc -p tsconfig.publish.json --module es2015 --moduleResolution node",
31-
"test": "yarn node --experimental-vm-modules $(yarn bin jest)",
31+
"test": "vitest",
3232
"test-bundle:cjs": "node test-bundle/main.cjs",
3333
"test-bundle:mjs": "node test-bundle/main.mjs",
3434
"test-bundle": "yarn test-bundle:cjs && yarn test-bundle:mjs",
@@ -56,9 +56,8 @@
5656
"@types/jest": "^29.5.13",
5757
"@types/node": "^20.14.15",
5858
"babel-jest": "^29.7.0",
59-
"jest": "^29.7.0",
60-
"jest-ts-webcompat-resolver": "^1.0.0",
61-
"typescript": "~5.6.3"
59+
"typescript": "~5.6.3",
60+
"vitest": "^2.1.3"
6261
},
6362
"keywords": [
6463
"worker",

packages/worker/test/e2e/asyncThrow.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isMainThread } from 'node:worker_threads';
22
import type { Parameters } from 'fast-check';
33
import { assert } from '@fast-check/worker';
4+
import { describe, it, expect } from 'vitest';
45

56
/* eslint-disable @typescript-eslint/ban-ts-comment */
67
// @ts-ignore

packages/worker/test/e2e/blockEventLoop.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isMainThread } from 'node:worker_threads';
22
import type { Parameters } from 'fast-check';
33
import { assert } from '@fast-check/worker';
4+
import { describe, it, expect } from 'vitest';
45

56
/* eslint-disable @typescript-eslint/ban-ts-comment */
67
// @ts-ignore

packages/worker/test/e2e/concurrentAssert.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isMainThread } from 'node:worker_threads';
22
import type { Parameters } from 'fast-check';
33
import { assert } from '@fast-check/worker';
4+
import { describe, it, expect } from 'vitest';
45

56
/* eslint-disable @typescript-eslint/ban-ts-comment */
67
// @ts-ignore

packages/worker/test/e2e/failing.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isMainThread } from 'node:worker_threads';
22
import type { Parameters } from 'fast-check';
33
import { assert } from '@fast-check/worker';
4+
import { describe, it, expect } from 'vitest';
45

56
/* eslint-disable @typescript-eslint/ban-ts-comment */
67
// @ts-ignore

packages/worker/test/e2e/noWorker.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isMainThread } from 'node:worker_threads';
22
import fc, { type Parameters } from 'fast-check';
33
import { assert } from '@fast-check/worker';
4+
import { describe, it, expect } from 'vitest';
45

56
if (isMainThread) {
67
describe('@fast-check/worker', () => {

packages/worker/test/e2e/nonSerializableButSameData.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { isMainThread } from 'node:worker_threads';
22
import type { Parameters } from 'fast-check';
33
import { check, stringify } from 'fast-check';
44
import { assert } from '@fast-check/worker';
5+
import { describe, it, expect } from 'vitest';
56

67
import {
78
nonSerializableButSameDataProperty,

packages/worker/test/e2e/nonSerializableData.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isMainThread } from 'node:worker_threads';
22
import type { Parameters } from 'fast-check';
33
import { assert } from '@fast-check/worker';
4+
import { describe, it, expect } from 'vitest';
45

56
import {
67
nonSerializableDataProperty,

packages/worker/test/e2e/passing.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isMainThread } from 'node:worker_threads';
22
import type { Parameters } from 'fast-check';
33
import { assert } from '@fast-check/worker';
4+
import { describe, it, expect } from 'vitest';
45

56
/* eslint-disable @typescript-eslint/ban-ts-comment */
67
// @ts-ignore

packages/worker/test/e2e/predicateIsolation.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isMainThread } from 'node:worker_threads';
22
import type { Parameters } from 'fast-check';
33
import { assert } from '@fast-check/worker';
4+
import { describe, it, expect } from 'vitest';
45

56
/* eslint-disable @typescript-eslint/ban-ts-comment */
67
// @ts-ignore

packages/worker/test/e2e/propertyIsolation.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isMainThread } from 'node:worker_threads';
22
import type { Parameters } from 'fast-check';
33
import { assert } from '@fast-check/worker';
4+
import { describe, it, expect } from 'vitest';
45

56
/* eslint-disable @typescript-eslint/ban-ts-comment */
67
// @ts-ignore

packages/worker/test/e2e/supportPre.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isMainThread } from 'node:worker_threads';
22
import type { Parameters } from 'fast-check';
33
import { assert } from '@fast-check/worker';
4+
import { describe, it, expect } from 'vitest';
45

56
/* eslint-disable @typescript-eslint/ban-ts-comment */
67
// @ts-ignore

packages/worker/test/e2e/syncThrow.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isMainThread } from 'node:worker_threads';
22
import type { Parameters } from 'fast-check';
33
import { assert } from '@fast-check/worker';
4+
import { describe, it, expect } from 'vitest';
45

56
/* eslint-disable @typescript-eslint/ban-ts-comment */
67
// @ts-ignore

packages/worker/test/e2e/unregistered.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isMainThread } from 'node:worker_threads';
22
import type { Parameters } from 'fast-check';
33
import { assert } from '@fast-check/worker';
4+
import { describe, it, expect } from 'vitest';
45

56
/* eslint-disable @typescript-eslint/ban-ts-comment */
67
// @ts-ignore

packages/worker/test/internals/lock/Lock.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { AcquiredLock } from '../../../src/internals/lock/Lock.js';
22
import { Lock } from '../../../src/internals/lock/Lock.js';
3+
import { describe, it, expect } from 'vitest';
34

45
describe('Lock', () => {
56
it('should be able to take the first lock', async () => {

packages/worker/test/internals/worker-pool/BasicPool.spec.ts

+45-45
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import {
33
type PoolToWorkerMessage,
44
type WorkerToPoolMessage,
55
} from '../../../src/internals/worker-pool/IWorkerPool.js';
6-
import { jest } from '@jest/globals';
6+
import { describe, it, expect, beforeEach, vi } from 'vitest';
7+
import type { MockedObject, MockedFunction } from 'vitest';
8+
import { BasicPool } from '../../../src/internals/worker-pool/BasicPool.js';
9+
import * as WorkerThreadsMock from 'node:worker_threads';
710

8-
jest.unstable_mockModule('node:worker_threads', () => ({
9-
Worker: jest.fn(),
11+
vi.mock('node:worker_threads', () => ({
12+
Worker: vi.fn(),
1013
}));
1114

12-
const { BasicPool } = await import('../../../src/internals/worker-pool/BasicPool.js');
13-
const WorkerThreadsMock = await import('node:worker_threads');
14-
1515
beforeEach(() => {
16-
jest.resetAllMocks();
16+
vi.resetAllMocks();
1717
});
1818

1919
describe('BasicPool', () => {
@@ -55,9 +55,9 @@ describe('BasicPool', () => {
5555
const { on, postMessage } = mockWorker();
5656
const workerFileUrl = new URL('file:///worker.cjs');
5757
const predicateId = 0;
58-
const onSuccess = jest.fn();
59-
const onFailure = jest.fn();
60-
const onSkipped = jest.fn();
58+
const onSuccess = vi.fn();
59+
const onFailure = vi.fn();
60+
const onSkipped = vi.fn();
6161
const pool = new BasicPool<string, string>(workerFileUrl);
6262
const workerPromise = pool.spawnNewWorker();
6363
fireOnlineEvent(on);
@@ -80,9 +80,9 @@ describe('BasicPool', () => {
8080
const { on, postMessage } = mockWorker();
8181
const workerFileUrl = new URL('file:///worker.cjs');
8282
const predicateId = 0;
83-
const onSuccess = jest.fn();
84-
const onFailure = jest.fn();
85-
const onSkipped = jest.fn();
83+
const onSuccess = vi.fn();
84+
const onFailure = vi.fn();
85+
const onSkipped = vi.fn();
8686
const successMessage = 'success!';
8787
const pool = new BasicPool<string, string>(workerFileUrl);
8888
const workerPromise = pool.spawnNewWorker();
@@ -116,9 +116,9 @@ describe('BasicPool', () => {
116116
const { on, postMessage } = mockWorker();
117117
const workerFileUrl = new URL('file:///worker.cjs');
118118
const predicateId = 0;
119-
const onSuccess = jest.fn();
120-
const onFailure = jest.fn();
121-
const onSkipped = jest.fn();
119+
const onSuccess = vi.fn();
120+
const onFailure = vi.fn();
121+
const onSkipped = vi.fn();
122122
const errorMessage = 'oups there was an error!';
123123
const pool = new BasicPool<string, string>(workerFileUrl);
124124
const workerPromise = pool.spawnNewWorker();
@@ -152,9 +152,9 @@ describe('BasicPool', () => {
152152
const { on, postMessage } = mockWorker();
153153
const workerFileUrl = new URL('file:///worker.cjs');
154154
const predicateId = 0;
155-
const onSuccess = jest.fn();
156-
const onFailure = jest.fn();
157-
const onSkipped = jest.fn();
155+
const onSuccess = vi.fn();
156+
const onFailure = vi.fn();
157+
const onSkipped = vi.fn();
158158
const pool = new BasicPool<string, string>(workerFileUrl);
159159
const workerPromise = pool.spawnNewWorker();
160160
fireOnlineEvent(on);
@@ -185,9 +185,9 @@ describe('BasicPool', () => {
185185
const { on, postMessage } = mockWorker();
186186
const workerFileUrl = new URL('file:///worker.cjs');
187187
const predicateId = 0;
188-
const onSuccess = jest.fn();
189-
const onFailure = jest.fn();
190-
const onSkipped = jest.fn();
188+
const onSuccess = vi.fn();
189+
const onFailure = vi.fn();
190+
const onSkipped = vi.fn();
191191
const pool = new BasicPool<string, string>(workerFileUrl);
192192
const workerPromise = pool.spawnNewWorker();
193193
fireOnlineEvent(on);
@@ -231,9 +231,9 @@ describe('BasicPool', () => {
231231
const { on, postMessage } = mockWorker();
232232
const workerFileUrl = new URL('file:///worker.cjs');
233233
const predicateId = 0;
234-
const onSuccess = jest.fn();
235-
const onFailure = jest.fn();
236-
const onSkipped = jest.fn();
234+
const onSuccess = vi.fn();
235+
const onFailure = vi.fn();
236+
const onSkipped = vi.fn();
237237
const pool = new BasicPool<string, string>(workerFileUrl);
238238
const workerPromise = pool.spawnNewWorker();
239239
fireOnlineEvent(on);
@@ -258,9 +258,9 @@ describe('BasicPool', () => {
258258
const { on, postMessage } = mockWorker();
259259
const workerFileUrl = new URL('file:///worker.cjs');
260260
const predicateId = 0;
261-
const onSuccess = jest.fn();
262-
const onFailure = jest.fn();
263-
const onSkipped = jest.fn();
261+
const onSuccess = vi.fn();
262+
const onFailure = vi.fn();
263+
const onSkipped = vi.fn();
264264
const pool = new BasicPool<string, string>(workerFileUrl);
265265
const workerPromise = pool.spawnNewWorker();
266266
fireOnlineEvent(on);
@@ -285,9 +285,9 @@ describe('BasicPool', () => {
285285
const { on, postMessage } = mockWorker();
286286
const workerFileUrl = new URL('file:///worker.cjs');
287287
const predicateId = 0;
288-
const onSuccess = jest.fn();
289-
const onFailure = jest.fn();
290-
const onSkipped = jest.fn();
288+
const onSuccess = vi.fn();
289+
const onFailure = vi.fn();
290+
const onSkipped = vi.fn();
291291
const pool = new BasicPool<string, string>(workerFileUrl);
292292
const workerPromise = pool.spawnNewWorker();
293293
fireOnlineEvent(on);
@@ -382,9 +382,9 @@ describe('BasicPool', () => {
382382
const { on, postMessage, terminate } = mockWorker();
383383
const workerFileUrl = new URL('file:///worker.cjs');
384384
const predicateId = 0;
385-
const onSuccess = jest.fn();
386-
const onFailure = jest.fn();
387-
const onSkipped = jest.fn();
385+
const onSuccess = vi.fn();
386+
const onFailure = vi.fn();
387+
const onSkipped = vi.fn();
388388
const pool = new BasicPool<string, string>(workerFileUrl);
389389
const workerPromise = pool.spawnNewWorker();
390390
fireOnlineEvent(on);
@@ -418,9 +418,9 @@ describe('BasicPool', () => {
418418
const { on, postMessage, terminate } = mockWorker();
419419
const workerFileUrl = new URL('file:///worker.cjs');
420420
const predicateId = 0;
421-
const onSuccess = jest.fn();
422-
const onFailure = jest.fn();
423-
const onSkipped = jest.fn();
421+
const onSuccess = vi.fn();
422+
const onFailure = vi.fn();
423+
const onSkipped = vi.fn();
424424
const pool = new BasicPool<string, string>(workerFileUrl);
425425
const workerPromise = pool.spawnNewWorker();
426426
fireOnlineEvent(on);
@@ -454,9 +454,9 @@ describe('BasicPool', () => {
454454
const { on, terminate } = mockWorker();
455455
const workerFileUrl = new URL('file:///worker.cjs');
456456
const predicateId = 0;
457-
const onSuccess = jest.fn();
458-
const onFailure = jest.fn();
459-
const onSkipped = jest.fn();
457+
const onSuccess = vi.fn();
458+
const onFailure = vi.fn();
459+
const onSkipped = vi.fn();
460460
const pool = new BasicPool<string, string>(workerFileUrl);
461461
const workerPromise = pool.spawnNewWorker();
462462
fireOnlineEvent(on);
@@ -482,10 +482,10 @@ describe('BasicPool', () => {
482482
// Helpers
483483

484484
function mockWorker() {
485-
const Worker = WorkerThreadsMock.Worker as jest.Mocked<typeof WorkerThreadsMock.Worker>;
486-
const on = jest.fn<typeof WorkerThreadsMock.Worker.prototype.on>();
487-
const postMessage = jest.fn<typeof WorkerThreadsMock.Worker.prototype.postMessage>();
488-
const terminate = jest.fn<typeof WorkerThreadsMock.Worker.prototype.terminate>();
485+
const Worker = WorkerThreadsMock.Worker as unknown as MockedObject<typeof WorkerThreadsMock.Worker>;
486+
const on = vi.fn<typeof WorkerThreadsMock.Worker.prototype.on>();
487+
const postMessage = vi.fn<typeof WorkerThreadsMock.Worker.prototype.postMessage>();
488+
const terminate = vi.fn<typeof WorkerThreadsMock.Worker.prototype.terminate>();
489489
Worker.mockImplementation(
490490
() =>
491491
({
@@ -497,7 +497,7 @@ function mockWorker() {
497497
return { Worker, on, postMessage, terminate };
498498
}
499499

500-
function fireOnlineEvent(on: jest.Mock<typeof WorkerThreadsMock.Worker.prototype.on>) {
500+
function fireOnlineEvent(on: MockedFunction<typeof WorkerThreadsMock.Worker.prototype.on>) {
501501
const onOnlineHandler = on.mock.calls.find(([eventName]) => eventName === 'online')![1];
502502
onOnlineHandler();
503503
}

packages/worker/test/internals/worker-property/WorkerPropertyFromWorker.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { jest } from '@jest/globals';
21
import { WorkerPropertyFromWorker } from '../../../src/internals/worker-property/WorkerPropertyFromWorker.js';
32
import fc from 'fast-check';
43
import { xorshift128plus } from 'pure-rand';
4+
import { describe, it, expect, vi } from 'vitest';
55

66
describe('WorkerPropertyFromWorker', () => {
77
it('should not generate anything from the provided arbitraries eagerly on generate', () => {
88
// Arrange
99
const mrng = buildMrng();
1010
const { arbitrary, generate } = buildTrackedArbitrary();
1111
const arbitraries: [fc.Arbitrary<unknown>] = [arbitrary];
12-
const predicate = jest.fn<(...inputs: [unknown]) => Promise<void>>().mockResolvedValue();
12+
const predicate = vi.fn<(...inputs: [unknown]) => Promise<void>>().mockResolvedValue();
1313

1414
// Act
1515
const property = new WorkerPropertyFromWorker(arbitraries, predicate);
@@ -25,7 +25,7 @@ describe('WorkerPropertyFromWorker', () => {
2525
const mrng = buildMrng();
2626
const { arbitrary, generate } = buildTrackedArbitrary();
2727
const arbitraries: [fc.Arbitrary<unknown>] = [arbitrary];
28-
const predicate = jest.fn<(...inputs: [unknown]) => Promise<void>>().mockResolvedValue();
28+
const predicate = vi.fn<(...inputs: [unknown]) => Promise<void>>().mockResolvedValue();
2929

3030
// Act
3131
const property = new WorkerPropertyFromWorker(arbitraries, predicate);
@@ -45,7 +45,7 @@ describe('WorkerPropertyFromWorker', () => {
4545
const { arbitrary, generate } = buildTrackedArbitrary();
4646
generate.mockReturnValue(new fc.Value({ hello: 'world' }, undefined));
4747
const arbitraries: [fc.Arbitrary<unknown>] = [arbitrary];
48-
const predicate = jest.fn<(...inputs: [unknown]) => Promise<void>>().mockResolvedValue();
48+
const predicate = vi.fn<(...inputs: [unknown]) => Promise<void>>().mockResolvedValue();
4949

5050
// Act
5151
const property = new WorkerPropertyFromWorker(arbitraries, predicate);
@@ -72,7 +72,7 @@ describe('WorkerPropertyFromWorker', () => {
7272
return orderedValues[index];
7373
});
7474
const arbitraries: [fc.Arbitrary<unknown>] = [arbitrary];
75-
const predicate = jest.fn<(...inputs: [unknown]) => Promise<void>>().mockResolvedValue();
75+
const predicate = vi.fn<(...inputs: [unknown]) => Promise<void>>().mockResolvedValue();
7676

7777
// Act
7878
const property = new WorkerPropertyFromWorker(arbitraries, predicate);
@@ -119,7 +119,7 @@ class TrackedArbitrary extends fc.Arbitrary<unknown> {
119119
}
120120

121121
function buildTrackedArbitrary() {
122-
const generate = jest.fn<fc.Arbitrary<unknown>['generate']>();
122+
const generate = vi.fn<fc.Arbitrary<unknown>['generate']>();
123123
return { arbitrary: new TrackedArbitrary(generate), generate };
124124
}
125125

0 commit comments

Comments
 (0)