-
Notifications
You must be signed in to change notification settings - Fork 32
Migrate from ember-cli-mirage to miragejs
#3088
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
base: main
Are you sure you want to change the base?
Changes from all commits
90e30e5
8dab078
e9e7494
6fdf51b
d2f9e4f
06f3927
dae981e
d1ed4a1
69ef654
f0c4c72
28789c4
c0e3722
373ffc5
c455865
baa51d5
8fb0650
5ef4478
44ceb4e
aae64fe
f9ddf09
49cd107
35a37e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /** | ||
| * Copyright (c) HashiCorp, Inc. | ||
| * SPDX-License-Identifier: BUSL-1.1 | ||
| */ | ||
|
|
||
| import { settled } from '@ember/test-helpers'; | ||
| import startMirage from 'api/mirage/config'; | ||
|
|
||
| export function setupMirage(hooks) { | ||
| hooks.beforeEach(function () { | ||
| if (!this.owner) { | ||
| throw new Error( | ||
| 'Must call one of the ember-qunit setupTest() / setupRenderingTest() / setupApplicationTest() first', | ||
| ); | ||
| } | ||
|
|
||
| // the environment property here is configuration to the mirage server: | ||
| // https://github.com/miragejs/miragejs/blob/7ff4f3f6fe56bf0cb1648f5af3f5210fcb07e20b/types/index.d.ts#L383 | ||
| // It is not related to ember's build environment. In this case for mirage the "test" environment does | ||
| // not load the default scenario: | ||
| // https://github.com/miragejs/miragejs/blob/7ff4f3f6fe56bf0cb1648f5af3f5210fcb07e20b/lib/server.js#L302 | ||
| this.server = startMirage({ environment: 'test' }); | ||
| }); | ||
|
|
||
| hooks.afterEach(function () { | ||
| return settled().then(() => { | ||
| if (this.server) { | ||
| this.server.shutdown(); | ||
| delete this.server; | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,7 @@ | |
| * SPDX-License-Identifier: BUSL-1.1 | ||
| */ | ||
|
|
||
| import { | ||
| discoverEmberDataModels, | ||
| applyEmberDataSerializers, | ||
| } from 'ember-cli-mirage'; | ||
| import { createServer, Response } from 'miragejs'; | ||
| import environmentConfig from '../config/environment'; | ||
| import { authHandler, deauthHandler } from './route-handlers/auth'; | ||
| import { targetHandler } from './route-handlers/target'; | ||
| import { pickRandomStatusString } from './factories/session'; | ||
|
|
@@ -17,19 +12,177 @@ import makeBooleanFilter from './helpers/bexpr-filter'; | |
| import { faker } from '@faker-js/faker'; | ||
| import { asciicasts } from './data/asciicasts'; | ||
| import { TYPE_WORKER_PKI } from 'api/models/worker'; | ||
| import environmentConfig from 'ember-get-config'; | ||
|
|
||
| // mirage models (alphabetical) | ||
| import accountModel from './models/account'; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Part of the build magic |
||
| import aliasModel from './models/alias'; | ||
| import authMethodModel from './models/auth-method'; | ||
| import baseModel from './models/base'; | ||
| import channelRecordingModel from './models/channel-recording'; | ||
| import connectionRecordingModel from './models/connection-recording'; | ||
| import credentialLibraryModel from './models/credential-library'; | ||
| import credentialStoreModel from './models/credential-store'; | ||
| import credentialModel from './models/credential'; | ||
| import groupModel from './models/group'; | ||
| import hostCatalogModel from './models/host-catalog'; | ||
| import hostSetModel from './models/host-set'; | ||
| import hostModel from './models/host'; | ||
| import managedGroupModel from './models/managed-group'; | ||
| import policyModel from './models/policy'; | ||
| import roleModel from './models/role'; | ||
| import scopeModel from './models/scope'; | ||
| import sessionRecordingModel from './models/session-recording'; | ||
| import sessionModel from './models/session'; | ||
| import storageBucketModel from './models/storage-bucket'; | ||
| import targetModel from './models/target'; | ||
| import userModel from './models/user'; | ||
| import workerModel from './models/worker'; | ||
|
|
||
| // mirage serializers (alphabetical) | ||
| import accountSerializer from './serializers/account'; | ||
| import aliasSerializer from './serializers/alias'; | ||
| import applicationSerializer from './serializers/application'; | ||
| import authMethodSerializer from './serializers/auth-method'; | ||
| import channelRecordingSerializer from './serializers/channel-recording'; | ||
| import connectionRecordingSerializer from './serializers/connection-recording'; | ||
| import credentialLibrarySerializer from './serializers/credential-library'; | ||
| import credentialStoreSerializer from './serializers/credential-store'; | ||
| import credentialSerializer from './serializers/credential'; | ||
| import groupSerializer from './serializers/group'; | ||
| import hostCatalogSerializer from './serializers/host-catalog'; | ||
| import hostSetSerializer from './serializers/host-set'; | ||
| import hostSerializer from './serializers/host'; | ||
| import managedGroupSerializer from './serializers/managed-group'; | ||
| import policySerializer from './serializers/policy'; | ||
| import roleSerializer from './serializers/role'; | ||
| import scopeSerializer from './serializers/scope'; | ||
| import sessionRecordingSerializer from './serializers/session-recording'; | ||
| import sessionSerializer from './serializers/session'; | ||
| import storageBucketSerializer from './serializers/storage-bucket'; | ||
| import targetSerializer from './serializers/target'; | ||
| import userSerializer from './serializers/user'; | ||
| import workerSerializer from './serializers/worker'; | ||
|
|
||
| // mirage scenarios (alphabetical) | ||
| import defaultScenario from './scenarios/default'; | ||
| import ipcScenario from './scenarios/ipc'; | ||
|
|
||
| // mirage factories (alphabetical) | ||
| import accountFactory from './factories/account'; | ||
| import aliasFactory from './factories/alias'; | ||
| import authMethodFactory from './factories/auth-method'; | ||
| import channelRecordingFactory from './factories/channel-recording'; | ||
| import connectionRecordingFactory from './factories/connection-recording'; | ||
| import credentialLibraryFactory from './factories/credential-library'; | ||
| import credentialStoreFactory from './factories/credential-store'; | ||
| import credentialFactory from './factories/credential'; | ||
| import groupFactory from './factories/group'; | ||
| import hostCatalogFactory from './factories/host-catalog'; | ||
| import hostSetFactory from './factories/host-set'; | ||
| import hostFactory from './factories/host'; | ||
| import managedGroupFactory from './factories/managed-group'; | ||
| import policyFactory from './factories/policy'; | ||
| import roleFactory from './factories/role'; | ||
| import scopeFactory from './factories/scope'; | ||
| import sessionRecordingFactory from './factories/session-recording'; | ||
| import storageBucketFactory from './factories/storage-bucket'; | ||
| import sessionFactory from './factories/session'; | ||
| import targetFactory from './factories/target'; | ||
| import userFactory from './factories/user'; | ||
| import workerFactory from './factories/worker'; | ||
|
|
||
| const isTesting = environmentConfig.environment === 'test'; | ||
|
|
||
| // Main function | ||
| // More info about server configuration https://www.ember-cli-mirage.com/docs/advanced/server-configuration | ||
| // More info about server configuration: | ||
| // https://github.com/miragejs/miragejs/blob/7ff4f3f6fe56bf0cb1648f5af3f5210fcb07e20b/types/index.d.ts#L375-L404 | ||
| export default function (mirageConfig) { | ||
| let finalConfig = { | ||
| ...mirageConfig, | ||
|
|
||
| scenarios: { | ||
| default: defaultScenario, | ||
| ipcScenario: ipcScenario, | ||
| }, | ||
|
|
||
| factories: { | ||
| account: accountFactory, | ||
| alias: aliasFactory, | ||
| authMethod: authMethodFactory, | ||
| channelRecording: channelRecordingFactory, | ||
| connectionRecording: connectionRecordingFactory, | ||
| credentialLibrary: credentialLibraryFactory, | ||
| credentialStore: credentialStoreFactory, | ||
| credential: credentialFactory, | ||
| group: groupFactory, | ||
| hostCatalog: hostCatalogFactory, | ||
| hostSet: hostSetFactory, | ||
| host: hostFactory, | ||
| managedGroup: managedGroupFactory, | ||
| policy: policyFactory, | ||
| role: roleFactory, | ||
| scope: scopeFactory, | ||
| sessionRecording: sessionRecordingFactory, | ||
| session: sessionFactory, | ||
| storageBucket: storageBucketFactory, | ||
| target: targetFactory, | ||
| user: userFactory, | ||
| worker: workerFactory, | ||
| }, | ||
|
|
||
| models: { | ||
| ...discoverEmberDataModels(mirageConfig.store), | ||
| ...mirageConfig.models, | ||
| account: accountModel, | ||
| alias: aliasModel, | ||
| authMethod: authMethodModel, | ||
| base: baseModel, | ||
| channelRecording: channelRecordingModel, | ||
| connectionRecording: connectionRecordingModel, | ||
| credentialLibrary: credentialLibraryModel, | ||
| credentialStore: credentialStoreModel, | ||
| credential: credentialModel, | ||
| group: groupModel, | ||
| hostCatalog: hostCatalogModel, | ||
| hostSet: hostSetModel, | ||
| host: hostModel, | ||
| managedGroup: managedGroupModel, | ||
| policy: policyModel, | ||
| role: roleModel, | ||
| scope: scopeModel, | ||
| sessionRecording: sessionRecordingModel, | ||
| session: sessionModel, | ||
| storageBucket: storageBucketModel, | ||
| target: targetModel, | ||
| user: userModel, | ||
| worker: workerModel, | ||
| }, | ||
|
|
||
| serializers: { | ||
| account: accountSerializer, | ||
| alias: aliasSerializer, | ||
| application: applicationSerializer, | ||
| authMethod: authMethodSerializer, | ||
| channelRecording: channelRecordingSerializer, | ||
| connectionRecording: connectionRecordingSerializer, | ||
| credentialLibrary: credentialLibrarySerializer, | ||
| credentialStore: credentialStoreSerializer, | ||
| credential: credentialSerializer, | ||
| group: groupSerializer, | ||
| hostCatalog: hostCatalogSerializer, | ||
| hostSet: hostSetSerializer, | ||
| host: hostSerializer, | ||
| managedGroup: managedGroupSerializer, | ||
| policy: policySerializer, | ||
| role: roleSerializer, | ||
| scope: scopeSerializer, | ||
| sessionRecording: sessionRecordingSerializer, | ||
| session: sessionSerializer, | ||
| storageBucket: storageBucketSerializer, | ||
| target: targetSerializer, | ||
| user: userSerializer, | ||
| worker: workerSerializer, | ||
| }, | ||
| serializers: applyEmberDataSerializers(mirageConfig.serializers), | ||
|
|
||
| routes, | ||
| }; | ||
| return createServer(finalConfig); | ||
|
|
@@ -58,7 +211,7 @@ function routes() { | |
| }); | ||
|
|
||
| // make this `/api`, for example, if your API is namespaced | ||
| this.namespace = environmentConfig.api.namespace; | ||
| this.namespace = environmentConfig.api?.namespace; | ||
| // delay for each request, automatically set to 0 during testing | ||
| this.timing = 1; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think I got this but why don't we want to load the default scenario?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is encouraged approach by ember-cli-mirage that our apps have been following: