Skip to content

Commit eaf8836

Browse files
authored
ref: Move from Jest to Vitest (#120)
Migrate off of Jest over to Vitest for unit testing in the packages. GH codecov/engineering-team#1596
1 parent e6f3e66 commit eaf8836

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+876
-1889
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ jobs:
187187
run: pnpm run build
188188

189189
- name: Run unit tests
190-
run: pnpm run test:unit:ci --maxWorkers=2
190+
run: pnpm run test:unit:ci
191191

192192
- name: Upload coverage reports to codecov
193193
if: ${{ !github.event.pull_request.head.repo.fork && github.repository_owner == 'codecov' }}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"format": "pnpm -r format && prettier --write '*.{cjs,json}' --ignore-unknown --no-error-on-unmatched-pattern",
1717
"format:check": "pnpm -r --filter='./packages/*' format:check && prettier --check '*.{cjs,json}' --ignore-unknown --no-error-on-unmatched-pattern",
1818
"test:unit": "pnpm -r --filter='./packages/*' run test:unit",
19+
"test:unit:watch": "pnpm -r --filter='./packages/*' run test:unit:watch",
1920
"test:unit:update": "pnpm -r --filter='./packages/*' run test:unit:update",
2021
"test:unit:ci": "pnpm -r --filter='./packages/*' run test:unit:ci",
2122
"test:e2e": "pnpm -r --filter='./integration-tests' run test:e2e",
@@ -43,7 +44,9 @@
4344
"lint-staged": "^15.2.1",
4445
"prettier": "^3.2.4",
4546
"typedoc": "^0.25.12",
46-
"typescript": "^5.3.3"
47+
"typescript": "^5.3.3",
48+
"vite-tsconfig-paths": "^4.3.2",
49+
"vitest": "^1.5.0"
4750
},
4851
"lint-staged": {
4952
"*.{js,jsx,ts,tsx,json,css,scss,md,json}": [

packages/bundler-plugin-core/jest.config.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

packages/bundler-plugin-core/package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
"lint:fix": "pnpm lint --fix",
3333
"format": "prettier '**/*.{cjs,mjs,ts,tsx,md,json}' --ignore-path ../.gitignore --ignore-unknown --no-error-on-unmatched-pattern --write",
3434
"format:check": "prettier '**/*.{cjs,mjs,ts,tsx,md,json}' --ignore-path ../.gitignore --ignore-unknown --no-error-on-unmatched-pattern --check",
35-
"test:unit": "jest --runInBand",
36-
"test:unit:ci": "JEST_JUNIT_OUTPUT_NAME='bundler-plugin-core.junit.xml' jest --coverage --reporters=jest-junit",
35+
"test:unit": "vitest run",
36+
"test:unit:watch": "vitest watch",
37+
"test:unit:ci": "vitest --coverage --reporter=junit --outputFile=./bundler-plugin-core.junit.xml run",
38+
"test:unit:update": "vitest -u run",
3739
"generate:typedoc": "typedoc --options ./typedoc.json"
3840
},
3941
"dependencies": {
@@ -43,21 +45,19 @@
4345
"zod": "^3.22.4"
4446
},
4547
"devDependencies": {
46-
"@swc/core": "^1.3.107",
47-
"@swc/jest": "^0.2.33",
48-
"@types/jest": "^29.5.11",
4948
"@types/node": "^20.11.15",
5049
"@types/semver": "^7.5.6",
50+
"@vitest/coverage-v8": "^1.5.0",
5151
"codecovProdRollupPlugin": "npm:@codecov/[email protected]",
52-
"jest": "^29.7.0",
53-
"jest-junit": "^16.0.0",
5452
"msw": "^2.1.5",
5553
"testdouble": "^3.20.1",
56-
"testdouble-jest": "^2.0.0",
54+
"testdouble-vitest": "^0.1.3",
5755
"ts-node": "^10.9.2",
5856
"typedoc": "^0.25.12",
5957
"typescript": "^5.3.3",
60-
"unbuild": "^2.0.0"
58+
"unbuild": "^2.0.0",
59+
"vite-tsconfig-paths": "^4.3.2",
60+
"vitest": "^1.5.0"
6161
},
6262
"volta": {
6363
"extends": "../../package.json"

packages/bundler-plugin-core/src/utils/__tests__/Output.test.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1-
import { type ProviderServiceParams } from "../../types";
1+
import {
2+
describe,
3+
it,
4+
expect,
5+
vi,
6+
afterEach,
7+
beforeAll,
8+
afterAll,
9+
type MockInstance,
10+
type Mock,
11+
} from "vitest";
12+
213
import { detectProvider } from "../provider";
314
import { Output } from "../Output";
415

5-
jest.mock("../provider");
16+
vi.mock("../provider");
617

7-
const mockedDetectProvider = detectProvider as jest.Mock<
8-
Promise<ProviderServiceParams>
9-
>;
18+
const mockedDetectProvider = detectProvider as Mock;
1019

1120
afterEach(() => {
12-
jest.resetAllMocks();
21+
vi.resetAllMocks();
1322
});
1423

15-
let consoleSpy: jest.SpyInstance;
24+
let consoleSpy: MockInstance;
1625

1726
interface SetupArgs {
1827
statsSendError?: boolean;
@@ -31,9 +40,9 @@ describe("Output", () => {
3140
statsSendError,
3241
urlSendError,
3342
}: SetupArgs) {
34-
const preSignedUrlBody = jest.fn();
35-
const statsBody = jest.fn();
36-
consoleSpy = jest.spyOn(console, "log").mockImplementation(() => null);
43+
const preSignedUrlBody = vi.fn();
44+
const statsBody = vi.fn();
45+
consoleSpy = vi.spyOn(console, "log").mockImplementation(() => null);
3746

3847
mockedDetectProvider.mockResolvedValue({
3948
branch: "main",
@@ -48,7 +57,7 @@ describe("Output", () => {
4857

4958
// need to mock out fetch wholly and check to see if it was called with the correct stuff
5059

51-
const fetchMock = jest
60+
const fetchMock = vi
5261
.spyOn(global, "fetch")
5362
// pre-signed URL
5463
// @ts-expect-error - testing fetch
@@ -87,11 +96,11 @@ describe("Output", () => {
8796

8897
describe("start method", () => {
8998
beforeAll(() => {
90-
jest.useFakeTimers().setSystemTime(1000);
99+
vi.useFakeTimers().setSystemTime(1000);
91100
});
92101

93102
afterAll(() => {
94-
jest.useRealTimers();
103+
vi.useRealTimers();
95104
});
96105

97106
it("should set builtAt to the current time", () => {
@@ -117,11 +126,11 @@ describe("Output", () => {
117126

118127
describe("end method", () => {
119128
beforeAll(() => {
120-
jest.useFakeTimers().setSystemTime(1000);
129+
vi.useFakeTimers().setSystemTime(1000);
121130
});
122131

123132
afterAll(() => {
124-
jest.useRealTimers();
133+
vi.useRealTimers();
125134
});
126135

127136
describe("builtAt is set", () => {

packages/bundler-plugin-core/src/utils/__tests__/fetchWithRetry.test.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
import {
2+
vi,
3+
beforeAll,
4+
afterAll,
5+
afterEach,
6+
describe,
7+
expect,
8+
it,
9+
type MockInstance,
10+
} from "vitest";
111
import { HttpResponse, http } from "msw";
212
import { setupServer } from "msw/node";
313

414
import { fetchWithRetry } from "../fetchWithRetry";
515

6-
jest.mock("../delay.ts");
16+
vi.mock("../delay.ts");
717

818
const server = setupServer();
919

@@ -28,7 +38,7 @@ interface SetupArgs {
2838
}
2939

3040
describe("fetchWithRetry", () => {
31-
let consoleSpy: jest.SpyInstance;
41+
let consoleSpy: MockInstance;
3242

3343
afterEach(() => {
3444
consoleSpy.mockReset();
@@ -41,7 +51,7 @@ describe("fetchWithRetry", () => {
4151
failFetch = false,
4252
retryCount = 0,
4353
}: SetupArgs) {
44-
consoleSpy = jest.spyOn(console, "log").mockImplementation(() => null);
54+
consoleSpy = vi.spyOn(console, "log").mockImplementation(() => null);
4555

4656
server.use(
4757
http.all("http://localhost", ({}) => {

packages/bundler-plugin-core/src/utils/__tests__/getPreSignedURL.test.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
import {
2+
vi,
3+
describe,
4+
it,
5+
expect,
6+
beforeAll,
7+
afterAll,
8+
afterEach,
9+
type MockInstance,
10+
} from "vitest";
111
import { HttpResponse, http } from "msw";
212
import { setupServer } from "msw/node";
313

@@ -28,10 +38,10 @@ interface SetupArgs {
2838
}
2939

3040
describe("getPreSignedURL", () => {
31-
let consoleSpy: jest.SpyInstance;
41+
let consoleSpy: MockInstance;
3242

3343
function setup({ status = 200, data = {}, sendError = false }: SetupArgs) {
34-
consoleSpy = jest.spyOn(console, "log").mockImplementation(() => null);
44+
consoleSpy = vi.spyOn(console, "log").mockImplementation(() => null);
3545

3646
server.use(
3747
http.post("http://localhost/upload/bundle_analysis/v1", ({}) => {
@@ -156,9 +166,9 @@ describe("getPreSignedURL", () => {
156166
});
157167

158168
describe("returned data is undefined", () => {
159-
let fetchSpy: jest.SpyInstance;
169+
let fetchSpy: MockInstance;
160170
beforeAll(() => {
161-
fetchSpy = jest
171+
fetchSpy = vi
162172
.spyOn(global, "fetch")
163173
.mockResolvedValue(new Response(undefined, { status: 200 }));
164174
});
@@ -218,9 +228,9 @@ describe("getPreSignedURL", () => {
218228
});
219229

220230
describe("fetch throws an error", () => {
221-
let fetchSpy: jest.SpyInstance;
231+
let fetchSpy: MockInstance;
222232
beforeAll(() => {
223-
fetchSpy = jest
233+
fetchSpy = vi
224234
.spyOn(global, "fetch")
225235
.mockRejectedValue(new Error("Failed to fetch"));
226236
});

packages/bundler-plugin-core/src/utils/__tests__/normalizeOptions.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { describe, it, expect } from "vitest";
12
import { type Options } from "../../types.ts";
23
import {
34
normalizeOptions,

packages/bundler-plugin-core/src/utils/__tests__/normalizePath.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { describe, expect, it } from "vitest";
12
import { normalizePath } from "../normalizePath";
23

34
interface Test {

packages/bundler-plugin-core/src/utils/__tests__/preProcessBody.test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1+
import {
2+
describe,
3+
it,
4+
expect,
5+
beforeEach,
6+
afterEach,
7+
vi,
8+
type MockInstance,
9+
} from "vitest";
110
import { InvalidSlugError } from "../../errors/InvalidSlugError";
211
import { preProcessBody } from "../preProcessBody";
312

413
describe("preProcessBody", () => {
5-
let consoleSpy: jest.SpyInstance;
14+
let consoleSpy: MockInstance;
615

716
beforeEach(() => {
8-
consoleSpy = jest.spyOn(console, "log").mockImplementation(() => null);
17+
consoleSpy = vi.spyOn(console, "log").mockImplementation(() => null);
918
});
1019

1120
afterEach(() => {

0 commit comments

Comments
 (0)