Skip to content

Commit ea9bbd6

Browse files
committed
msw: Add msw: true option to setupApplicationTest() fn
1 parent f5a486f commit ea9bbd6

File tree

4 files changed

+45
-2
lines changed

4 files changed

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

0 commit comments

Comments
 (0)