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

Allow for setupMirage to pass options to startMirage #2371

Merged
merged 1 commit into from
Mar 21, 2022
Merged
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
4 changes: 2 additions & 2 deletions addon-test-support/setup-mirage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { settled } from '@ember/test-helpers';
NOTE: the `hooks = self` is for mocha support
@hide
*/
export default function setupMirage(hooks = self) {
export default function setupMirage(hooks = self, options) {
hooks.beforeEach(function () {
if (!this.owner) {
throw new Error(
Expand All @@ -20,7 +20,7 @@ export default function setupMirage(hooks = self) {
);
}

this.server = startMirage(this.owner);
this.server = startMirage(this.owner, options);
});

hooks.afterEach(function () {
Expand Down
1 change: 1 addition & 0 deletions tests/dummy/app/pods/docs/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</nav.subnav>

{{nav.section "Testing"}}
{{nav.item "setupMirage test helper" "docs.testing.seupt-mirage"}}
{{nav.item "Acceptance tests" "docs.testing.acceptance-tests"}}
{{nav.item "Integration and unit tests" "docs.testing.integration-and-unit-tests"}}
{{nav.item "Assertions" "docs.testing.assertions"}}
Expand Down
68 changes: 68 additions & 0 deletions tests/dummy/app/pods/docs/testing/setup-mirage/template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# setupMirage test helper

In your tests (acceptance, integration and unit) you can import the `setupMirage` test helper to start the Mirage server. Passing the hooks from the test module allows the mirage server to be created and shutdown before and after each test.

```js
import { setupApplicationTest } from 'ember-qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';

module('Acceptance | Homepage test', function(hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);

test('my first test', async function(assert) {
// test code
});
});
```

The second parameter (optional) allows defining the mirage server to be used for this set of tests.
```js
import { setupApplicationTest } from 'ember-qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';
import makeServer from 'app-name/mirage/config'; // replace app-name with your app name

module('Acceptance | Homepage test', function(hooks) {
setupApplicationTest(hooks);
setupMirage(hooks, { makeServer });

test('my first test', async function(assert) {
// test code
});
});
```

If it is not desirable to use the default config from the mirage directory you could import any other file that implements the same function, or even define the function locally or inline
```js
import { setupApplicationTest } from 'ember-qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';
import { discoverEmberDataModels } from 'ember-cli-mirage';
import { createServer } from 'miragejs';

const makeServer = function(config) {
let finalConfig = {
...config,
models: { ...discoverEmberDataModels(), ...config.models },
routes() {
this.namespace = "api"
this.timing = 2000

this.get("/movies", () => {
return ["Interstellar", "Inception", "Dunkirk"]
})
}
};

return createServer(finalConfig);
}

module('Acceptance | Homepage test', function(hooks) {
setupApplicationTest(hooks);
setupMirage(hooks, { makeServer });

test('my first test', async function(assert) {
// test code
});
});
```