Skip to content

Commit

Permalink
Drop usage of MSW in unit tests (internal-2014)
Browse files Browse the repository at this point in the history
  • Loading branch information
underoot committed Dec 9, 2024
1 parent e3bc83d commit ee8a888
Show file tree
Hide file tree
Showing 19 changed files with 746 additions and 1,376 deletions.
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
"mapbox-gl-styles": "^2.0.2",
"minimist": "^1.2.6",
"mock-geolocation": "^1.0.11",
"msw": "^2.3.1",
"node-notifier": "^10.0.1",
"npm-font-open-sans": "^1.1.0",
"npm-run-all": "^4.1.5",
Expand Down
31 changes: 1 addition & 30 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,7 @@ See [`test/integration/README.md`](./integration/README.md).
- **You should not mock any internal domain objects.** Internal domain objects include `Style`, `Map`, `Transform`, and `Dispatcher`. If this is difficult because of some interface, refactor that interface. This ensures that tests accurately exercise the code paths used in production.
- **You should test one return value or side effect per test case.** Feel free to pull shared logic into a function. This ensures that tests are easy to understand and modify.
- **You should only test the return values and global side effects of methods.** You should not not test internal behavior, such as that another method is called with particular arguments. This ensures that method implementations may change without causing test failures.
- **You must not make network requests in test cases.** This rule holds in cases when result isn't used or is expected to fail. You may use `getNetworkWorker` from `test/util/network.js` module, which uses [`mswj`](https://mswjs.io/docs/api/setup-worker/) to simulate network requests. This ensures that tests are reliable, able to be run in an isolated environment, and performant. In your test suite you can setup network worker in the following way:
```js
let networkWorker;

beforeAll(async () => {
networkWorker = await getNetworkWorker(window);
});

afterEach(() => {
// Clear runtime mocks, which were added by `.use`
networkWorker.resetHandlers();
});

afterAll(() => {
networkWorker.stop();
});

test('should tests something', () => {
networkWorker.use(
http.get('/notfound.png', async () => {
return new HttpResponse(null, {status: 404});
}),
http.get('/style.json', async () => {
return HttpResponse.json({});
})
);

// Network mocks are ready: act and assert
});
```
- **You must not make network requests in test cases.** This rule holds in cases when result isn't used or is expected to fail. You may use `mockFetch` from `test/util/network.js` module to mock requests if you need though. This ensures that tests are reliable, able to be run in an isolated environment, and performant
- **You should use clear [input space partitioning](http://crystal.uta.edu/~ylei/cse4321/data/isp.pdf) schemes.** Look for edge cases! This ensures that tests suites are comprehensive and easy to understand.

If you want to debug your unit tests you can open UI for that with the following command:
Expand Down
295 changes: 0 additions & 295 deletions test/unit/mock-worker/mockServiceWorker.js

This file was deleted.

Loading

0 comments on commit ee8a888

Please sign in to comment.