Skip to content

Commit

Permalink
Get tests passing -- to get import.meta.glob working in the tests, we…
Browse files Browse the repository at this point in the history
… need a plugin for import.meta.glob
  • Loading branch information
NullVoxPopuli committed Nov 15, 2024
1 parent a1f7499 commit 1b9af89
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ember-mirage/src/create-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function entityName(path) {
return camelize(filenameWithoutExt);
}

export default async function createConfig(mirageImportMap = {}) {
export async function createConfig(mirageImportMap = {}) {
return {
factories: await importEntities(mirageImportMap.factories),
fixtures: await importEntities(mirageImportMap.fixtures),
Expand Down
1 change: 1 addition & 0 deletions ember-mirage/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { createConfig, entityName } from './create-config.js';
21 changes: 19 additions & 2 deletions ember-mirage/src/test-support/setup-mirage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { assert } from '@ember/debug';
import { settled } from '@ember/test-helpers';
import { createServer as _createServer } from 'miragejs';

export function setupMirage(hooks = self, { createServer, config }) {
assert(
`Unexpected arity for setupMirage. Expected 2 (hooks, { createServer, or config })`,
arguments.length <= 2 && arguments.length > 0,
);
assert(
`Second argument to setupMirage must be an object and not null`,
typeof arguments[1] === 'object' && arguments[1] !== null,
);
assert(
`Second argument to setupMirage must on or both of createServer and/or config. You passed ${Object.keys(arguments[1]).join(', ')}`,
'createServer' in arguments[1] || 'config' in arguments[1],
);

createServer ??= _createServer;

export function setupMirage(hooks = self, { createServer }) {
hooks.beforeEach(async function () {
if (!this.owner) {
throw new Error(
Expand All @@ -10,7 +27,7 @@ export function setupMirage(hooks = self, { createServer }) {
);
}

this.server = await createServer();
this.server = await createServer(config ?? {});
});

hooks.afterEach(function () {
Expand Down
32 changes: 32 additions & 0 deletions test-app/app/mirage/servers/create-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//import { createConfig } from 'ember-mirage';
//import defaultRoutes from '../routes/default';
//
//export async function config(store) {
// const { importEmberDataModels, importEmberDataSerializers } = await import(
// 'ember-mirage/ember-data'
// );
//
// const mirageConfig = await createConfig({
// factories: import.meta.glob('./factories/*'),
// fixtures: import.meta.glob('./fixtures/*'),
// // Don't import our mirage things that will be auto-discovered
// // models: import.meta.glob('./models/*'),
// // serializers: import.meta.glob('./serializers/*'),
// identityManagers: import.meta.glob('./identity-managers/*'),
// });
//
// return {
// ...mirageConfig,
// models: {
// // use ember-data model auto discovery
// ...importEmberDataModels(store, import.meta.glob('../models/*')),
// ...mirageConfig.models,
// },
// // apply ember-data serializer config details to mirage serializers
// serializers: importEmberDataSerializers(store, mirageConfig.serializers),
// routes() {
// this.config({ routes: defaultRoutes });
// },
// };
//}
//
Empty file.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { createServer } from 'miragejs';

import factories from '../factories';
import mirageModels from '../models';
import defaultRoutes from '../routes/default';
Expand Down Expand Up @@ -28,7 +26,7 @@ export default function (config) {
},
};

return createServer(finalConfig);
return finalConfig;
}

function defineRoutes() {
Expand Down
16 changes: 14 additions & 2 deletions test-app/tests/acceptance/example-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { currentURL, visit } from '@ember/test-helpers';
import { createServer } from 'miragejs';
import { module, test } from 'qunit';

import mirageConfig from 'test-app/mirage/servers/default';
import mirageConfig from 'test-app/mirage/servers/manual';
import { setupApplicationTest } from 'test-app/tests/helpers';

import { setupMirage } from 'test-app/tests/test-support/mirage';
Expand All @@ -11,6 +12,7 @@ module('Acceptance | example test', function (hooks) {

module('Test with default config', function (hooks) {
setupMirage(hooks);

test('visiting /example', async function (assert) {
await visit('/example');

Expand All @@ -19,7 +21,17 @@ module('Acceptance | example test', function (hooks) {
});

module('Test with imported config', function (hooks) {
setupMirage(hooks, { makeserver: mirageConfig });
setupMirage(hooks, { config: mirageConfig });
test('visiting /example', async function (assert) {
await visit('/example');

assert.strictEqual(currentURL(), '/example');
});
});

module('Test with custom server', function (hooks) {
setupMirage(hooks, { createServer: () => createServer(mirageConfig) });

test('visiting /example', async function (assert) {
await visit('/example');

Expand Down
11 changes: 6 additions & 5 deletions test-app/tests/test-support/mirage.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import mirageConfig from 'test-app/mirage/servers/default';
import mirageConfig from 'test-app/mirage/servers/manual';

import { setupMirage as _setupMirage } from 'ember-mirage/test-support';

export function setupMirage(hooks, options) {
options = options || {};
options.makeServer = options.makeServer || mirageConfig;
import { createServer as _createServer } from 'miragejs';

return _setupMirage(hooks, options);
export function setupMirage(hooks, { createServer, config } = {}) {
createServer ??= _createServer;
config ??= mirageConfig;
return _setupMirage(hooks, { createServer, config });
}

0 comments on commit 1b9af89

Please sign in to comment.