Skip to content

Commit

Permalink
chore(migrate): migrate tests from jest to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Sep 19, 2024
1 parent 4729e84 commit 51d5002
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 55 deletions.
9 changes: 0 additions & 9 deletions packages/@sanity/migrate/jest.config.cjs

This file was deleted.

6 changes: 3 additions & 3 deletions packages/@sanity/migrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"clean": "rimraf lib coverage",
"lint": "eslint .",
"prepublishOnly": "turbo run build",
"test": "jest",
"test": "vitest",
"watch": "pkg-utils watch"
},
"dependencies": {
Expand All @@ -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"
Expand Down
32 changes: 19 additions & 13 deletions packages/@sanity/migrate/src/fetch-utils/__test__/assert2xx.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expect, test} from '@jest/globals'
import {expect, test} from 'vitest'

import {assert2xx} from '../fetchStream'

Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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',
}),
)
})
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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]`,
)
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expect, test} from '@jest/globals'
import {expect, test} from 'vitest'

import {decodeText} from '../decodeText'
import {toArray} from '../toArray'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expect, test} from '@jest/globals'
import {expect, test} from 'vitest'

import {parseJSON} from '../json'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expect, test} from '@jest/globals'
import {expect, test} from 'vitest'

import {split} from '../split'

Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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']})
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
/* 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'

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<typeof import('@sanity/mutate')>('@sanity/mutate')
const actual = await vitest.importActual<typeof import('@sanity/mutate')>('@sanity/mutate')
return {
...actual,
SanityEncoder: {
...actual.SanityEncoder,
encodeAll: jest
.fn<typeof actual.SanityEncoder.encodeAll>()
.mockImplementation(actual.SanityEncoder.encodeAll),
encodeAll: vitest.fn().mockImplementation(actual.SanityEncoder.encodeAll),
},
}
})

afterEach(() => {
jest.clearAllMocks()
vitest.clearAllMocks()
})

describe('#toSanityMutations', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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.]`,
)
})

Expand All @@ -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?]`,
)
})

Expand All @@ -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?]`,
)
})
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
14 changes: 14 additions & 0 deletions packages/@sanity/migrate/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -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'],
},
})

0 comments on commit 51d5002

Please sign in to comment.