Skip to content

Commit 460ac90

Browse files
committed
msw: Add msw: true option to setupApplicationTest() fn
1 parent 87da680 commit 460ac90

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"@babel/core": "7.26.0",
5757
"@babel/eslint-parser": "7.26.5",
5858
"@babel/plugin-proposal-decorators": "7.25.9",
59+
"@crates-io/msw": "workspace:*",
5960
"@ember/optional-features": "2.2.0",
6061
"@ember/render-modifiers": "2.1.0",
6162
"@ember/string": "3.1.1",

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/helpers/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@ import { setupApplicationTest as upstreamSetupApplicationTest } from 'ember-quni
22

33
import { setupSentryMock } from './sentry';
44
import setupMirage from './setup-mirage';
5+
import setupMSW from './setup-msw';
56

67
export { default as setupMirage } from './setup-mirage';
78
export { setupTest, setupRenderingTest } from 'ember-qunit';
89

910
// see http://emberjs.github.io/rfcs/0637-customizable-test-setups.html
10-
export function setupApplicationTest(hooks, options) {
11+
export function setupApplicationTest(hooks, options = {}) {
12+
let { msw } = options;
13+
1114
upstreamSetupApplicationTest(hooks, options);
12-
setupMirage(hooks);
15+
16+
if (msw) {
17+
setupMSW(hooks);
18+
} else {
19+
setupMirage(hooks);
20+
}
21+
1322
setupSentryMock(hooks);
1423
setupAppTestDataAttr(hooks);
1524
}

tests/helpers/setup-msw.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { db, handlers } from '@crates-io/msw';
2+
import window from 'ember-window-mock';
3+
import { setupWindowMock } from 'ember-window-mock/test-support';
4+
import { http, passthrough } from 'msw';
5+
import { setupWorker } from 'msw/browser';
6+
7+
import { setupFakeTimers } from './fake-timers';
8+
9+
export default function (hooks) {
10+
setupWindowMock(hooks);
11+
setupFakeTimers(hooks, '2017-11-20T12:00:00');
12+
13+
let worker = setupWorker(
14+
...handlers,
15+
http.all('/assets/*', passthrough),
16+
http.all(/.*\/percy\/.*/, passthrough),
17+
http.all('https://:avatars.githubusercontent.com/u/:id', passthrough),
18+
);
19+
20+
hooks.before(() => worker.start({ onUnhandledRequest: 'error' }));
21+
hooks.afterEach(() => worker.resetHandlers());
22+
hooks.afterEach(() => db.reset());
23+
hooks.after(() => worker.stop());
24+
25+
hooks.beforeEach(function () {
26+
this.worker = worker;
27+
this.db = db;
28+
29+
this.authenticateAs = user => {
30+
db.mswSession.create({ user });
31+
window.localStorage.setItem('isLoggedIn', '1');
32+
};
33+
});
34+
}

0 commit comments

Comments
 (0)