From 51d50027d7938c594d17a79c59da17d76c3f9d9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rge=20N=C3=A6ss?= Date: Thu, 19 Sep 2024 14:51:08 +0200 Subject: [PATCH] chore(migrate): migrate tests from jest to vitest --- packages/@sanity/migrate/jest.config.cjs | 9 ------ packages/@sanity/migrate/package.json | 6 ++-- .../fetch-utils/__test__/assert2xx.test.ts | 32 +++++++++++-------- .../__test__/bufferThroughFile.test.ts | 4 +-- .../src/it-utils/__test__/decodeText.test.ts | 2 +- .../src/it-utils/__test__/json.test.ts | 2 +- .../src/it-utils/__test__/split.test.ts | 2 +- .../src/mutations/__tests__/mutate.test.ts | 2 +- .../normalizeMigrationDefinition.test.ts | 13 +++----- .../utils/__tests__/batchMutations.test.ts | 2 +- .../utils/__tests__/toSanityMutations.test.ts | 12 +++---- .../__test__/fromExportArchive.test.ts | 2 +- .../tar-webstream/__test__/invalid.test.ts | 8 ++--- .../src/tar-webstream/__test__/movies.test.ts | 2 +- .../src/tar-webstream/__test__/small.test.ts | 2 +- packages/@sanity/migrate/vitest.config.ts | 14 ++++++++ 16 files changed, 59 insertions(+), 55 deletions(-) delete mode 100644 packages/@sanity/migrate/jest.config.cjs create mode 100644 packages/@sanity/migrate/vitest.config.ts diff --git a/packages/@sanity/migrate/jest.config.cjs b/packages/@sanity/migrate/jest.config.cjs deleted file mode 100644 index 073a08fcc23..00000000000 --- a/packages/@sanity/migrate/jest.config.cjs +++ /dev/null @@ -1,9 +0,0 @@ -'use strict' - -const {createJestConfig} = require('../../../test/config.cjs') - -module.exports = createJestConfig({ - displayName: require('./package.json').name, - testEnvironment: 'node', - transformIgnorePatterns: ['node_modules/(?!(uint8array-extras)/)'], -}) diff --git a/packages/@sanity/migrate/package.json b/packages/@sanity/migrate/package.json index 33c4f74d323..250c5dfa7aa 100644 --- a/packages/@sanity/migrate/package.json +++ b/packages/@sanity/migrate/package.json @@ -46,7 +46,7 @@ "clean": "rimraf lib coverage", "lint": "eslint .", "prepublishOnly": "turbo run build", - "test": "jest", + "test": "vitest", "watch": "pkg-utils watch" }, "dependencies": { @@ -61,11 +61,11 @@ "p-map": "^7.0.1" }, "devDependencies": { - "@jest/globals": "^29.7.0", "@repo/package.config": "workspace:*", "@types/arrify": "^2.0.1", "@types/debug": "^4.1.12", - "rimraf": "^3.0.2" + "rimraf": "^3.0.2", + "vitest": "^2.1.1" }, "engines": { "node": ">=18" diff --git a/packages/@sanity/migrate/src/fetch-utils/__test__/assert2xx.test.ts b/packages/@sanity/migrate/src/fetch-utils/__test__/assert2xx.test.ts index 8397f2cd1ab..804a5998ddb 100644 --- a/packages/@sanity/migrate/src/fetch-utils/__test__/assert2xx.test.ts +++ b/packages/@sanity/migrate/src/fetch-utils/__test__/assert2xx.test.ts @@ -1,4 +1,4 @@ -import {expect, test} from '@jest/globals' +import {expect, test} from 'vitest' import {assert2xx} from '../fetchStream' @@ -25,10 +25,12 @@ test('server responds with 4xx and error response', () => { message: 'More details', }), } - expect(assert2xx(mockResponse as unknown as Response)).rejects.toThrowError({ - statusCode: 400, - message: 'Error message: More details', - }) + expect(assert2xx(mockResponse as unknown as Response)).rejects.toThrowError( + expect.objectContaining({ + statusCode: 400, + message: 'Error message: More details', + }), + ) }) test('server responds with 5xx and no json response', () => { @@ -37,10 +39,12 @@ test('server responds with 5xx and no json response', () => { statusText: 'Internal Server Error', json: () => Promise.reject(new Error('Failed to parse JSON')), } - expect(assert2xx(mockResponse as unknown as Response)).rejects.toThrowError({ - statusCode: 500, - message: 'HTTP Error 500: Internal Server Error', - }) + expect(assert2xx(mockResponse as unknown as Response)).rejects.toThrowError( + expect.objectContaining({ + statusCode: 500, + message: 'HTTP Error 500: Internal Server Error', + }), + ) }) test('server responds with 5xx and json response', () => { @@ -56,8 +60,10 @@ test('server responds with 5xx and json response', () => { status: 500, }), } - expect(assert2xx(mockResponse as unknown as Response)).rejects.toThrowError({ - statusCode: 500, - message: 'validationError: Document is not of valid type', - }) + expect(assert2xx(mockResponse as unknown as Response)).rejects.toThrowError( + expect.objectContaining({ + statusCode: 500, + message: 'validationError: Document is not of valid type', + }), + ) }) diff --git a/packages/@sanity/migrate/src/fs-webstream/__test__/bufferThroughFile.test.ts b/packages/@sanity/migrate/src/fs-webstream/__test__/bufferThroughFile.test.ts index c5f7f55cbe2..9838e380f30 100644 --- a/packages/@sanity/migrate/src/fs-webstream/__test__/bufferThroughFile.test.ts +++ b/packages/@sanity/migrate/src/fs-webstream/__test__/bufferThroughFile.test.ts @@ -2,7 +2,7 @@ import {stat} from 'node:fs/promises' import path from 'node:path' -import {describe, expect, test} from '@jest/globals' +import {describe, expect, test} from 'vitest' import {decodeText, parse} from '../../it-utils' import {firstValueFrom} from '../../it-utils/firstValueFrom' @@ -200,7 +200,7 @@ describe('using secondary stream', () => { await expect(() => lastValueFrom(parse(decodeText(streamToAsyncIterator(createReader())))), ).rejects.toThrowErrorMatchingInlineSnapshot( - `"Cannot create new buffered readers on aborted stream"`, + `[Error: Cannot create new buffered readers on aborted stream]`, ) }) }) diff --git a/packages/@sanity/migrate/src/it-utils/__test__/decodeText.test.ts b/packages/@sanity/migrate/src/it-utils/__test__/decodeText.test.ts index 2e02bddd375..637ae8373fa 100644 --- a/packages/@sanity/migrate/src/it-utils/__test__/decodeText.test.ts +++ b/packages/@sanity/migrate/src/it-utils/__test__/decodeText.test.ts @@ -1,4 +1,4 @@ -import {expect, test} from '@jest/globals' +import {expect, test} from 'vitest' import {decodeText} from '../decodeText' import {toArray} from '../toArray' diff --git a/packages/@sanity/migrate/src/it-utils/__test__/json.test.ts b/packages/@sanity/migrate/src/it-utils/__test__/json.test.ts index 27ce4edab92..0a18c3830fc 100644 --- a/packages/@sanity/migrate/src/it-utils/__test__/json.test.ts +++ b/packages/@sanity/migrate/src/it-utils/__test__/json.test.ts @@ -1,4 +1,4 @@ -import {expect, test} from '@jest/globals' +import {expect, test} from 'vitest' import {parseJSON} from '../json' diff --git a/packages/@sanity/migrate/src/it-utils/__test__/split.test.ts b/packages/@sanity/migrate/src/it-utils/__test__/split.test.ts index 275882c0fb8..aa970d00e18 100644 --- a/packages/@sanity/migrate/src/it-utils/__test__/split.test.ts +++ b/packages/@sanity/migrate/src/it-utils/__test__/split.test.ts @@ -1,4 +1,4 @@ -import {expect, test} from '@jest/globals' +import {expect, test} from 'vitest' import {split} from '../split' diff --git a/packages/@sanity/migrate/src/mutations/__tests__/mutate.test.ts b/packages/@sanity/migrate/src/mutations/__tests__/mutate.test.ts index d760638ceb4..5f583aa9a55 100644 --- a/packages/@sanity/migrate/src/mutations/__tests__/mutate.test.ts +++ b/packages/@sanity/migrate/src/mutations/__tests__/mutate.test.ts @@ -1,4 +1,4 @@ -import {expect, test} from '@jest/globals' +import {expect, test} from 'vitest' import {at, create, createIfNotExists, createOrReplace, del, patch} from '../creators' import {inc, insert, set, setIfMissing, unset} from '../operations/creators' diff --git a/packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts b/packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts index 15578ab1c82..90a38dcac17 100644 --- a/packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts +++ b/packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts @@ -1,13 +1,8 @@ -import {describe, expect, it, jest} from '@jest/globals' import {type SanityDocument} from '@sanity/types' +import {describe, expect, it, vitest} from 'vitest' import {createIfNotExists} from '../../mutations' -import { - type DocumentMigrationReturnValue, - type Migration, - type MigrationContext, - type NodeMigration, -} from '../../types' +import {type Migration, type MigrationContext, type NodeMigration} from '../../types' import { createAsyncIterableMutation, normalizeMigrateDefinition, @@ -43,7 +38,7 @@ describe('#normalizeMigrateDefinition', () => { const result = normalizeMigrateDefinition(mockMigration) const res = [] - for await (const item of result(jest.fn() as any, {} as any)) { + for await (const item of result(vitest.fn(), {} as any)) { res.push(item) } @@ -96,7 +91,7 @@ describe('#normalizeMigrateDefinition', () => { describe('#createAsyncIterableMutation', () => { it('should return an async iterable', async () => { const mockMigration: NodeMigration = { - document: jest.fn<() => DocumentMigrationReturnValue>(), + document: vitest.fn(), } const iterable = createAsyncIterableMutation(mockMigration, {documentTypes: ['foo']}) diff --git a/packages/@sanity/migrate/src/runner/utils/__tests__/batchMutations.test.ts b/packages/@sanity/migrate/src/runner/utils/__tests__/batchMutations.test.ts index fe3f62f14e5..50cd8a6a751 100644 --- a/packages/@sanity/migrate/src/runner/utils/__tests__/batchMutations.test.ts +++ b/packages/@sanity/migrate/src/runner/utils/__tests__/batchMutations.test.ts @@ -1,4 +1,4 @@ -import {describe, expect, test} from '@jest/globals' +import {describe, expect, test} from 'vitest' import {toArray} from '../../../it-utils' import {batchMutations} from '../batchMutations' diff --git a/packages/@sanity/migrate/src/runner/utils/__tests__/toSanityMutations.test.ts b/packages/@sanity/migrate/src/runner/utils/__tests__/toSanityMutations.test.ts index 6ed39b0bd1c..e47edc17c88 100644 --- a/packages/@sanity/migrate/src/runner/utils/__tests__/toSanityMutations.test.ts +++ b/packages/@sanity/migrate/src/runner/utils/__tests__/toSanityMutations.test.ts @@ -1,6 +1,6 @@ /* eslint-disable simple-import-sort/imports */ // Note: for some reason, this needs to be imported before the mocked module -import {afterEach, describe, expect, it, jest} from '@jest/globals' +import {afterEach, describe, expect, it, vitest} from 'vitest' /* eslint-enable simple-import-sort/imports */ import {SanityEncoder} from '@sanity/mutate' @@ -8,22 +8,20 @@ import {SanityEncoder} from '@sanity/mutate' import {type Mutation, type Transaction} from '../../../mutations' import {toSanityMutations, type TransactionPayload} from '../toSanityMutations' -jest.mock('@sanity/mutate', () => { +vitest.mock('@sanity/mutate', async () => { // eslint-disable-next-line @typescript-eslint/consistent-type-imports - const actual = jest.requireActual('@sanity/mutate') + const actual = await vitest.importActual('@sanity/mutate') return { ...actual, SanityEncoder: { ...actual.SanityEncoder, - encodeAll: jest - .fn() - .mockImplementation(actual.SanityEncoder.encodeAll), + encodeAll: vitest.fn().mockImplementation(actual.SanityEncoder.encodeAll), }, } }) afterEach(() => { - jest.clearAllMocks() + vitest.clearAllMocks() }) describe('#toSanityMutations', () => { diff --git a/packages/@sanity/migrate/src/sources/__test__/fromExportArchive.test.ts b/packages/@sanity/migrate/src/sources/__test__/fromExportArchive.test.ts index a87c3b6878c..fbdbfc11778 100644 --- a/packages/@sanity/migrate/src/sources/__test__/fromExportArchive.test.ts +++ b/packages/@sanity/migrate/src/sources/__test__/fromExportArchive.test.ts @@ -1,4 +1,4 @@ -import {expect, test} from '@jest/globals' +import {expect, test} from 'vitest' import {decodeText, parse, toArray} from '../../it-utils' import {fromExportArchive} from '../fromExportArchive' diff --git a/packages/@sanity/migrate/src/tar-webstream/__test__/invalid.test.ts b/packages/@sanity/migrate/src/tar-webstream/__test__/invalid.test.ts index dc725addbff..0b637779447 100644 --- a/packages/@sanity/migrate/src/tar-webstream/__test__/invalid.test.ts +++ b/packages/@sanity/migrate/src/tar-webstream/__test__/invalid.test.ts @@ -1,4 +1,4 @@ -import {expect, test} from '@jest/globals' +import {expect, test} from 'vitest' import {readFileAsWebStream} from '../../fs-webstream/readFileAsWebStream' import {streamToAsyncIterator} from '../../utils/streamToAsyncIterator' @@ -19,7 +19,7 @@ test('untar an empty tar file', async () => { } } }).rejects.toThrowErrorMatchingInlineSnapshot( - `"Unexpected end of tar file. Expected 512 bytes of headers."`, + `[Error: Unexpected end of tar file. Expected 512 bytes of headers.]`, ) }) @@ -31,7 +31,7 @@ test('untar an invalid tar file of > 512b', async () => { } } }).rejects.toThrowErrorMatchingInlineSnapshot( - `"Invalid tar header. Maybe the tar is corrupted or it needs to be gunzipped?"`, + `[Error: Invalid tar header. Maybe the tar is corrupted or it needs to be gunzipped?]`, ) }) @@ -43,6 +43,6 @@ test('untar a corrupted tar file', async () => { } } }).rejects.toThrowErrorMatchingInlineSnapshot( - `"Invalid tar header. Maybe the tar is corrupted or it needs to be gunzipped?"`, + `[Error: Invalid tar header. Maybe the tar is corrupted or it needs to be gunzipped?]`, ) }) diff --git a/packages/@sanity/migrate/src/tar-webstream/__test__/movies.test.ts b/packages/@sanity/migrate/src/tar-webstream/__test__/movies.test.ts index bc20a535f4a..27e0a83f400 100644 --- a/packages/@sanity/migrate/src/tar-webstream/__test__/movies.test.ts +++ b/packages/@sanity/migrate/src/tar-webstream/__test__/movies.test.ts @@ -1,4 +1,4 @@ -import {expect, test} from '@jest/globals' +import {expect, test} from 'vitest' import {readFileAsWebStream} from '../../fs-webstream/readFileAsWebStream' import {toArray} from '../../it-utils/toArray' diff --git a/packages/@sanity/migrate/src/tar-webstream/__test__/small.test.ts b/packages/@sanity/migrate/src/tar-webstream/__test__/small.test.ts index fa91f9f2a50..d3cf40e40eb 100644 --- a/packages/@sanity/migrate/src/tar-webstream/__test__/small.test.ts +++ b/packages/@sanity/migrate/src/tar-webstream/__test__/small.test.ts @@ -1,4 +1,4 @@ -import {expect, test} from '@jest/globals' +import {expect, test} from 'vitest' import {readFileAsWebStream} from '../../fs-webstream/readFileAsWebStream' import {decodeText} from '../../it-utils/decodeText' diff --git a/packages/@sanity/migrate/vitest.config.ts b/packages/@sanity/migrate/vitest.config.ts new file mode 100644 index 00000000000..6549d830962 --- /dev/null +++ b/packages/@sanity/migrate/vitest.config.ts @@ -0,0 +1,14 @@ +import {configDefaults, defineConfig} from 'vitest/config' + +import {getAliases} from '../../../vitest-aliases' + +export default defineConfig({ + test: { + alias: getAliases(), + typecheck: { + exclude: [...(configDefaults.typecheck?.exclude || []), '.tmp/**', './lib/**'], + }, + exclude: [...configDefaults.exclude, '.tmp/**', './lib/**'], + includeSource: ['./src/**/*.ts'], + }, +})