Skip to content

Commit

Permalink
👷(poisoning) Move to Vitest (#5353)
Browse files Browse the repository at this point in the history
**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?
-->
  • Loading branch information
dubzzz authored Oct 22, 2024
1 parent 83874bd commit 36b418b
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 58 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-owls-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fast-check/poisoning": patch
---

👷(poisoning) Move to Vitest
3 changes: 0 additions & 3 deletions packages/poisoning/babel.config.cjs

This file was deleted.

4 changes: 0 additions & 4 deletions packages/poisoning/jest.config.js

This file was deleted.

12 changes: 3 additions & 9 deletions packages/poisoning/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"build:publish-types": "tsc -p tsconfig.publish.types.json && tsc -p tsconfig.publish.types.json --outDir lib/cjs",
"build:publish-cjs": "tsc -p tsconfig.publish.json --outDir lib/cjs && cp package.cjs-template.json lib/cjs/package.json",
"build:publish-esm": "tsc -p tsconfig.publish.json --module es2015 --moduleResolution node",
"test": "yarn node --experimental-vm-modules $(yarn bin jest)",
"test": "vitest",
"typecheck": "tsc --noEmit"
},
"repository": {
Expand All @@ -43,15 +43,9 @@
},
"homepage": "https://github.com/dubzzz/fast-check/tree/main/packages/poisoning#readme",
"devDependencies": {
"@babel/core": "^7.25.8",
"@babel/preset-typescript": "^7.25.7",
"@jest/globals": "^29.7.0",
"@types/jest": "^29.5.13",
"@types/node": "^20.14.15",
"babel-jest": "^29.7.0",
"jest": "^29.7.0",
"jest-ts-webcompat-resolver": "^1.0.0",
"typescript": "~5.6.3"
"typescript": "~5.6.3",
"vitest": "^2.1.3"
},
"keywords": [
"poisoning",
Expand Down
8 changes: 7 additions & 1 deletion packages/poisoning/test/internals/CaptureAllGlobals.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest';
import { captureAllGlobals } from '../../src/internals/CaptureAllGlobals.js';
import type { PoisoningFreeMap } from '../../src/internals/PoisoningFreeMap.js';
import { PoisoningFreeSet } from '../../src/internals/PoisoningFreeSet.js';
Expand Down Expand Up @@ -133,7 +134,12 @@ describe('captureAllGlobals', () => {
const globals = captureAllGlobals();

// Assert
expect(globals.get(globalValue)?.rootAncestors).toEqual(expectedRoots);
const rootAncestorsWithoutVitest = new Set(
// We are ignoring any root ancestor related to Vitest as they are likely to evolve over time.
// As such we stick to non test framework related ones.
[...(globals.get(globalValue)?.rootAncestors ?? new Set())].filter((name) => !/__vitest_\w+__/.test(name)),
);
expect(rootAncestorsWithoutVitest).toEqual(expectedRoots);
},
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest';
import type { GlobalDetails } from '../../src/internals/types/AllGlobals';
import { shouldIgnoreGlobal, shouldIgnoreProperty } from '../../src/internals/FilterNonEligibleDiffs';
import { PoisoningFreeSet } from '../../src/internals/PoisoningFreeSet';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest';
import { MapSymbol, PushSymbol, SortSymbol, PoisoningFreeArray } from '../../src/internals/PoisoningFreeArray.js';

describe('PoisoningFreeArray', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/poisoning/test/internals/PoisoningFreeMap.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest';
import {
EntriesSymbol,
GetSymbol,
Expand Down
1 change: 1 addition & 0 deletions packages/poisoning/test/internals/PoisoningFreeSet.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest';
import { AddSymbol, HasSymbol, PoisoningFreeSet } from '../../src/internals/PoisoningFreeSet.js';

describe('PoisoningFreeSet', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest';
import { PoisoningFreeMap } from '../../src/internals/PoisoningFreeMap.js';
import { PoisoningFreeSet } from '../../src/internals/PoisoningFreeSet.js';
import { trackDiffsOnGlobals } from '../../src/internals/TrackDiffsOnGlobal.js';
Expand Down
52 changes: 27 additions & 25 deletions packages/poisoning/test/main.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { jest } from '@jest/globals';
import { describe, it, expect, vi } from 'vitest';
import { assertNoPoisoning, restoreGlobals } from '../src/main.js';

const options = { ignoredRootRegex: /__vitest_worker__/ };

describe('assertNoPoisoning', () => {
it('should not throw any Error if no poisoning occurred', () => {
// Arrange / Act / Assert
expect(() => assertNoPoisoning()).not.toThrow();
restoreGlobals();
expect(() => assertNoPoisoning()).not.toThrow();
expect(() => assertNoPoisoning(options)).not.toThrow();
restoreGlobals(options);
expect(() => assertNoPoisoning(options)).not.toThrow();
});

it('should throw an Error if new global appeared and be able to revert the change', () => {
Expand All @@ -17,9 +19,9 @@ describe('assertNoPoisoning', () => {

// Act / Assert
try {
expect(() => assertNoPoisoning()).toThrowError(/Poisoning detected/);
restoreGlobals();
expect(() => assertNoPoisoning()).not.toThrow();
expect(() => assertNoPoisoning(options)).toThrowError(/Poisoning detected/);
restoreGlobals(options);
expect(() => assertNoPoisoning(options)).not.toThrow();
} finally {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand All @@ -36,9 +38,9 @@ describe('assertNoPoisoning', () => {

// Act / Assert
try {
expect(() => assertNoPoisoning()).toThrowError(/Poisoning detected/);
restoreGlobals();
expect(() => assertNoPoisoning()).not.toThrow();
expect(() => assertNoPoisoning(options)).toThrowError(/Poisoning detected/);
restoreGlobals(options);
expect(() => assertNoPoisoning(options)).not.toThrow();
} finally {
globalThis.Function = F;
}
Expand All @@ -47,13 +49,13 @@ describe('assertNoPoisoning', () => {
it('should throw an Error if global altered via globalThis and be able to revert the change', () => {
// Arrange
const F = globalThis.Function;
globalThis.Function = jest.fn() as any;
globalThis.Function = vi.fn() as any;

// Act / Assert
try {
expect(() => assertNoPoisoning()).toThrowError(/Poisoning detected/);
restoreGlobals();
expect(() => assertNoPoisoning()).not.toThrow();
expect(() => assertNoPoisoning(options)).toThrowError(/Poisoning detected/);
restoreGlobals(options);
expect(() => assertNoPoisoning(options)).not.toThrow();
} finally {
globalThis.Function = F;
}
Expand All @@ -63,13 +65,13 @@ describe('assertNoPoisoning', () => {
// Arrange
const F = Function;
// eslint-disable-next-line no-global-assign
Function = jest.fn() as any;
Function = vi.fn() as any;

// Act / Assert
try {
expect(() => assertNoPoisoning()).toThrowError(/Poisoning detected/);
restoreGlobals();
expect(() => assertNoPoisoning()).not.toThrow();
expect(() => assertNoPoisoning(options)).toThrowError(/Poisoning detected/);
restoreGlobals(options);
expect(() => assertNoPoisoning(options)).not.toThrow();
} finally {
// eslint-disable-next-line no-global-assign
Function = F;
Expand All @@ -85,7 +87,7 @@ describe('assertNoPoisoning', () => {
// Act / Assert
let error: unknown = undefined;
try {
assertNoPoisoning();
assertNoPoisoning(options);
} catch (err) {
error = err;
}
Expand All @@ -100,8 +102,8 @@ describe('assertNoPoisoning', () => {
throw new Error(`Received error does not fulfill expectations, got: ${error}`);
}
try {
restoreGlobals();
expect(() => assertNoPoisoning()).not.toThrow();
restoreGlobals(options);
expect(() => assertNoPoisoning(options)).not.toThrow();
} finally {
// eslint-disable-next-line no-global-assign
(globalThis as any) = G;
Expand Down Expand Up @@ -141,11 +143,11 @@ describe('assertNoPoisoning', () => {
dropAll('Error', Error);

// Act / Assert
// Manual expectation mimicing "expect(() => assertNoPoisoning()).toThrowError(/Poisoning detected/)"
// Manual expectation mimicing "expect(() => assertNoPoisoning(options)).toThrowError(/Poisoning detected/)"
// as Jest makes use of Object.keys and probably others in its code
let caughtError: unknown = undefined;
try {
assertNoPoisoning();
assertNoPoisoning(options);
} catch (err) {
caughtError = err;
}
Expand All @@ -155,8 +157,8 @@ describe('assertNoPoisoning', () => {
if (!(caughtError instanceof Error)) {
throw new Error('Expected an error of type Error to be thrown during the test');
}
restoreGlobals();
expect(() => assertNoPoisoning()).not.toThrow();
restoreGlobals(options);
expect(() => assertNoPoisoning(options)).not.toThrow();
// WARNING: If restoreGlobals failed, then this test may break others
});
});
17 changes: 1 addition & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3091,15 +3091,9 @@ __metadata:
version: 0.0.0-use.local
resolution: "@fast-check/poisoning@workspace:packages/poisoning"
dependencies:
"@babel/core": "npm:^7.25.8"
"@babel/preset-typescript": "npm:^7.25.7"
"@jest/globals": "npm:^29.7.0"
"@types/jest": "npm:^29.5.13"
"@types/node": "npm:^20.14.15"
babel-jest: "npm:^29.7.0"
jest: "npm:^29.7.0"
jest-ts-webcompat-resolver: "npm:^1.0.0"
typescript: "npm:~5.6.3"
vitest: "npm:^2.1.3"
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -12786,15 +12780,6 @@ __metadata:
languageName: node
linkType: hard

"jest-ts-webcompat-resolver@npm:^1.0.0":
version: 1.0.0
resolution: "jest-ts-webcompat-resolver@npm:1.0.0"
peerDependencies:
jest-resolve: "*"
checksum: 10c0/0567766de8fc25a9b87fedfdafc3bee093ff09020b1f564fbf8eb5e324c77afee4f43c3bbfe36e09a08526bd5daed4697098192a9815fea09a263a473b9627b4
languageName: node
linkType: hard

"jest-util@npm:^29.7.0":
version: 29.7.0
resolution: "jest-util@npm:29.7.0"
Expand Down

0 comments on commit 36b418b

Please sign in to comment.