From 96093b4083137eccd7d623e6ca5773f07bec4b3a Mon Sep 17 00:00:00 2001 From: Brian Donovan <1938+eventualbuddha@users.noreply.github.com> Date: Mon, 16 Dec 2024 16:05:18 -0800 Subject: [PATCH] test(grout/test-utils): replace `jest` with `vitest` --- libs/grout/test-utils/.eslintignore | 1 + libs/grout/test-utils/jest.config.js | 8 ------- libs/grout/test-utils/package.json | 13 +++++------- libs/grout/test-utils/src/mock_client.test.ts | 11 +++++----- libs/grout/test-utils/src/mock_client.ts | 6 +++--- libs/grout/test-utils/vitest.config.ts | 3 +++ pnpm-lock.yaml | 21 ++++++------------- 7 files changed, 24 insertions(+), 39 deletions(-) delete mode 100644 libs/grout/test-utils/jest.config.js create mode 100644 libs/grout/test-utils/vitest.config.ts diff --git a/libs/grout/test-utils/.eslintignore b/libs/grout/test-utils/.eslintignore index 6bf89faa9d..b392aa2c36 100644 --- a/libs/grout/test-utils/.eslintignore +++ b/libs/grout/test-utils/.eslintignore @@ -1,4 +1,5 @@ *.js *.d.ts +vitest.config.ts build coverage \ No newline at end of file diff --git a/libs/grout/test-utils/jest.config.js b/libs/grout/test-utils/jest.config.js deleted file mode 100644 index 6d78c29b98..0000000000 --- a/libs/grout/test-utils/jest.config.js +++ /dev/null @@ -1,8 +0,0 @@ -const shared = require('../../../jest.config.shared'); - -/** - * @type {import('@jest/types').Config.InitialOptions} - */ -module.exports = { - ...shared, -}; diff --git a/libs/grout/test-utils/package.json b/libs/grout/test-utils/package.json index c9bc511d86..5a6def6c52 100644 --- a/libs/grout/test-utils/package.json +++ b/libs/grout/test-utils/package.json @@ -15,26 +15,23 @@ "lint:fix": "pnpm type-check && eslint . --fix", "pre-commit": "lint-staged", "test": "is-ci test:ci test:watch", - "test:ci": "jest --coverage --reporters=default --reporters=jest-junit --maxWorkers=6", - "test:coverage": "jest --coverage", - "test:watch": "jest --watch", + "test:ci": "vitest run --coverage", + "test:coverage": "vitest --coverage", + "test:watch": "vitest", "type-check": "tsc --build" }, "dependencies": { "@votingworks/test-utils": "workspace:*" }, "devDependencies": { - "@types/jest": "^29.5.3", + "@vitest/coverage-istanbul": "^2.1.8", "@votingworks/grout": "workspace:*", "eslint-plugin-vx": "workspace:*", "expect-type": "^0.15.0", "is-ci-cli": "2.2.0", - "jest": "^29.6.2", - "jest-junit": "^16.0.0", - "jest-watch-typeahead": "^2.2.2", "lint-staged": "11.0.0", "sort-package-json": "^1.50.0", - "ts-jest": "29.1.1" + "vitest": "^2.1.8" }, "peerDependencies": { "@votingworks/grout": "workspace:*" diff --git a/libs/grout/test-utils/src/mock_client.test.ts b/libs/grout/test-utils/src/mock_client.test.ts index fd2874f3c2..aac0a98a0e 100644 --- a/libs/grout/test-utils/src/mock_client.test.ts +++ b/libs/grout/test-utils/src/mock_client.test.ts @@ -1,3 +1,4 @@ +import { expect, test, vi } from 'vitest'; import { MockFunction } from '@votingworks/test-utils'; import { expectTypeOf } from 'expect-type'; import { createApi, createClient } from '@votingworks/grout'; @@ -32,12 +33,12 @@ test('catches exceptions from mock function failures and logs them', async () => const mockClient = createMockClient({ catchUnexpectedErrors: true, }); - const consoleErrorMock = jest.fn(); + const consoleErrorMock = vi.fn(); // eslint-disable-next-line no-console console.error = consoleErrorMock; await mockClient.add({ num1: 1, num2: 2 }); expect(consoleErrorMock).toHaveBeenCalledTimes(1); - expect(consoleErrorMock.mock.calls[0][0]).toMatchInlineSnapshot( + expect(consoleErrorMock.mock.calls[0]![0]).toMatchInlineSnapshot( `"Unexpected call to mock function: add({ num1: 1, num2: 2 })"` ); }); @@ -46,7 +47,7 @@ test('doesnt catch intentional exceptions from mock functions', () => { const mockClient = createMockClient({ catchUnexpectedErrors: true, }); - const consoleErrorMock = jest.fn(); + const consoleErrorMock = vi.fn(); // eslint-disable-next-line no-console console.error = consoleErrorMock; mockClient.add @@ -66,10 +67,10 @@ test('asserts complete for all methods', async () => { await expect(mockClient.add({ num1: 1, num2: 2 })).resolves.toEqual(42); expect(() => mockClient.assertComplete()).toThrowErrorMatchingInlineSnapshot(` - "Mismatch between expected mock function calls and actual mock function calls: + [Error: Mismatch between expected mock function calls and actual mock function calls: Call #0 Expected: sqrt({ num: 4 }) - Actual: " + Actual: ] `); }); diff --git a/libs/grout/test-utils/src/mock_client.ts b/libs/grout/test-utils/src/mock_client.ts index 9d4ad8d101..66b3a92016 100644 --- a/libs/grout/test-utils/src/mock_client.ts +++ b/libs/grout/test-utils/src/mock_client.ts @@ -29,9 +29,9 @@ type AnyMockFunction = MockFunction; function createSafeMockMethod(methodName: string): AnyMockFunction { // API methods are sometimes called without exception handling, which can - // cause cause jest to exit early and swallow the error. So we wrap the - // mock method in a proxy that catches any exceptions and logs them to the - // console instead. + // cause cause tests to exit early and swallow the error. So we wrap the mock + // method in a proxy that catches any exceptions and logs them to the console + // instead. return new Proxy(mockFunction(methodName), { apply: (target, thisArg, args) => { try { diff --git a/libs/grout/test-utils/vitest.config.ts b/libs/grout/test-utils/vitest.config.ts new file mode 100644 index 0000000000..0489cf29ba --- /dev/null +++ b/libs/grout/test-utils/vitest.config.ts @@ -0,0 +1,3 @@ +import { defineConfig } from '../../../vitest.config.shared.mjs'; + +export default defineConfig(); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 46e95aeb37..c687121365 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4262,9 +4262,9 @@ importers: specifier: workspace:* version: link:../../test-utils devDependencies: - '@types/jest': - specifier: ^29.5.3 - version: 29.5.3 + '@vitest/coverage-istanbul': + specifier: ^2.1.8 + version: 2.1.8(vitest@2.1.8) '@votingworks/grout': specifier: workspace:* version: link:.. @@ -4277,24 +4277,15 @@ importers: is-ci-cli: specifier: 2.2.0 version: 2.2.0 - jest: - specifier: ^29.6.2 - version: 29.6.2(@types/node@20.16.0) - jest-junit: - specifier: ^16.0.0 - version: 16.0.0 - jest-watch-typeahead: - specifier: ^2.2.2 - version: 2.2.2(jest@29.6.2) lint-staged: specifier: 11.0.0 version: 11.0.0 sort-package-json: specifier: ^1.50.0 version: 1.53.1 - ts-jest: - specifier: 29.1.1 - version: 29.1.1(@babel/core@7.26.0)(@jest/types@29.6.1)(esbuild@0.18.17)(jest@29.6.2)(typescript@5.6.2) + vitest: + specifier: ^2.1.8 + version: 2.1.8 libs/hmpb: dependencies: