Skip to content

Commit 3c63eee

Browse files
committed
chore(test): move @sanity/migrate to vitest
1 parent 44dee81 commit 3c63eee

File tree

18 files changed

+66
-61
lines changed

18 files changed

+66
-61
lines changed

packages/@sanity/migrate/.eslintrc.cjs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict'
2-
3-
const path = require('path')
1+
const path = require('node:path')
42

53
const ROOT_PATH = path.resolve(__dirname, '../../..')
64

packages/@sanity/migrate/jest.config.cjs

-9
This file was deleted.

packages/@sanity/migrate/package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"clean": "rimraf lib coverage",
4747
"lint": "eslint .",
4848
"prepublishOnly": "turbo run build",
49-
"test": "jest",
49+
"test": "vitest",
5050
"watch": "pkg-utils watch"
5151
},
5252
"dependencies": {
@@ -61,11 +61,12 @@
6161
"p-map": "^7.0.1"
6262
},
6363
"devDependencies": {
64-
"@jest/globals": "^29.7.0",
6564
"@repo/package.config": "workspace:*",
65+
"@repo/test-config": "workspace:*",
6666
"@types/arrify": "^2.0.1",
6767
"@types/debug": "^4.1.12",
68-
"rimraf": "^3.0.2"
68+
"rimraf": "^3.0.2",
69+
"vitest": "^2.1.1"
6970
},
7071
"engines": {
7172
"node": ">=18"

packages/@sanity/migrate/src/fetch-utils/__test__/assert2xx.test.ts

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {expect, test} from '@jest/globals'
1+
import {expect, test} from 'vitest'
22

33
import {assert2xx} from '../fetchStream'
44

@@ -25,10 +25,12 @@ test('server responds with 4xx and error response', () => {
2525
message: 'More details',
2626
}),
2727
}
28-
expect(assert2xx(mockResponse as unknown as Response)).rejects.toThrowError({
29-
statusCode: 400,
30-
message: 'Error message: More details',
31-
})
28+
expect(assert2xx(mockResponse as unknown as Response)).rejects.toThrowError(
29+
expect.objectContaining({
30+
statusCode: 400,
31+
message: 'Error message: More details',
32+
}),
33+
)
3234
})
3335

3436
test('server responds with 5xx and no json response', () => {
@@ -37,10 +39,12 @@ test('server responds with 5xx and no json response', () => {
3739
statusText: 'Internal Server Error',
3840
json: () => Promise.reject(new Error('Failed to parse JSON')),
3941
}
40-
expect(assert2xx(mockResponse as unknown as Response)).rejects.toThrowError({
41-
statusCode: 500,
42-
message: 'HTTP Error 500: Internal Server Error',
43-
})
42+
expect(assert2xx(mockResponse as unknown as Response)).rejects.toThrowError(
43+
expect.objectContaining({
44+
statusCode: 500,
45+
message: 'HTTP Error 500: Internal Server Error',
46+
}),
47+
)
4448
})
4549

4650
test('server responds with 5xx and json response', () => {
@@ -56,8 +60,10 @@ test('server responds with 5xx and json response', () => {
5660
status: 500,
5761
}),
5862
}
59-
expect(assert2xx(mockResponse as unknown as Response)).rejects.toThrowError({
60-
statusCode: 500,
61-
message: 'validationError: Document is not of valid type',
62-
})
63+
expect(assert2xx(mockResponse as unknown as Response)).rejects.toThrowError(
64+
expect.objectContaining({
65+
statusCode: 500,
66+
message: 'validationError: Document is not of valid type',
67+
}),
68+
)
6369
})

packages/@sanity/migrate/src/fs-webstream/__test__/bufferThroughFile.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import {stat} from 'node:fs/promises'
33
import path from 'node:path'
44

5-
import {describe, expect, test} from '@jest/globals'
5+
import {describe, expect, test} from 'vitest'
66

77
import {decodeText, parse} from '../../it-utils'
88
import {firstValueFrom} from '../../it-utils/firstValueFrom'
@@ -200,7 +200,7 @@ describe('using secondary stream', () => {
200200
await expect(() =>
201201
lastValueFrom(parse(decodeText(streamToAsyncIterator(createReader())))),
202202
).rejects.toThrowErrorMatchingInlineSnapshot(
203-
`"Cannot create new buffered readers on aborted stream"`,
203+
`[Error: Cannot create new buffered readers on aborted stream]`,
204204
)
205205
})
206206
})

packages/@sanity/migrate/src/it-utils/__test__/decodeText.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {expect, test} from '@jest/globals'
1+
import {expect, test} from 'vitest'
22

33
import {decodeText} from '../decodeText'
44
import {toArray} from '../toArray'

packages/@sanity/migrate/src/it-utils/__test__/json.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {expect, test} from '@jest/globals'
1+
import {expect, test} from 'vitest'
22

33
import {parseJSON} from '../json'
44

packages/@sanity/migrate/src/it-utils/__test__/split.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {expect, test} from '@jest/globals'
1+
import {expect, test} from 'vitest'
22

33
import {split} from '../split'
44

packages/@sanity/migrate/src/mutations/__tests__/mutate.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {expect, test} from '@jest/globals'
1+
import {expect, test} from 'vitest'
22

33
import {at, create, createIfNotExists, createOrReplace, del, patch} from '../creators'
44
import {inc, insert, set, setIfMissing, unset} from '../operations/creators'

packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
import {describe, expect, it, jest} from '@jest/globals'
21
import {type SanityDocument} from '@sanity/types'
2+
import {describe, expect, it, vitest} from 'vitest'
33

