Skip to content

Commit

Permalink
Deprecates "composeMocks" in favor of "setupWorker"
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Apr 29, 2020
1 parent 1e0d09f commit c1b7cea
Show file tree
Hide file tree
Showing 29 changed files with 90 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { composeMocks } from './composeMocks'
import { setupWorker } from './setupWorker'
import rest, { restContext } from '../handlers/rest'
import { MockedRequest, ResponseResolver } from '../handlers/requestHandler'

Expand All @@ -11,7 +11,7 @@ test('Generates schema based on provided handlers', () => {
return res(json({ a: 2 }))
}

const api = composeMocks(
const api = setupWorker(
rest.get('https://api.github.com/users/:username', simpleResolver),
rest.get('foo', simpleResolver),
rest.post(/footer/, simpleResolver),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import { createStart } from './start/createStart'
import { createStop } from './stop/createStop'

/**
* Composes multiple request handlers into a single mocking schema.
* Configures a Service Worker with the given request handler functions.
*/
export const composeMocks = (
...requestHandlers: RequestHandler<any, any>[]
) => {
export function setupWorker(...requestHandlers: RequestHandler<any, any>[]) {
const context: ComposeMocksInternalContext = {
worker: null,
registration: null,
Expand All @@ -20,3 +18,14 @@ export const composeMocks = (
stop: createStop(context),
}
}

/**
* Composes multiple request handlers into a single mocking schema.
* @deprecated
*/
export function composeMocks(...requestHandlers: RequestHandler<any, any>[]) {
console.warn(
'[MSW] The `composeMocks()` function is deprecated and will be removed in the next release. Please use the `setupWorker()` function instead.',
)
return setupWorker(...requestHandlers)
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as context from './context'

export { composeMocks } from './composeMocks/composeMocks'
export { setupWorker, composeMocks } from './composeMocks/setupWorker'
export { MockedResponse, ResponseTransformer, response } from './response'
export { context }

Expand Down
6 changes: 3 additions & 3 deletions test/graphql-api/errors.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { composeMocks, graphql } from 'msw'
import { setupWorker, graphql } from 'msw'

const { start } = composeMocks(
const worker = setupWorker(
graphql.query('Login', (req, res, ctx) => {
return res(
ctx.errors([
Expand All @@ -18,4 +18,4 @@ const { start } = composeMocks(
}),
)

start()
worker.start()
6 changes: 3 additions & 3 deletions test/graphql-api/mutation.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { composeMocks, graphql } from 'msw'
import { setupWorker, graphql } from 'msw'

const { start } = composeMocks(
const worker = setupWorker(
graphql.mutation('Logout', (req, res, ctx) => {
return res(
ctx.data({
Expand All @@ -12,4 +12,4 @@ const { start } = composeMocks(
}),
)

start()
worker.start()
6 changes: 3 additions & 3 deletions test/graphql-api/query.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { composeMocks, graphql } from 'msw'
import { setupWorker, graphql } from 'msw'

const { start } = composeMocks(
const worker = setupWorker(
graphql.query('GetUserDetail', (req, res, ctx) => {
return res(
ctx.data({
Expand All @@ -13,4 +13,4 @@ const { start } = composeMocks(
}),
)

start()
worker.start()
6 changes: 3 additions & 3 deletions test/graphql-api/variables.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { composeMocks, graphql } from 'msw'
import { setupWorker, graphql } from 'msw'

const { start } = composeMocks(
const worker = setupWorker(
graphql.query('GetGithubUser', (req, res, ctx) => {
const { username } = req.variables

Expand Down Expand Up @@ -40,4 +40,4 @@ const { start } = composeMocks(
}),
)

start()
worker.start()
6 changes: 3 additions & 3 deletions test/msw-api/exception-handling.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { composeMocks, rest } from 'msw'
import { setupWorker, rest } from 'msw'

const { start } = composeMocks(
const worker = setupWorker(
rest.get('https://api.github.com/users/:username', () => {
// @ts-ignore
nonExisting()
return null
}),
)

start()
worker.start()
6 changes: 3 additions & 3 deletions test/msw-api/hard-reload.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { composeMocks, rest } from 'msw'
import { setupWorker, rest } from 'msw'

const { start } = composeMocks(
const worker = setupWorker(
rest.get('https://api.github.com', (req, res, ctx) => {
return res(
ctx.json({
Expand All @@ -10,4 +10,4 @@ const { start } = composeMocks(
}),
)

start()
worker.start()
6 changes: 3 additions & 3 deletions test/msw-api/integrity-check-invalid.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { composeMocks, rest } from 'msw'
import { setupWorker, rest } from 'msw'

const { start } = composeMocks(
const worker = setupWorker(
rest.get('https://api.github.com/users/:username', (req, res, ctx) => {
return res(ctx.json({ mocked: true }))
}),
)

start({
worker.start({
serviceWorker: {
// Use custom Service Worker URL for the purpose of intentionally
// registering an outdated worker. Please do not use this as an example.
Expand Down
6 changes: 3 additions & 3 deletions test/msw-api/integrity-check-valid.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { composeMocks, rest } from 'msw'
import { setupWorker, rest } from 'msw'

const { start } = composeMocks(
const worker = setupWorker(
rest.get('https://api.github.com/users/octocat', (req, res, ctx) => {
return res(ctx.json({ mocked: true }))
}),
)

start()
worker.start()
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { composeMocks, rest } from 'msw'
import { setupWorker, rest } from 'msw'

const { start } = composeMocks(
const worker = setupWorker(
rest.get('/user', (req, res, ctx) => {
return res(
ctx.json({
Expand All @@ -12,7 +12,7 @@ const { start } = composeMocks(
)

// @ts-ignore
window.__MSW_REGISTRATION__ = start({
window.__MSW_REGISTRATION__ = worker.start({
// Disable logging of matched requests into browser's console
quiet: true,
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from 'path'
import { TestAPI, runBrowserWith } from '../../../support/runBrowserWith'
import { Response } from 'puppeteer'

describe('API: composeMocks / start / quiet', () => {
describe('API: setupWorker / start / quiet', () => {
let test: TestAPI
let logs: string[] = []

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { composeMocks, rest } from 'msw'
import { setupWorker, rest } from 'msw'

const { start } = composeMocks(
const worker = setupWorker(
rest.get('/user', (req, res, ctx) => {
return res(ctx.status(200))
}),
)

// @ts-ignore
window.__MSW_REGISTRATION__ = start().then((reg) => {
window.__MSW_REGISTRATION__ = worker.start().then((reg) => {
console.log('Registration Promise resolved')
return reg.constructor.name
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path'
import { TestAPI, runBrowserWith } from '../../../support/runBrowserWith'

describe('API: composeMocks / start', () => {
describe('API: setupWorker / start', () => {
let test: TestAPI

beforeAll(async () => {
Expand All @@ -12,7 +12,7 @@ describe('API: composeMocks / start', () => {
return test.cleanup()
})

describe('given a custom Promise chain handler to start()', () => {
describe('given a custom Promise chain handler to worker.start()', () => {
let resolvedPayload

beforeAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { composeMocks, rest } from 'msw'
import { setupWorker, rest } from 'msw'

const { start, stop } = composeMocks(
const worker = setupWorker(
rest.get('https://api.github.com', (req, res, ctx) => {
return res(ctx.json({ mocked: true }))
}),
)

start()
worker.start()

// @ts-ignore
window.__mswStop = stop
window.__mswStop = worker.stop
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const stopWorkerOn = async (page: Page) => {
})
}

describe('API: composeMocks / stop', () => {
describe('API: setupWorker / stop', () => {
let test: TestAPI

beforeAll(async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/msw-api/unregister.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { composeMocks, rest } from 'msw'
import { setupWorker, rest } from 'msw'

const { start } = composeMocks(
const worker = setupWorker(
rest.get('https://api.github.com', (req, res, ctx) => {
return res(
ctx.json({
Expand All @@ -11,4 +11,4 @@ const { start } = composeMocks(
)

// @ts-ignore
window.__mswStart = start
window.__mswStart = worker.start
6 changes: 3 additions & 3 deletions test/rest-api/basic.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { composeMocks, rest } from 'msw'
import { setupWorker, rest } from 'msw'

const { start } = composeMocks(
const worker = setupWorker(
rest.get('https://api.github.com/users/:username', (req, res, ctx) => {
const { username } = req.params

Expand All @@ -13,4 +13,4 @@ const { start } = composeMocks(
}),
)

start()
worker.start()
6 changes: 3 additions & 3 deletions test/rest-api/context.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { composeMocks, rest } from 'msw'
import { setupWorker, rest } from 'msw'

const { start } = composeMocks(
const worker = setupWorker(
rest.get('https://test.msw.io/', (req, res, ctx) => {
return res(
ctx.delay(2000),
Expand All @@ -16,4 +16,4 @@ const { start } = composeMocks(
}),
)

start()
worker.start()
6 changes: 3 additions & 3 deletions test/rest-api/cookies.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { composeMocks, rest } from 'msw'
import { setupWorker, rest } from 'msw'

const { start } = composeMocks(
const worker = setupWorker(
rest.get('/user', (req, res, ctx) => {
return res(
ctx.cookie('my-cookie', 'value'),
Expand All @@ -11,4 +11,4 @@ const { start } = composeMocks(
}),
)

start()
worker.start()
6 changes: 3 additions & 3 deletions test/rest-api/custom-request-handler.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
composeMocks,
setupWorker,
ResponseResolver,
RequestHandler,
ResponseTransformer,
Expand Down Expand Up @@ -53,7 +53,7 @@ const withUrl = (
}
}

const { start } = composeMocks(
const worker = setupWorker(
withHeader('x-custom-header', (req, res, ctx) => {
return res(
ctx.status(401),
Expand All @@ -73,4 +73,4 @@ const { start } = composeMocks(
}),
)

start()
worker.start()
6 changes: 3 additions & 3 deletions test/rest-api/headers-multiple.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { composeMocks, rest } from 'msw'
import { setupWorker, rest } from 'msw'

const { start } = composeMocks(
const worker = setupWorker(
rest.post('https://test.msw.io/', (req, res, ctx) => {
return res(
ctx.json({
Expand All @@ -21,4 +21,4 @@ const { start } = composeMocks(
}),
)

start()
worker.start()
6 changes: 3 additions & 3 deletions test/rest-api/params.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { composeMocks, rest } from 'msw'
import { setupWorker, rest } from 'msw'

const { start } = composeMocks(
const worker = setupWorker(
rest.get(
'https://api.github.com/users/:username/messages/:messageId',
(req, res, ctx) => {
Expand All @@ -16,4 +16,4 @@ const { start } = composeMocks(
),
)

start()
worker.start()
6 changes: 3 additions & 3 deletions test/rest-api/query.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { composeMocks, rest } from 'msw'
import { setupWorker, rest } from 'msw'

const { start } = composeMocks(
const worker = setupWorker(
rest.get('https://test.msw.io/api/books', (req, res, ctx) => {
const bookId = req.query.get('id')
req.query.getAll
Expand All @@ -23,4 +23,4 @@ const { start } = composeMocks(
}),
)

start()
worker.start()
Loading

0 comments on commit c1b7cea

Please sign in to comment.