Skip to content

Commit

Permalink
chore: migrate node tests to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Nov 1, 2023
1 parent 493efb7 commit b2a8ff3
Show file tree
Hide file tree
Showing 29 changed files with 154 additions and 117 deletions.
22 changes: 0 additions & 22 deletions _old_tsconfig.json

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
"patch:dts": "node \"./config/scripts/patch-ts.js\"",
"check:exports": "node \"./config/scripts/validate-esm.js\"",
"test": "pnpm test:unit && pnpm test:node && pnpm test:browser",
"test:unit": "vitest run",
"test:node": "jest --config=./test/jest.config.js --forceExit",
"test:unit": "vitest",
"test:node": "vitest run --config=./test/node/vitest.config.ts",
"test:browser": "playwright test -c ./test/browser/playwright.config.ts",
"test:modules:node": "jest --config=./test/modules/node/jest.config.js",
"test:modules:browser": "playwright test -c ./test/modules/browser/playwright.config.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/browser/tsconfig.browser.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
// source code under the "src/browser" directory.
"lib": ["DOM", "WebWorker"]
},
"include": ["./global.browser.d.ts", "./**/*.ts"]
"include": ["../../global.d.ts", "./global.browser.d.ts", "./**/*.ts"]
}
3 changes: 3 additions & 0 deletions test/modules/node/node-esm-tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
6 changes: 3 additions & 3 deletions test/node/graphql-api/anonymous-operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ const server = setupServer(graphql.query('GetUser', () => {}))
beforeAll(async () => {
server.listen()
await httpServer.listen()
jest.spyOn(console, 'warn').mockImplementation(() => {})
vi.spyOn(console, 'warn').mockImplementation(() => {})
})

afterEach(() => {
server.resetHandlers()
jest.resetAllMocks()
vi.resetAllMocks()
})

afterAll(async () => {
jest.restoreAllMocks()
vi.restoreAllMocks()
server.close()
await httpServer.close()
})
Expand Down
4 changes: 2 additions & 2 deletions test/node/graphql-api/cookies.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @vitest-environment node
*/
import * as cookieUtils from 'cookie'
import cookieUtils from '@bundled-es-modules/cookie'
import fetch from 'node-fetch'
import { graphql as executeGraphql, buildSchema } from 'graphql'
import { graphql, HttpResponse } from 'msw'
Expand Down Expand Up @@ -70,7 +70,7 @@ test('sets cookie on the mocked response', async () => {
}),
})
const body = await res.json()
const cookieString = res.headers.get('set-cookie')
const cookieString = res.headers.get('set-cookie')!
const responseCookies = cookieUtils.parse(cookieString)

expect(cookieString).toBe('test-cookie=value')
Expand Down
2 changes: 1 addition & 1 deletion test/node/msw-api/auto-update-worker.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as fs from 'fs'
import { execSync } from 'child_process'
import { createTeardown } from 'fs-teardown'
import { fromTemp } from '../../support/utils'
import packageJson from '../../../package.json'
import * as packageJson from '../../../package.json'