44
import {createIfNotExists} from '../../mutations'
5-
import {
6-
type DocumentMigrationReturnValue,
7-
type Migration,
8-
type MigrationContext,
9-
type NodeMigration,
10-
} from '../../types'
5+
import {type Migration, type MigrationContext, type NodeMigration} from '../../types'
116
import {
127
createAsyncIterableMutation,
138
normalizeMigrateDefinition,
@@ -43,7 +38,7 @@ describe('#normalizeMigrateDefinition', () => {
4338
const result = normalizeMigrateDefinition(mockMigration)
4439

4540
const res = []
46-
for await (const item of result(jest.fn() as any, {} as any)) {
41+
for await (const item of result(vitest.fn(), {} as any)) {
4742
res.push(item)
4843
}
4944

@@ -96,7 +91,7 @@ describe('#normalizeMigrateDefinition', () => {
9691
describe('#createAsyncIterableMutation', () => {
9792
it('should return an async iterable', async () => {
9893
const mockMigration: NodeMigration = {
99-
document: jest.fn<() => DocumentMigrationReturnValue>(),
94+
document: vitest.fn(),
10095
}
10196

10297
const iterable = createAsyncIterableMutation(mockMigration, {documentTypes: ['foo']})

packages/@sanity/migrate/src/runner/utils/__tests__/batchMutations.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {describe, expect, test} from '@jest/globals'
1+
import {describe, expect, test} from 'vitest'
22

33
import {toArray} from '../../../it-utils'
44
import {batchMutations} from '../batchMutations'

packages/@sanity/migrate/src/runner/utils/__tests__/toSanityMutations.test.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
/* eslint-disable simple-import-sort/imports */
22
// Note: for some reason, this needs to be imported before the mocked module
3-
import {afterEach, describe, expect, it, jest} from '@jest/globals'
3+
import {afterEach, describe, expect, it, vitest} from 'vitest'
44
/* eslint-enable simple-import-sort/imports */
55

66
import {SanityEncoder} from '@sanity/mutate'
77

88
import {type Mutation, type Transaction} from '../../../mutations'
99
import {toSanityMutations, type TransactionPayload} from '../toSanityMutations'
1010

11-
jest.mock('@sanity/mutate', () => {
11+
vitest.mock('@sanity/mutate', async () => {
1212
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
13-
const actual = jest.requireActual<typeof import('@sanity/mutate')>('@sanity/mutate')
13+
const actual = await vitest.importActual<typeof import('@sanity/mutate')>('@sanity/mutate')
1414
return {
1515
...actual,
1616
SanityEncoder: {
1717
...actual.SanityEncoder,
18-
encodeAll: jest
19-
.fn<typeof actual.SanityEncoder.encodeAll>()
20-
.mockImplementation(actual.SanityEncoder.encodeAll),
18+
encodeAll: vitest.fn().mockImplementation(actual.SanityEncoder.encodeAll),
2119
},
2220
}
2321
})
2422

2523
afterEach(() => {
26-
jest.clearAllMocks()
24+
vitest.clearAllMocks()
2725
})
2826

2927
describe('#toSanityMutations', () => {

packages/@sanity/migrate/src/sources/__test__/fromExportArchive.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {expect, test} from '@jest/globals'
1+
import {expect, test} from 'vitest'
22

33
import {decodeText, parse, toArray} from '../../it-utils'
44
import {fromExportArchive} from '../fromExportArchive'

packages/@sanity/migrate/src/tar-webstream/__test__/invalid.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {expect, test} from '@jest/globals'
1+
import {expect, test} from 'vitest'
22

33
import {readFileAsWebStream} from '../../fs-webstream/readFileAsWebStream'
44
import {streamToAsyncIterator} from '../../utils/streamToAsyncIterator'
@@ -19,7 +19,7 @@ test('untar an empty tar file', async () => {
1919
}
2020
}
2121
}).rejects.toThrowErrorMatchingInlineSnapshot(
22-
`"Unexpected end of tar file. Expected 512 bytes of headers."`,
22+
`[Error: Unexpected end of tar file. Expected 512 bytes of headers.]`,
2323
)
2424
})
2525

@@ -31,7 +31,7 @@ test('untar an invalid tar file of > 512b', async () => {
3131
}
3232
}
3333
}).rejects.toThrowErrorMatchingInlineSnapshot(
34-
`"Invalid tar header. Maybe the tar is corrupted or it needs to be gunzipped?"`,
34+
`[Error: Invalid tar header. Maybe the tar is corrupted or it needs to be gunzipped?]`,
3535
)
3636
})
3737

@@ -43,6 +43,6 @@ test('untar a corrupted tar file', async () => {
4343
}
4444
}
4545
}).rejects.toThrowErrorMatchingInlineSnapshot(
46-
`"Invalid tar header. Maybe the tar is corrupted or it needs to be gunzipped?"`,
46+
`[Error: Invalid tar header. Maybe the tar is corrupted or it needs to be gunzipped?]`,
4747
)
4848
})

packages/@sanity/migrate/src/tar-webstream/__test__/movies.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {expect, test} from '@jest/globals'
1+
import {expect, test} from 'vitest'
22

33
import {readFileAsWebStream} from '../../fs-webstream/readFileAsWebStream'
44
import {toArray} from '../../it-utils/toArray'

packages/@sanity/migrate/src/tar-webstream/__test__/small.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {expect, test} from '@jest/globals'
1+
import {expect, test} from 'vitest'
22

33
import {readFileAsWebStream} from '../../fs-webstream/readFileAsWebStream'
44
import {decodeText} from '../../it-utils/decodeText'
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {getViteAliases} from '@repo/dev-aliases/vitest'
2+
import {configDefaults, defineConfig} from 'vitest/config'
3+
4+
export default defineConfig({
5+
test: {
6+
alias: getViteAliases(),
7+
typecheck: {
8+
exclude: [...(configDefaults.typecheck?.exclude || []), '.tmp/**', './lib/**'],
9+
},
10+
exclude: [...configDefaults.exclude, '.tmp/**', './lib/**'],
11+
includeSource: ['./src/**/*.ts'],
12+
},
13+
})

pnpm-lock.yaml

+6-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)