Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With karma #75

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ __*
msw-v*
cypress
package
storybook-static
storybook-static
msw_generated_pacts/
4 changes: 3 additions & 1 deletion examples/with-jest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This repository illustrates how to use [Mock Service Worker](https://github.com/
- [whatwg-fetch](https://github.com/github/fetch) for polyfilling `window.fetch` in tests.
- [Babel](https://babeljs.io/) to allow JSX usage in tests.
- [React Testing Library](https://github.com/testing-library/react-testing-library) for unit test assertions.
- [msw-pact](https://github.com/you54f/msw-pact) for generating pact consumer contracts from msw interactions

## Getting started

Expand All @@ -28,8 +29,9 @@ $ yarn test

- [`src/mocks/handlers.js`](src/mocks/handlers.js) describes what requests to mock.
- [`src/mocks/server.js`](src/mocks/server.js) enabled requests interception in Node.js.
- [`./msw_generated_pacts`](./msw_generated_pacts) stores the generated pact files

### Jest

- [`jest.config.js`](jest.config.js) configures Jest.
- [`jest.setup.js`](jest.setup.js) imports `fetch` polyfill (`whatwg-fetch`), configures React Testing Library, and enables requests interception.
- [`jest.setup.js`](jest.setup.js) imports `fetch` polyfill (`whatwg-fetch`), configures React Testing Library, and enables requests interception. Also setups `msw-pact` to generate pact files from specified msw interactions
15 changes: 14 additions & 1 deletion examples/with-jest/jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,28 @@ import 'whatwg-fetch'
import '@testing-library/jest-dom'

import { server } from './src/mocks/server'
import { setupMswPact } from "msw-pact";

const mswPact = setupMswPact({
server,
options: { consumer: "pactflow-example-consumer", providers: { ['pactflow-example-provider']: ['login'] } },
});

beforeAll(() => {
server.listen()
})

beforeEach(() => {
mswPact.newTest();
});

afterEach(() => {
mswPact.verifyTest();
server.resetHandlers()
})

afterAll(() => {
afterAll(async () => {
await mswPact.writeToFile(); // writes the pacts to a file
mswPact.clear();
server.close()
})
2 changes: 2 additions & 0 deletions examples/with-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"@testing-library/user-event": "^13.1.9",
"babel-preset-react-app": "^10.0.0",
"jest": "^26.6.3",
"msw": "^0.38.2",
"msw-pact": "0.8.0-alpha-01",
"whatwg-fetch": "^3.6.2"
}
}
1 change: 1 addition & 0 deletions examples/with-karma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"karma-webpack": "^4.0.2",
"mocha": "^8.2.1",
"msw": "latest",
"msw-pact": "0.8.0-alpha-01",
"webpack": "^4.44.2"
}
}
19 changes: 17 additions & 2 deletions examples/with-karma/test/User.spec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
const { expect } = require('chai')
import { worker } from '../mocks/browser'
import { setupMswPact } from "msw-pact";

const mswPact = setupMswPact({
worker,
options: { consumer: "pactflow-example-consumer", providers: { ['pactflow-example-provider']: ['login'] } },
});

describe('User', function () {
this.beforeAll(async () => {
await worker.start()
// See https://github.com/mswjs/examples/issues/56#issuecomment-856521932
const registration = await worker.start();
await registration.update();
})

this.beforeEach(() => {
mswPact.newTest();
});

this.afterEach(() => {
mswPact.verifyTest();
worker.resetHandlers()
})

this.afterAll(() => {
this.afterAll(async() => {
// await mswPact.writeToFile(); // writes the pacts to a file
mswPact.clear();
worker.stop()
})

Expand Down
Loading