mirror of
https://github.com/hanzoai/msw.git
synced 2026-09-24 17:00:34 +00:00
chore: use vitest for tests (#1817)
This commit is contained in:
committed by
GitHub
parent
5db96e25d2
commit
22da721820
@@ -1,5 +1,5 @@
|
||||
name: 'Bug report (Node.js)'
|
||||
description: I experience unexpected behavior using the library in Node.js (Jest/React Native/Express/etc.).
|
||||
description: I experience unexpected behavior using the library in Node.js (Vitest/React Native/Express/etc.).
|
||||
labels: ['bug', 'scope:node', 'needs:triage']
|
||||
body:
|
||||
- type: markdown
|
||||
|
||||
+1
-4
@@ -7,7 +7,7 @@ Hey! Thank you for deciding to contribute to Mock Service Worker! This page will
|
||||
Getting yourself familiar with the tools below will substantially ease your contribution experience.
|
||||
|
||||
- [TypeScript](https://www.typescriptlang.org/)
|
||||
- [Jest](https://jestjs.io/)
|
||||
- [Vitest](https://vitest.dev/)
|
||||
- [Playwright](https://playwright.dev/)
|
||||
|
||||
## Dependencies
|
||||
@@ -74,8 +74,6 @@ Build the library with the following command:
|
||||
$ pnpm build
|
||||
```
|
||||
|
||||
[jest-url]: https://jestjs.io
|
||||
|
||||
## Tests
|
||||
|
||||
### Testing levels
|
||||
@@ -269,6 +267,5 @@ $ pnpm build
|
||||
```
|
||||
|
||||
[pnpm-url]: https://pnpm.io/
|
||||
[jest-url]: https://jestjs.io
|
||||
[page-with-url]: https://github.com/kettanaito/page-with
|
||||
[pnpm-install-guide-url]: https://pnpm.io/7.x/installation#installing-a-specific-version
|
||||
|
||||
@@ -125,7 +125,7 @@ There's no such thing as Service Workers in Node.js. Instead, MSW implements a [
|
||||
|
||||
### Usage example
|
||||
|
||||
Take a look at the example of an integration test in Jest that uses [React Testing Library](https://github.com/testing-library/react-testing-library) and Mock Service Worker:
|
||||
Take a look at the example of an integration test in Vitest that uses [React Testing Library](https://github.com/testing-library/react-testing-library) and Mock Service Worker:
|
||||
|
||||
```js
|
||||
// test/Dashboard.test.js
|
||||
|
||||
Vendored
-2
@@ -1,5 +1,3 @@
|
||||
declare const SERVICE_WORKER_CHECKSUM: string
|
||||
|
||||
declare module '@bundled-es-modules/cookie' {
|
||||
export * as default from 'cookie'
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
module.exports = {
|
||||
bail: true,
|
||||
roots: ['<rootDir>/src', '<rootDir>/cli'],
|
||||
transform: {
|
||||
'^.+\\.tsx?$': '@swc/jest',
|
||||
},
|
||||
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(j|t)sx?$',
|
||||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
|
||||
setupFiles: ['./jest.setup.js'],
|
||||
moduleNameMapper: {
|
||||
'^~/core(/.*)?$': '<rootDir>/src/core/$1',
|
||||
},
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
const { TextEncoder, TextDecoder } = require('util')
|
||||
|
||||
/**
|
||||
* @note Temporary global polyfills for Jest because it's
|
||||
* ignoring Node.js defaults.
|
||||
*/
|
||||
Object.defineProperties(globalThis, {
|
||||
TextDecoder: { value: TextDecoder },
|
||||
TextEncoder: { value: TextEncoder },
|
||||
})
|
||||
|
||||
const { Blob } = require('buffer')
|
||||
const { Request, Response, Headers, File, FormData } = require('undici')
|
||||
|
||||
Object.defineProperties(globalThis, {
|
||||
Headers: { value: Headers },
|
||||
Request: { value: Request },
|
||||
Response: { value: Response },
|
||||
File: { value: File },
|
||||
Blob: { value: Blob },
|
||||
FormData: { value: FormData },
|
||||
})
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
Object.defineProperty(navigator || {}, 'serviceWorker', {
|
||||
writable: false,
|
||||
value: {
|
||||
addEventListener: () => null,
|
||||
},
|
||||
})
|
||||
}
|
||||
+5
-8
@@ -46,15 +46,14 @@
|
||||
"start": "tsup --watch",
|
||||
"clean": "rimraf ./lib",
|
||||
"lint": "eslint \"{cli,config,src,test}/**/*.ts\"",
|
||||
"prebuild": "rimraf ./lib",
|
||||
"build": "pnpm clean && cross-env NODE_ENV=production tsup && pnpm patch:dts",
|
||||
"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": "cross-env BABEL_ENV=test jest --maxWorkers=3",
|
||||
"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:node": "vitest run --config=./test/modules/node/vitest.config.ts",
|
||||
"test:modules:browser": "playwright test -c ./test/modules/browser/playwright.config.ts",
|
||||
"test:ts": "ts-node test/typings/run.ts",
|
||||
"prepare": "pnpm simple-git-hooks init",
|
||||
@@ -143,11 +142,9 @@
|
||||
"@ossjs/release": "^0.8.0",
|
||||
"@playwright/test": "^1.30.0",
|
||||
"@swc/core": "^1.3.35",
|
||||
"@swc/jest": "^0.2.24",
|
||||
"@types/express": "^4.17.17",
|
||||
"@types/fs-extra": "^9.0.13",
|
||||
"@types/glob": "^8.1.0",
|
||||
"@types/jest": "^29.4.0",
|
||||
"@types/json-bigint": "^1.0.1",
|
||||
"@types/node": "18.x",
|
||||
"@types/node-fetch": "^2.5.11",
|
||||
@@ -170,8 +167,7 @@
|
||||
"fs-extra": "^10.0.0",
|
||||
"fs-teardown": "^0.3.0",
|
||||
"glob": "^9.3.4",
|
||||
"jest": "^29.4.3",
|
||||
"jest-environment-jsdom": "^29.4.3",
|
||||
"jsdom": "^22.1.0",
|
||||
"json-bigint": "^1.0.0",
|
||||
"lint-staged": "^13.0.3",
|
||||
"page-with": "^0.6.1",
|
||||
@@ -184,6 +180,7 @@
|
||||
"typescript": "^5.0.2",
|
||||
"undici": "^5.20.0",
|
||||
"url-loader": "^4.1.1",
|
||||
"vitest": "^0.34.6",
|
||||
"webpack": "^5.68.0",
|
||||
"webpack-dev-server": "^3.11.2",
|
||||
"webpack-http-server": "^0.5.0"
|
||||
|
||||
Generated
+629
-1230
File diff suppressed because it is too large
Load Diff
Vendored
+1
@@ -0,0 +1 @@
|
||||
declare const SERVICE_WORKER_CHECKSUM: string
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { setupWorker } from './setupWorker'
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ describe('resolveStartOptions', () => {
|
||||
|
||||
describe('prepareStartHandler', () => {
|
||||
test('exposes resolved start options to the generated star handler', () => {
|
||||
const createStartHandler = jest.fn()
|
||||
const createStartHandler = vi.fn()
|
||||
const context: SetupWorkerInternalContext = {} as any
|
||||
const startHandler = prepareStartHandler(createStartHandler, context)
|
||||
expect(startHandler).toBeInstanceOf(Function)
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { printStartMessage } from './printStartMessage'
|
||||
|
||||
beforeEach(() => {
|
||||
jest.spyOn(console, 'groupCollapsed').mockImplementation()
|
||||
jest.spyOn(console, 'log').mockImplementation()
|
||||
vi.spyOn(console, 'groupCollapsed').mockImplementation(() => void 0)
|
||||
vi.spyOn(console, 'log').mockImplementation(() => void 0)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
test('prints out a default start message into console', () => {
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import { printStopMessage } from './printStopMessage'
|
||||
|
||||
beforeAll(() => {
|
||||
jest.spyOn(global.console, 'log').mockImplementation()
|
||||
vi.spyOn(global.console, 'log').mockImplementation(() => void 0)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetAllMocks()
|
||||
vi.resetAllMocks()
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
jest.restoreAllMocks()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
test('prints a stop message to the console', () => {
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"extends": "./tsconfig.browser.json",
|
||||
"compilerOptions": {
|
||||
"composite": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../tsconfig.src.json",
|
||||
"compilerOptions": {
|
||||
// Expose browser-specific libraries only for the
|
||||
// source code under the "src/browser" directory.
|
||||
"lib": ["DOM", "WebWorker"]
|
||||
},
|
||||
"include": ["../../global.d.ts", "./global.browser.d.ts", "./**/*.ts"]
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"lib": ["dom", "WebWorker"]
|
||||
},
|
||||
"include": ["./**/*.ts"]
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import { deferNetworkRequestsUntil } from './deferNetworkRequestsUntil'
|
||||
|
||||
beforeAll(() => {
|
||||
// Stubs native `fetch` function to remove any external
|
||||
// asynchronicity from this test suite.
|
||||
window.fetch = jest.fn().mockImplementation(() => {
|
||||
window.fetch = vi.fn().mockImplementation(() => {
|
||||
return Promise.resolve({
|
||||
json: () => ({
|
||||
response: 'body',
|
||||
@@ -16,7 +16,7 @@ beforeAll(() => {
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
jest.restoreAllMocks()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
test('defers any requests that happen while a given promise is pending', async () => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import { getAbsoluteWorkerUrl } from './getAbsoluteWorkerUrl'
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import { TextEncoder } from 'util'
|
||||
import { pruneGetRequestBody } from './pruneGetRequestBody'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { TextEncoder } from 'util'
|
||||
import { HttpResponse } from './HttpResponse'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import { bypass } from './bypass'
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import { encodeBuffer } from '@mswjs/interceptors'
|
||||
import { OperationTypeNode, parse } from 'graphql'
|
||||
@@ -583,7 +583,7 @@ describe('isDocumentNode', () => {
|
||||
|
||||
describe('request', () => {
|
||||
it('has parsed operationName', async () => {
|
||||
const matchAllResolver = jest.fn()
|
||||
const matchAllResolver = vi.fn()
|
||||
const handler = new GraphQLHandler(
|
||||
OperationTypeNode.QUERY,
|
||||
/.*/,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import { HttpHandler, HttpRequestResolverExtras } from './HttpHandler'
|
||||
import { HttpResponse } from '..'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { passthrough } from './passthrough'
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import { Emitter } from 'strict-event-emitter'
|
||||
import { LifeCycleEventsMap, SharedOptions } from '../sharedOptions'
|
||||
@@ -12,16 +12,16 @@ import { HttpResponse } from '../HttpResponse'
|
||||
import { passthrough } from '../passthrough'
|
||||
|
||||
const options: RequiredDeep<SharedOptions> = {
|
||||
onUnhandledRequest: jest.fn(),
|
||||
onUnhandledRequest: vi.fn(),
|
||||
}
|
||||
const callbacks: Partial<Record<keyof HandleRequestOptions, any>> = {
|
||||
onPassthroughResponse: jest.fn(),
|
||||
onMockedResponse: jest.fn(),
|
||||
onPassthroughResponse: vi.fn(),
|
||||
onMockedResponse: vi.fn(),
|
||||
}
|
||||
|
||||
function setup() {
|
||||
const emitter = new Emitter<LifeCycleEventsMap>()
|
||||
const listener = jest.fn()
|
||||
const listener = vi.fn()
|
||||
|
||||
const createMockListener = (name: string) => {
|
||||
return (...args: any) => {
|
||||
@@ -41,11 +41,11 @@ function setup() {
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
jest.spyOn(global.console, 'warn').mockImplementation()
|
||||
vi.spyOn(global.console, 'warn').mockImplementation(() => void 0)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetAllMocks()
|
||||
vi.resetAllMocks()
|
||||
})
|
||||
|
||||
test('returns undefined for a request with the "x-msw-intention" header equal to "bypass"', async () => {
|
||||
@@ -244,8 +244,8 @@ test('returns a transformed response if the "transformResponse" option is provid
|
||||
const transformResponseImpelemntation = (response: Response): Response => {
|
||||
return new Response('transformed', response)
|
||||
}
|
||||
const transformResponse = jest
|
||||
.fn<Response, [Response]>()
|
||||
const transformResponse = vi
|
||||
.fn<[Response], Response>()
|
||||
.mockImplementation(transformResponseImpelemntation)
|
||||
const finalResponse = transformResponseImpelemntation(mockedResponse)
|
||||
const lookupResult = {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import { getCallFrame } from './getCallFrame'
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import { encodeBuffer } from '@mswjs/interceptors'
|
||||
import { OperationTypeNode } from 'graphql'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import { parseMultipartData } from './parseMultipartData'
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ it('pipes events from the source emitter to the destination emitter', () => {
|
||||
const destination = new Emitter()
|
||||
pipeEvents(source, destination)
|
||||
|
||||
const callback = jest.fn()
|
||||
const callback = vi.fn()
|
||||
destination.on('hello', callback)
|
||||
|
||||
source.emit('hello', 'world', { data: true })
|
||||
|
||||
@@ -13,15 +13,17 @@ test('silences exceptions by default', () => {
|
||||
expect(result).toBeUndefined()
|
||||
})
|
||||
|
||||
test('executes a custom callback function when an exception occurs', (done) => {
|
||||
tryCatch(
|
||||
() => {
|
||||
throw new Error('Exception')
|
||||
},
|
||||
(error) => {
|
||||
expect(error).toBeInstanceOf(Error)
|
||||
expect(error.message).toEqual('Exception')
|
||||
done()
|
||||
},
|
||||
)
|
||||
test('executes a custom callback function when an exception occurs', async () => {
|
||||
await new Promise<void>((resolve) => {
|
||||
tryCatch(
|
||||
() => {
|
||||
throw new Error('Exception')
|
||||
},
|
||||
(error) => {
|
||||
expect(error).toBeInstanceOf(Error)
|
||||
expect(error.message).toEqual('Exception')
|
||||
resolve()
|
||||
},
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -3,13 +3,13 @@ import { getTimestamp } from './getTimestamp'
|
||||
beforeAll(() => {
|
||||
// Stub native `Date` prototype methods used in the tested module,
|
||||
// to always produce a predictable value for testing purposes.
|
||||
jest.spyOn(global.Date.prototype, 'getHours').mockImplementation(() => 12)
|
||||
jest.spyOn(global.Date.prototype, 'getMinutes').mockImplementation(() => 4)
|
||||
jest.spyOn(global.Date.prototype, 'getSeconds').mockImplementation(() => 8)
|
||||
vi.spyOn(global.Date.prototype, 'getHours').mockImplementation(() => 12)
|
||||
vi.spyOn(global.Date.prototype, 'getMinutes').mockImplementation(() => 4)
|
||||
vi.spyOn(global.Date.prototype, 'getSeconds').mockImplementation(() => 8)
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
jest.restoreAllMocks()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
test('returns a timestamp string of the invocation time', () => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { encodeBuffer } from '@mswjs/interceptors'
|
||||
import { serializeResponse } from './serializeResponse'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import { coercePath, matchRequestUrl } from './matchRequestUrl'
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { normalizePath } from './normalizePath'
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import { normalizePath } from './normalizePath'
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import { getPublicUrlFromRequest } from './getPublicUrlFromRequest'
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { getRequestCookies } from './getRequestCookies'
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import { getRequestCookies } from './getRequestCookies'
|
||||
import { clearCookies } from '../../../../test/support/utils'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import {
|
||||
onUnhandledRequest,
|
||||
@@ -41,12 +41,12 @@ Read more: https://mswjs.io/docs/getting-started/mocks`,
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
jest.spyOn(console, 'warn').mockImplementation()
|
||||
jest.spyOn(console, 'error').mockImplementation()
|
||||
vi.spyOn(console, 'warn').mockImplementation(() => void 0)
|
||||
vi.spyOn(console, 'error').mockImplementation(() => void 0)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
test('supports the "bypass" request strategy', async () => {
|
||||
@@ -85,11 +85,9 @@ test('supports the "error" request strategy', async () => {
|
||||
})
|
||||
|
||||
test('supports a custom callback function', async () => {
|
||||
const callback = jest.fn<void, Parameters<UnhandledRequestCallback>>(
|
||||
(request) => {
|
||||
console.warn(`callback: ${request.method} ${request.url}`)
|
||||
},
|
||||
)
|
||||
const callback = vi.fn<Parameters<UnhandledRequestCallback>>((request) => {
|
||||
console.warn(`callback: ${request.method} ${request.url}`)
|
||||
})
|
||||
const request = new Request(new URL('/user', 'http://localhost:3000'))
|
||||
await onUnhandledRequest(request, [], callback)
|
||||
|
||||
@@ -106,7 +104,7 @@ test('supports a custom callback function', async () => {
|
||||
})
|
||||
|
||||
test('supports calling default strategies from the custom callback function', async () => {
|
||||
const callback = jest.fn<void, Parameters<UnhandledRequestCallback>>(
|
||||
const callback = vi.fn<Parameters<UnhandledRequestCallback>>(
|
||||
(request, print) => {
|
||||
// Call the default "error" strategy.
|
||||
print.error()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { getAbsoluteUrl } from './getAbsoluteUrl'
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import { getAbsoluteUrl } from './getAbsoluteUrl'
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { isAbsoluteUrl } from './isAbsoluteUrl'
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"extends": "./tsconfig.src.json",
|
||||
"compilerOptions": {
|
||||
"composite": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"extends": "./tsconfig.node.json",
|
||||
"compilerOptions": {
|
||||
"composite": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "./tsconfig.src.json",
|
||||
"compilerOptions": {
|
||||
"types": ["node"]
|
||||
},
|
||||
"include": ["./node", "./native"],
|
||||
"exclude": ["**/*.test.ts"]
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
// Common configuration for everything
|
||||
// living in the "src" directory.
|
||||
"extends": "../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"baseUrl": "./",
|
||||
"paths": {
|
||||
"~/core": ["core"],
|
||||
"~/core/*": ["core/*"]
|
||||
}
|
||||
},
|
||||
"include": ["../global.d.ts", "./**/*.ts"],
|
||||
"exclude": ["./**/*.test.ts"]
|
||||
}
|
||||
+1
-1
@@ -13,7 +13,7 @@ This directory categorizes all test suites based on the library's execution or A
|
||||
Example-driven test consists of two parts:
|
||||
|
||||
- `*.mocks.ts`, a usage example and also a code snippet to test.
|
||||
- `*.test.ts`, an actual test suite for Jest.
|
||||
- `*.test.ts`, an actual test suite for Vitest.
|
||||
|
||||
## Contributing
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import express from 'express'
|
||||
import { test, expect } from '../../../../playwright.extend'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { Response } from '@playwright/test'
|
||||
import { test, expect } from '../../../playwright.extend'
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
/** @type {import('jest').Config} */
|
||||
module.exports = {
|
||||
rootDir: './node',
|
||||
transform: {
|
||||
'^.+\\.ts$': '@swc/jest',
|
||||
},
|
||||
// Limit the concurrency because some tests recompile the library,
|
||||
// creating a moment of time when it has no "lib" files.
|
||||
maxWorkers: 1,
|
||||
moduleNameMapper: {
|
||||
'^msw(.*)': '<rootDir>/../..$1',
|
||||
},
|
||||
testEnvironmentOptions: {
|
||||
// Force JSDOM to use the Node module resolution because we're still in Node.js.
|
||||
// Using browser resolution won't work by design because JSDOM is not a browser
|
||||
// and doesn't ship with 100% compatibility with the browser APIs.
|
||||
// In tests, using browser resolution will result in "ClientRequest" imports
|
||||
// from "@mswjs/interceptors" to not be found because they are not exported
|
||||
// by the browser bundle of that library.
|
||||
customExportConditions: [''],
|
||||
},
|
||||
globals: {
|
||||
fetch,
|
||||
Request,
|
||||
Response,
|
||||
TextEncoder,
|
||||
TextDecoder,
|
||||
},
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
/** @type {import('jest').Config} */
|
||||
module.exports = {
|
||||
rootDir: '.',
|
||||
transform: {
|
||||
'^.+\\.ts$': '@swc/jest',
|
||||
},
|
||||
testEnvironment: 'node',
|
||||
testTimeout: 60_000,
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "../../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"types": ["vitest/globals"]
|
||||
},
|
||||
"include": ["./**/*.test.ts"]
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { defineConfig } from 'vitest/config'
|
||||
|
||||
export default defineConfig({
|
||||
test: {
|
||||
globals: true,
|
||||
dir: __dirname,
|
||||
environment: 'node',
|
||||
testTimeout: 60_000,
|
||||
},
|
||||
})
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { HttpServer } from '@open-draft/test-server/http'
|
||||
@@ -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()
|
||||
})
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @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'
|
||||
@@ -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')
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import type { ExecutionResult } from 'graphql'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { bypass, graphql, HttpResponse } from 'msw'
|
||||
import { setupServer } from 'msw/node'
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
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'),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import * as path from 'path'
|
||||
import * as fs from 'fs-extra'
|
||||
@@ -37,7 +37,7 @@ beforeEach(async () => {
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
jest.restoreAllMocks()
|
||||
vi.restoreAllMocks()
|
||||
await fsMock.cleanup()
|
||||
})
|
||||
|
||||
@@ -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'
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { delay, HttpResponse, http } from 'msw'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { HttpResponse, passthrough, http } from 'msw'
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { http, HttpResponse } from 'msw'
|
||||
import { setupServer } from 'msw/node'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
@@ -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]')
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
@@ -26,7 +26,7 @@ beforeAll(async () => {
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
@@ -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)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { http, graphql } from 'msw'
|
||||
import { setupServer } from 'msw/node'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
@@ -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()
|
||||
})
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import https from 'https'
|
||||
import { http, HttpResponse } from 'msw'
|
||||
|
||||
@@ -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' })
|
||||
})
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
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')),
|
||||
@@ -24,7 +24,7 @@ beforeAll(() => {
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetAllMocks()
|
||||
vi.resetAllMocks()
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'cross-fetch'
|
||||
import { graphql, HttpResponse } from 'msw'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import nodeHttp from 'http'
|
||||
import { HttpServer } from '@open-draft/test-server/http'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import https from 'https'
|
||||
import { HttpServer, httpsAgent } from '@open-draft/test-server/http'
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
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) => {
|
||||
@@ -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()
|
||||
})
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { setupServer } from 'msw/node'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { setupServer } from 'msw/node'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { setupServer } from 'msw/node'
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { HttpServer } from '@open-draft/test-server/http'
|
||||
@@ -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()
|
||||
})
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { setupServer } from 'msw/node'
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { HttpServer } from '@open-draft/test-server/http'
|
||||
import { HttpResponse, http, bypass } from 'msw'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import { http } from 'msw'
|
||||
import { setupServer } from 'msw/node'
|
||||
@@ -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', () => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
* @vitest-environment jsdom
|
||||
*
|
||||
* @note In JSDOM, the "AbortSignal" class is polyfilled instead of
|
||||
* using the Node.js global. Because of that, its instances won't
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { graphql, http, HttpResponse } from 'msw'
|
||||
import { setupServer } from 'msw/node'
|
||||
@@ -25,12 +25,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 () => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
@@ -61,11 +61,15 @@ test('inherits cookies set from a preceeding request', async () => {
|
||||
const res = await fetch(httpServer.https.url('/login'), {
|
||||
method: 'POST',
|
||||
}).then(() => {
|
||||
// Fetch the user after requesting login to see
|
||||
// if the response cookies set in the login request handler
|
||||
// are automatically forwarded to the "GET /user" request.
|
||||
return fetch(httpServer.https.url('/user'))
|
||||
})
|
||||
const json = await res.json()
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
|
||||
const json = await res.json()
|
||||
expect(json).toEqual({
|
||||
firstName: 'John',
|
||||
lastName: 'Maverick',
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import https from 'https'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
@@ -19,7 +19,7 @@ afterAll(() => {
|
||||
server.close()
|
||||
})
|
||||
|
||||
test('intercepts and mocks a request made via "https"', (done) => {
|
||||
test('intercepts and mocks a request made via "https"', async () => {
|
||||
server.use(
|
||||
http.get('https://api.example.com/resource', () => {
|
||||
return HttpResponse.text('Hello, world!')
|
||||
@@ -27,20 +27,21 @@ test('intercepts and mocks a request made via "https"', (done) => {
|
||||
)
|
||||
const request = https.get('https://api.example.com/resource')
|
||||
|
||||
request.on('response', (response) => {
|
||||
const chunks: Array<Buffer> = []
|
||||
response.on('data', (chunk) => chunks.push(Buffer.from(chunk)))
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
request.on('response', (response) => {
|
||||
const chunks: Array<Buffer> = []
|
||||
response.on('data', (chunk) => chunks.push(Buffer.from(chunk)))
|
||||
|
||||
response.on('error', done)
|
||||
response.once('end', () => {
|
||||
expect(chunks).toHaveLength(1)
|
||||
response.on('error', (error) => reject(error))
|
||||
response.once('end', () => {
|
||||
expect(chunks).toHaveLength(1)
|
||||
|
||||
const responseText = Buffer.concat(chunks).toString('utf8')
|
||||
expect(responseText).toBe('Hello, world!')
|
||||
const responseText = Buffer.concat(chunks).toString('utf8')
|
||||
expect(responseText).toBe('Hello, world!')
|
||||
|
||||
done()
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
request.on('error', (error) => reject(error))
|
||||
})
|
||||
|
||||
request.on('error', done)
|
||||
})
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { http, HttpResponse } from 'msw'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { HttpResponse, http } from 'msw'
|
||||
import { setupServer } from 'msw/node'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { HttpResponse, http, graphql } from 'msw'
|
||||
import { setupServer } from 'msw/node'
|
||||
@@ -21,7 +21,7 @@ beforeAll(async () => {
|
||||
|
||||
afterEach(() => {
|
||||
server.resetHandlers()
|
||||
jest.restoreAllMocks()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
@@ -31,7 +31,7 @@ afterAll(async () => {
|
||||
|
||||
it('does not read the body while parsing an unhandled request', async () => {
|
||||
// Expecting an unhandled request warning in this test.
|
||||
jest.spyOn(console, 'warn').mockImplementation(() => {})
|
||||
vi.spyOn(console, 'warn').mockImplementation(() => {})
|
||||
|
||||
const requestUrl = httpServer.http.url('/resource')
|
||||
const response = await fetch(requestUrl, {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch, { Response } from 'node-fetch'
|
||||
import { HttpServer } from '@open-draft/test-server/http'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import fetch from 'node-fetch'
|
||||
import { HttpResponse, http } from 'msw'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest environment-node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import * as path from 'path'
|
||||
import * as fs from 'fs'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { HttpResponse, http } from 'msw'
|
||||
import { setupServer } from 'msw/node'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import https from 'https'
|
||||
import { HttpResponse, http, delay } from 'msw'
|
||||
@@ -49,7 +49,7 @@ test('responds with a ReadableStream', async () => {
|
||||
expect(await response.text()).toBe('helloworld')
|
||||
})
|
||||
|
||||
test('supports delays when enqueuing chunks', (done) => {
|
||||
test('supports delays when enqueuing chunks', async () => {
|
||||
server.use(
|
||||
http.get('https://api.example.com/stream', () => {
|
||||
const stream = new ReadableStream({
|
||||
@@ -74,29 +74,31 @@ test('supports delays when enqueuing chunks', (done) => {
|
||||
}),
|
||||
)
|
||||
|
||||
const request = https.get('https://api.example.com/stream', (response) => {
|
||||
const chunks: Array<{ buffer: Buffer; timestamp: number }> = []
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const request = https.get('https://api.example.com/stream', (response) => {
|
||||
const chunks: Array<{ buffer: Buffer; timestamp: number }> = []
|
||||
|
||||
response.on('data', (data) => {
|
||||
chunks.push({
|
||||
buffer: Buffer.from(data),
|
||||
timestamp: Date.now(),
|
||||
response.on('data', (data) => {
|
||||
chunks.push({
|
||||
buffer: Buffer.from(data),
|
||||
timestamp: Date.now(),
|
||||
})
|
||||
})
|
||||
|
||||
response.once('end', () => {
|
||||
const textChunks = chunks.map((chunk) => chunk.buffer.toString('utf8'))
|
||||
expect(textChunks).toEqual(['first', 'second', 'third'])
|
||||
|
||||
// Ensure that the chunks were sent over time,
|
||||
// respecting the delay set in the mocked stream.
|
||||
const chunkTimings = chunks.map((chunk) => chunk.timestamp)
|
||||
expect(chunkTimings[1] - chunkTimings[0]).toBeGreaterThanOrEqual(490)
|
||||
expect(chunkTimings[2] - chunkTimings[1]).toBeGreaterThanOrEqual(490)
|
||||
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
|
||||
response.once('end', () => {
|
||||
const textChunks = chunks.map((chunk) => chunk.buffer.toString('utf8'))
|
||||
expect(textChunks).toEqual(['first', 'second', 'third'])
|
||||
|
||||
// Ensure that the chunks were sent over time,
|
||||
// respecting the delay set in the mocked stream.
|
||||
const chunkTimings = chunks.map((chunk) => chunk.timestamp)
|
||||
expect(chunkTimings[1] - chunkTimings[0]).toBeGreaterThanOrEqual(490)
|
||||
expect(chunkTimings[2] - chunkTimings[1]).toBeGreaterThanOrEqual(490)
|
||||
|
||||
done()
|
||||
})
|
||||
request.on('error', (error) => reject(error))
|
||||
})
|
||||
|
||||
request.on('error', done)
|
||||
})
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user