-
Notifications
You must be signed in to change notification settings - Fork 991
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(api): Switch to use vitest over jest (#9853)
We know that jest will likely become troublesome for us internally and in individual user projects as we move forward with our epoch agenda. This PR switches out just one package `api` to use vitest over jest for the unit tests. The majority of the changes here are simply explicitly importing vitest testing utilities. Some notable points: 1. Style of using explicit imports over globals. I personally prefer being explicit and have done that here. There is a note from vitest that when not using globals other third party testing libraries might not work (see: https://vitest.dev/guide/migration.html#globals-as-a-default). We don't use them here so we do not need to worry about that. 2. Hook execution is by default slightly different in vitest. They run hooks like `beforeEach` in parallel rather than in series like jest would (see: https://vitest.dev/guide/migration.html#hooks). I have configured vitest to behave like jest and run them in series. We are free to revisit this decision in the future - we would have to go through the usage of each hook and confirm there are no cases where running them in parallel would cause undesirable side effects. 3. In this package there were some `__mocks__` present. Vitest does not load these by default and they must be explicitly loaded via `vi.mock(...)`. See: https://vitest.dev/guide/migration.html#auto-mocking-behaviour. It appears that for this package these mocks actually served no purpose so I have removed them here anyway. 4. CLI options are slightly different between jest and vitest. Vitest has no `--colors` like jest (see: https://jestjs.io/docs/cli#--colors). I have removed our usage of this option as I don't think we will consider losing colors in non-tty environments a blocker. `maxWorkers` is a CLI option in both vitest and jest however it appears that if you wish to use it in vitest you must also specify a `minWorkers`. I have updated appropriately with a `minWorkers=1`. Specific test change: 1. I had to update the prisma client mock in `packages/api/src/cache/__tests__/cacheFindMany.test.ts`. There already exists a note within the appropriate source code that the `PrismaClientValidationError` is not available until the prisma client has been generated. I have added a mock `PrismaClientValidationError` error and the tests pass as they used to.
- Loading branch information
1 parent
d78c1cd
commit 4b541be
Showing
28 changed files
with
935 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/api/src/auth/__tests__/getAuthenticationContext.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/api/src/auth/verifiers/__tests__/base64Sha1Verifier.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/api/src/auth/verifiers/__tests__/base64Sha256Verifier.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/api/src/auth/verifiers/__tests__/jwtVerifier.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { describe, test, expect } from 'vitest' | ||
|
||
import { | ||
createVerifier, | ||
WebhookSignError, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/api/src/auth/verifiers/__tests__/sha1Verifier.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/api/src/auth/verifiers/__tests__/sha256Verifier.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/api/src/auth/verifiers/__tests__/timestampSchemeVerifier.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { describe, it, expect, vi, beforeEach } from 'vitest' | ||
|
||
import * as ValidationErrors from '../errors' | ||
import { | ||
validate, | ||
|
@@ -1153,7 +1155,7 @@ describe('validate', () => { | |
|
||
describe('validateWithSync', () => { | ||
it('runs a custom function as a validation', () => { | ||
const validateFunction = jest.fn() | ||
const validateFunction = vi.fn() | ||
validateWithSync(validateFunction) | ||
|
||
expect(validateFunction).toBeCalledWith() | ||
|
@@ -1186,7 +1188,7 @@ describe('validateWithSync', () => { | |
|
||
describe('validateWith', () => { | ||
it('runs a custom function as a validation', () => { | ||
const validateFunction = jest.fn() | ||
const validateFunction = vi.fn() | ||
validateWith(validateFunction) | ||
|
||
expect(validateFunction).toBeCalledWith() | ||
|
@@ -1220,9 +1222,9 @@ describe('validateWith', () => { | |
// the actual methods of an instance of the class | ||
// | ||
// mockFindFirst.mockImplementation() to change what `findFirst()` would return | ||
const mockFindFirst = jest.fn() | ||
jest.mock('@prisma/client', () => ({ | ||
PrismaClient: jest.fn(() => ({ | ||
const mockFindFirst = vi.fn() | ||
vi.mock('@prisma/client', () => ({ | ||
PrismaClient: vi.fn(() => ({ | ||
$transaction: async (func) => | ||
func({ | ||
user: { | ||
|
@@ -1309,7 +1311,7 @@ describe('validateUniqueness', () => { | |
}) | ||
|
||
it('uses the given prisma client', async () => { | ||
const mockFindFirstOther = jest.fn() | ||
const mockFindFirstOther = vi.fn() | ||
mockFindFirstOther.mockImplementation(() => ({ | ||
id: 2, | ||
email: '[email protected]', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { defineConfig } from 'vitest/config' | ||
|
||
export default defineConfig({ | ||
test: { | ||
sequence: { | ||
hooks: 'list', | ||
}, | ||
setupFiles: [ | ||
'./vitest.setup.mts' | ||
], | ||
logHeapUsage: true, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
module.exports = {} | ||
|
||
// Set the default webhook secret for all tests | ||
process.env = Object.assign(process.env, { | ||
WEBHOOK_SECRET: 'MY_VOICE_IS_MY_PASSPORT_VERIFY_ME', | ||
}) |
Oops, something went wrong.