-
-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use @swc/jest vs ts-jest (#1457)
* chore: use @swc/jest vs ts-jest * chore: fix esm default import issue in tests
- Loading branch information
1 parent
48ccae4
commit ff54d7a
Showing
10 changed files
with
292 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,75 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
import * as http from 'http' | ||
/** | ||
* @note Do not import as wildcard lest the ESM gods be displeased. | ||
* Make sure "allowSyntheticDefaultImports" is true in tsconfig.json. | ||
*/ | ||
import http from 'http' | ||
import { ServerApi, createServer } from '@open-draft/test-server' | ||
import { rest } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
import { waitForClientRequest } from '../../../support/utils' | ||
|
||
let httpServer: ServerApi | ||
const server = setupServer() | ||
|
||
describe('setupServer / http', () => { | ||
const server = setupServer( | ||
rest.get('http://test.mswjs.io', (req, res, ctx) => { | ||
beforeAll(async () => { | ||
server.listen() | ||
|
||
httpServer = await createServer((app) => { | ||
app.get('/resource', (_, res) => { | ||
return res.status(500).send('original-response') | ||
}) | ||
}) | ||
}) | ||
|
||
beforeEach(() => { | ||
server.use( | ||
rest.get(httpServer.http.makeUrl('/resource'), (req, res, ctx) => { | ||
return res( | ||
ctx.status(401), | ||
ctx.set('x-header', 'yes'), | ||
ctx.json({ | ||
firstName: 'John', | ||
}), | ||
ctx.json({ firstName: 'John' }), | ||
) | ||
}), | ||
) | ||
}) | ||
|
||
beforeAll(() => { | ||
server.listen() | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
}) | ||
|
||
describe('given I perform a request using http.get', () => { | ||
let res: http.IncomingMessage | ||
let resBody = '' | ||
|
||
beforeAll((done) => { | ||
http.get('http://test.mswjs.io', (message) => { | ||
res = message | ||
res.setEncoding('utf8') | ||
res.on('data', (chunk) => (resBody += chunk)) | ||
res.on('end', done) | ||
}) | ||
}) | ||
|
||
test('should return mocked status code', () => { | ||
expect(res.statusCode).toEqual(401) | ||
}) | ||
|
||
test('should return mocked headers', () => { | ||
expect(res.headers).toHaveProperty('content-type', 'application/json') | ||
expect(res.headers).toHaveProperty('x-header', 'yes') | ||
}) | ||
|
||
test('should return mocked body', () => { | ||
expect(resBody).toEqual('{"firstName":"John"}') | ||
}) | ||
}) | ||
|
||
describe('given I perform a request using http.request', () => { | ||
let res: http.IncomingMessage | ||
let resBody = '' | ||
afterEach(() => { | ||
server.resetHandlers() | ||
}) | ||
|
||
beforeAll((done) => { | ||
const req = http.request('http://test.mswjs.io', (message) => { | ||
res = message | ||
res.setEncoding('utf8') | ||
res.on('data', (chunk) => (resBody += chunk)) | ||
res.on('end', done) | ||
}) | ||
afterAll(async () => { | ||
server.close() | ||
await httpServer.close() | ||
}) | ||
|
||
req.end() | ||
}) | ||
it('returns a mocked response to an "http.get" request', async () => { | ||
const request = http.get(httpServer.http.makeUrl('/resource')) | ||
const { response, responseText } = await waitForClientRequest(request) | ||
|
||
test('should return mocked status code', () => { | ||
expect(res.statusCode).toEqual(401) | ||
}) | ||
expect(response.statusCode).toBe(401) | ||
expect(response.headers).toEqual( | ||
expect.objectContaining({ | ||
'content-type': 'application/json', | ||
'x-header': 'yes', | ||
}), | ||
) | ||
expect(responseText).toBe('{"firstName":"John"}') | ||
}) | ||
|
||
test('should return mocked headers', () => { | ||
expect(res.headers).toHaveProperty('content-type', 'application/json') | ||
expect(res.headers).toHaveProperty('x-header', 'yes') | ||
}) | ||
it('returns a mocked response to an "http.request" request', async () => { | ||
const request = http.request(httpServer.http.makeUrl('/resource')) | ||
request.end() | ||
const { response, responseText } = await waitForClientRequest(request) | ||
|
||
test('should return mocked body', () => { | ||
expect(resBody).toEqual('{"firstName":"John"}') | ||
}) | ||
}) | ||
expect(response.statusCode).toBe(401) | ||
expect(response.headers).toEqual( | ||
expect.objectContaining({ | ||
'content-type': 'application/json', | ||
'x-header': 'yes', | ||
}), | ||
) | ||
expect(responseText).toBe('{"firstName":"John"}') | ||
}) |
Oops, something went wrong.