Skip to content

Commit

Permalink
Merge branch 'main' into mattcosta7/statuses-dev-dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito authored Nov 15, 2022
2 parents c638b67 + ff54d7a commit d5dffb8
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 228 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 jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
bail: true,
roots: ['<rootDir>/src', '<rootDir>/cli'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
'^.+\\.tsx?$': '@swc/jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(j|t)sx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@
"@commitlint/config-conventional": "^16.0.0",
"@open-draft/test-server": "^0.2.3",
"@ossjs/release": "^0.4.0",
"@swc/core": "^1.3.15",
"@swc/jest": "^0.2.23",
"@types/fs-extra": "^9.0.13",
"@types/jest": "26",
"@types/json-bigint": "^1.0.1",
Expand Down Expand Up @@ -141,7 +143,6 @@
"rimraf": "^3.0.2",
"simple-git-hooks": "^2.8.0",
"statuses": "^2.0.0",
"ts-jest": "26",
"ts-loader": "^9.2.6",
"ts-node": "^10.1.0",
"tsup": "^5.12.8",
Expand Down
6 changes: 4 additions & 2 deletions test/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module.exports = {
preset: 'ts-jest',
testTimeout: 15000,
transform: {
'^.+\\.tsx?$': '@swc/jest',
},
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"}')
})
Loading

0 comments on commit d5dffb8

Please sign in to comment.