-
-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: fix esm default import issue in tests
- Loading branch information
1 parent
8a0f925
commit fa198d3
Showing
7 changed files
with
165 additions
and
170 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
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"}') | ||
}) |
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,85 +1,71 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
import * as https from 'https' | ||
import { IncomingMessage } from 'http' | ||
import https from 'https' | ||
import { ServerApi, createServer } from '@open-draft/test-server' | ||
import { rest } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
import { waitForClientRequest } from '../../../support/utils' | ||
|
||
describe('setupServer / https', () => { | ||
const server = setupServer( | ||
rest.get('https://test.mswjs.io', (req, res, ctx) => { | ||
let httpServer: ServerApi | ||
const server = setupServer() | ||
|
||
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.https.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 https.get', () => { | ||
let res: IncomingMessage | ||
let resBody = '' | ||
|
||
beforeAll((done) => { | ||
https.get('https://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 https.request', () => { | ||
let res: IncomingMessage | ||
let resBody = '' | ||
afterEach(() => { | ||
server.resetHandlers() | ||
}) | ||
|
||
beforeAll((done) => { | ||
const req = https.request('https://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 "https.get" request', async () => { | ||
const request = https.get(httpServer.https.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 "https.request" request', async () => { | ||
const request = https.request(httpServer.https.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.