Skip to content

Commit

Permalink
chore: fix esm default import issue in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Nov 14, 2022
1 parent 8a0f925 commit fa198d3
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 170 deletions.
4 changes: 3 additions & 1 deletion config/copyServiceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as path from 'path'
import chalk from 'chalk'
import { until } from '@open-draft/until'

const { cyan } = chalk

/**
* Copies the given Service Worker source file into the destination.
* Injects the integrity checksum into the destination file.
Expand Down Expand Up @@ -46,5 +48,5 @@ export default async function copyServiceWorker(
throw new Error(`Failed to write file.\n${writeFileError.message}`)
}

console.log('Service Worker copied to: %s', chalk.cyan(destFilePath))
console.log('Service Worker copied to: %s', cyan(destFilePath))
}
2 changes: 1 addition & 1 deletion test/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
transform: {
'^.+\\.tsx?$': '@swc/jest',
},
testTimeout: 15000,
testTimeout: 10000,
moduleNameMapper: {
'^msw(.*)': '<rootDir>/..$1',
},
Expand Down
60 changes: 24 additions & 36 deletions test/msw-api/setup-server/scenarios/cookies-request.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import * as https from 'https'
/**
* @jest-environment node
*/
import https from 'https'
import { rest } from 'msw'
import { setupServer, SetupServerApi } from 'msw/node'
import { setupServer } from 'msw/node'
import { ServerApi, createServer } from '@open-draft/test-server'
import { waitForClientRequest } from '../../../support/utils'

let httpServer: ServerApi
let server: SetupServerApi
const server = setupServer()

beforeAll(async () => {
httpServer = await createServer((app) => {
Expand All @@ -13,12 +17,6 @@ beforeAll(async () => {
})
})

server = setupServer(
rest.get(httpServer.https.makeUrl('/user'), (req, res, ctx) => {
return res(ctx.json({ cookies: req.cookies }))
}),
)

server.listen()
})

Expand All @@ -27,34 +25,24 @@ afterAll(async () => {
await httpServer.close()
})

test('has access to request cookies', (done) => {
let responseBody = ''
test('has access to request cookies', async () => {
server.use(
rest.get(httpServer.https.makeUrl('/user'), (req, res, ctx) => {
return res(ctx.json({ cookies: req.cookies }))
}),
)

const url = new URL(httpServer.https.makeUrl('/user'))

https.get(
{
method: 'GET',
protocol: url.protocol,
host: url.host,
path: url.pathname,
headers: {
Cookie: 'auth-token=abc-123',
},
},
(res) => {
res.setEncoding('utf8')
res.on('error', done)
res.on('data', (chunk) => (responseBody += chunk))
res.on('end', () => {
const json = JSON.parse(responseBody)
expect(json).toEqual({
cookies: {
'auth-token': 'abc-123',
},
})

done()
})
const request = https.get({
protocol: url.protocol,
host: url.host,
path: url.pathname,
headers: {
Cookie: 'auth-token=abc-123',
},
)
})
const { responseText } = await waitForClientRequest(request)

expect(responseText).toBe('{"cookies":{"auth-token":"abc-123"}}')
})
121 changes: 56 additions & 65 deletions test/msw-api/setup-server/scenarios/http.test.ts
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"}')
})
118 changes: 52 additions & 66 deletions test/msw-api/setup-server/scenarios/https.test.ts
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"}')
})
Loading

0 comments on commit fa198d3

Please sign in to comment.