const fsMock = createTeardown({
rootDir: fromTemp('auto-update-worker'),
Expand Down
10 changes: 5 additions & 5 deletions test/node/msw-api/cli/init.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ beforeEach(async () => {
})

afterAll(async () => {
jest.restoreAllMocks()
vi.restoreAllMocks()
await fsMock.cleanup()
})

Expand Down Expand Up @@ -170,15 +170,15 @@ test('does not produce eslint errors or warnings', async () => {
test('errors and shuts down if creating a directory fails', async () => {
const init = require('../../../../cli/init')
const error = new Error('Could not create this directory')
jest.spyOn(fs.promises, 'mkdir').mockRejectedValue(error)
vi.spyOn(fs.promises, 'mkdir').mockRejectedValue(error)

const exitSpy = jest.spyOn(process, 'exit').mockImplementationOnce(() => {
const exitSpy = vi.spyOn(process, 'exit').mockImplementationOnce(() => {
throw error
})

const consoleSpy = jest
const consoleSpy = vi
.spyOn(console, 'error')
.mockImplementationOnce(jest.fn())
.mockImplementationOnce(() => void 0)

const publicDir = 'public'

Expand Down
4 changes: 2 additions & 2 deletions test/node/msw-api/req/passthrough.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ beforeAll(async () => {
})

beforeEach(() => {
jest.spyOn(console, 'warn').mockImplementation()
vi.spyOn(console, 'warn').mockImplementation(() => void 0)
})

afterEach(() => {
server.resetHandlers()
jest.restoreAllMocks()
vi.restoreAllMocks()
})

afterAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const httpServer = new HttpServer((app) => {

const server = setupServer()

const listener = jest.fn()
const listener = vi.fn()

function getRequestId(requestStartListener: jest.Mock) {
function getRequestId(requestStartListener: vi.Mock) {
const { calls } = requestStartListener.mock
const requestStartCall = calls.find((call) => {
return call[0].startsWith('[request:start]')
Expand Down Expand Up @@ -78,11 +78,11 @@ beforeAll(async () => {
beforeEach(() => {
// Supress "Expected a mocking resolver function to return a mocked response"
// warnings. Using intentional explicit empty resolver.
jest.spyOn(global.console, 'warn').mockImplementation()
vi.spyOn(global.console, 'warn').mockImplementation(() => void 0)
})

afterEach(() => {
jest.restoreAllMocks()
vi.restoreAllMocks()
})

afterAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ beforeAll(async () => {
})

afterEach(() => {
jest.restoreAllMocks()
vi.restoreAllMocks()
})

afterAll(async () => {
Expand All @@ -36,8 +36,8 @@ afterAll(async () => {

test('removes all listeners attached to the server instance', async () => {
const listeners = {
requestStart: jest.fn(),
requestEnd: jest.fn(),
requestStart: vi.fn(),
requestEnd: vi.fn(),
}
server.events.on('request:start', listeners.requestStart)
server.events.on('request:end', listeners.requestEnd)
Expand All @@ -57,8 +57,8 @@ test('removes all listeners attached to the server instance', async () => {

test('removes all the listeners by the event name', async () => {
const listeners = {
requestStart: jest.fn(),
requestEnd: jest.fn(),
requestStart: vi.fn(),
requestEnd: vi.fn(),
}
server.events.on('request:start', listeners.requestStart)
server.events.on('request:start', listeners.requestStart)
Expand All @@ -73,8 +73,8 @@ test('removes all the listeners by the event name', async () => {

test('does not remove the internal listeners', async () => {
const listeners = {
requestStart: jest.fn(),
responseMocked: jest.fn(),
requestStart: vi.fn(),
responseMocked: vi.fn(),
}

server.events.on('request:start', listeners.requestStart)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ afterAll(async () => {

test('removes a listener by the event name', async () => {
const listeners = {
requestStart: jest.fn(),
requestEnd: jest.fn(),
requestStart: vi.fn(),
requestEnd: vi.fn(),
}
server.events.on('request:start', listeners.requestStart)
server.events.on('request:end', listeners.requestEnd)
Expand Down
4 changes: 2 additions & 2 deletions test/node/msw-api/setup-server/resetHandlers.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const server = setupServer(
)

beforeAll(() => {
jest.spyOn(global.console, 'warn').mockImplementation()
vi.spyOn(global.console, 'warn').mockImplementation(() => void 0)
server.listen()
})

afterAll(() => {
jest.restoreAllMocks()
vi.restoreAllMocks()
server.close()
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ beforeAll(() => server.listen())
afterAll(() => server.close())

test('tolerates fake timers', async () => {
jest.useFakeTimers()
vi.useFakeTimers()

const res = await fetch('https://test.mswjs.io/pull')
const body = await res.json()

jest.useRealTimers()
vi.useRealTimers()

expect(body).toEqual({ status: 'pulled' })
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fetch from 'node-fetch'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'

const log = jest.fn()
const log = vi.fn()

const server = setupServer(
http.get('https://test.mswjs.io/*', () => log('[get] first')),
Expand All @@ -24,7 +24,7 @@ beforeAll(() => {
})

afterEach(() => {
jest.resetAllMocks()
vi.resetAllMocks()
})

afterAll(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*/
import fetch from 'node-fetch'
import { HttpServer } from '@open-draft/test-server/http'
import { setupServer } from 'msw/node'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'

const httpServer = new HttpServer((app) => {
app.get('/', (req, res) => {
Expand All @@ -27,12 +27,12 @@ beforeAll(async () => {
)
server.listen({ onUnhandledRequest: 'bypass' })

jest.spyOn(global.console, 'error').mockImplementation()
jest.spyOn(global.console, 'warn').mockImplementation()
vi.spyOn(global.console, 'error').mockImplementation(() => void 0)
vi.spyOn(global.console, 'warn').mockImplementation(() => void 0)
})

afterAll(async () => {
jest.restoreAllMocks()
vi.restoreAllMocks()
server.close()
await httpServer.close()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ const server = setupServer(

beforeAll(() => {
server.listen()
jest.spyOn(global.console, 'error').mockImplementation()
jest.spyOn(global.console, 'warn').mockImplementation()
vi.spyOn(global.console, 'error').mockImplementation(() => void 0)
vi.spyOn(global.console, 'warn').mockImplementation(() => void 0)
})

afterAll(() => {
server.close()
jest.restoreAllMocks()
vi.restoreAllMocks()
})

test('warns on unhandled requests by default', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ beforeAll(async () => {
})

beforeEach(() => {
jest.spyOn(global.console, 'error').mockImplementation()
jest.spyOn(global.console, 'warn').mockImplementation()
vi.spyOn(global.console, 'error').mockImplementation(() => void 0)
vi.spyOn(global.console, 'warn').mockImplementation(() => void 0)
})

afterEach(() => {
jest.resetAllMocks()
vi.resetAllMocks()
})

afterAll(async () => {
jest.restoreAllMocks()
vi.restoreAllMocks()
server.close()
await httpServer.close()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ const server = setupServer(

beforeAll(() => {
server.listen({ onUnhandledRequest: 'warn' })
jest.spyOn(global.console, 'warn').mockImplementation()
vi.spyOn(global.console, 'warn').mockImplementation(() => void 0)
})

afterAll(() => {
server.close()
jest.restoreAllMocks()
vi.restoreAllMocks()
})

test('warns on unhandled request when using the "warn" value', async () => {
Expand Down
20 changes: 13 additions & 7 deletions test/node/msw-api/setup-server/scenarios/xhr.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,22 @@ describe('given I perform an XMLHttpRequest', () => {
let headers: Headers
let body: string

beforeAll((done) => {
beforeAll(async () => {
const req = new XMLHttpRequest()
req.open('GET', 'http://localhost:3001/resource')
req.onload = function () {
statusCode = this.status
body = JSON.parse(this.response)
headers = stringToHeaders(this.getAllResponseHeaders())
done()
}

const requestFinishPromise = new Promise<void>((resolve, reject) => {
req.onload = function () {
statusCode = this.status
body = JSON.parse(this.response)
headers = stringToHeaders(this.getAllResponseHeaders())
resolve()
}
req.onerror = reject.bind(req)
})
req.send()

await requestFinishPromise
})

test('returns mocked status code', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/node/regressions/many-request-handlers-jsdom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ const server = setupServer(...restHandlers, ...graphqlHanlers)

beforeAll(() => {
server.listen()
jest.spyOn(process.stderr, 'write')
vi.spyOn(process.stderr, 'write')
})

afterAll(() => {
server.close()
jest.restoreAllMocks()
vi.restoreAllMocks()
})

it('does not print a memory leak warning when having many request handlers', async () => {
Expand Down
Loading

0 comments on commit b2a8ff3

Please sign in to comment.