-
Notifications
You must be signed in to change notification settings - Fork 439
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
Uncaught Error: You must pass owner to startMirage() #2212
Comments
owner
to startMirage()
Digging through the stack trace, it looks like the issue arises from: ember-cli-mirage/addon/start-mirage.js Lines 19 to 23 in f6a9732
Our app doesn't currently This issue isn't encountered when using the classic Ember builds, it triggers once were using Is it a requirement that Mirage consumers export a Currently, we export a named |
Looks like this has come up briefly in Ember Discord in April: Problem: https://discord.com/channels/480462759797063690/568935504288940056/834541111065313330 Ed Faulkner's explanation: https://discord.com/channels/480462759797063690/568935504288940056/834888941881655306
|
The original version of mirage does require an export default from the config. Thats where it looks for routes. When Mirage was abstracted away to miragejs, I implemented a makeServer function that if you define, will no longer use the export default config, but instead use the makeServer to define the server which is very similar to the way miragejs now defines the server. The default export is there for backward compatibility, but hopefully people would start using the makerServer version. Since Sam is considering doing away with this addon, I am working on a replacement that would still provide emberish functionality, and part of that is removing the legacy export default being just the routes and instead be the makeServer function. If that is the only issue you are currently having with embroider I think we are ok. As a curiosity, what does your current config.js look like? |
Thanks for taking a look! A redacted version of our config is: import appConfig from 'my-app/config/environment'
import { createServer, discoverEmberDataModels } from 'ember-cli-mirage'
/**
* Pass all route handlers here, to be used in `makeServer` fn.
*/
function routes() {
this.passthrough('/write-coverage')
this.post('/my-api/resource')
this.delete('/my-api/resource/:id')
this.get('/my-api/resource/:id')
// and so on, for all Mirage routes used in development
}
/**
* Defines a centralized Mirage Server
*
* @param {Object} config - contains all modules that exist in the project's /mirage directory
* @returns {Object} - The mirage server
*/
export function makeServer(config) {
// `config` does not contain autogenerated Mirage model definitions by default
// `discoverEmberDataModels` will add the models to config, so that Mirage schema is automatically inferred from the Ember Data models and relationships
const models = discoverEmberDataModels(config)
const namespace = appConfig.APP.apiNamespace
const finalConfig = { ...config, models, routes, namespace }
return createServer(finalConfig)
} I've included comments from other devs, which may've informed my conception that Mirage is auto-generating models that could present problems in Embroider's static world. Note that the above config will trigger the reported error. To avoid this I have to add the following: export default function () {
// no-op function to appease Mirage
} So it's possible this is a config bug? It seems like Mirage is still trying to configure an |
Adding the empty A side issue is that it causes a string of test failures when building via embroider, with the error message:
I'm not yet sure if this is caused by the empty function, or whether there's something else Mirage is doing by default in tests that's causing it. |
discoverEmberDataModels does not take a parameter, it just discovers all the ember data models. If you have models you have also defined in mirage you will need to do the following Your make server should be
If you have the same model name in ember data and in mirage, the mirage one will win since config.models is last. The second Pretender instance is usually caused by mirage being started twice. If this is caused by embroider and you find the cause please let me know. Right now I get it when I do the tests wrong, like do a setupMirage in a parent and and a nested module for example. Lastly I guess I wasnt smart enough by doing some conditional require for the default, I plan for the makeServer to be the default and the only way to config. Right now though they are both there for backward compatibility. So the docs might say you dont have to export a default (but I guess under embroider you do until we deprecate and remove it). What this addon currently does is look for a default export and a makeServer function. If there is a makeServer function if will use that to create the server, if not it will use its internal logic to create a server. So you can make the routes function an export default, you dont have to create a second empty one. Once Mirage was extracted to MirageJS, many of the exports are just re-exports (https://github.com/miragejs/ember-cli-mirage/blob/master/addon/index.js#L2) and we plan to drop them and have comsumers directly import them from miragejs.. I cant figure out yet how to deprecate an export. Until then, I would highly suggest not importing from ember-cli-mirage that could be imported from mirageJs directly. So for example, a model defined in my mirage looks like this. You should also add miragejs to your package.json since you will not be referring to it directly.
Note that you have to use the extend syntax. It doesnt seem to work with class syntax and thats a mirageJS concern. You can import factories, traits etc. The main things that are provided by ember-cli-mirage is the setupMirage, startMirage, and the discoverEmberDataModels function. |
Thankyou so much for all your suggestions @cah-briangantzler. I've made a proposed fix PR in #2213 for the specific issue reported here. I'm still getting myriad test failures related to miragejs, , even after I amend my Mirage config, and consume the fix in the PR. I'm unable to understand them well enough to file new issues just yet. They may end up being user error. The issues still include myriad test failures relating to 2nd instances of Pretender being started, and I can confirm that we're not starting Mirage multiple times. In all cases we call import { setupMirage } from 'ember-cli-mirage/test-support';
module('my ember test', function (hooks) {
setupMirage(hooks) I'm also getting regular, but flaky, test failures with the message
...which are triggered from the following import statement inside a test file: import { Response } from 'miragejs' Again, I think these additional details are out of scope for the reported issue, and I'll open issues if I can get shareable repros happening. |
Hey @timiyay and @cah-briangantzler did either of you find a resolution for
We're getting the same thing once we turn on |
I have not seen the two server pretender problem. I would think that would come from calling setupMirage twice in the same test, or the setupMirage failing to hook into the afterEach to shut down at the end of the test. Looking back over this I see we were talking about the default export. When I created the makeServer and the way it was exposed I was unaware this was going to cause an issue for embroider. That has since been fixed in that if the default export takes 2 params, it assumes that it is the make server function. However I have been unable to actually release a new version of ember-cli-mirage containing this. |
Yeah I'm not entirely sure what we've got wrong tbh. I've compared with https://github.com/xg-wang/ember-realworld-fastboot which also uses Mirage but that seems to work perfectly fine. We're not using makeServer, just exporting the default function with routes in it. I'll keep digging and see if theres anything funky we're doing. |
Hi @rahulk94 , I have the same "You created a second Pretender instance" problem on an embroider project. After tracing code, I find the setting 'ember-cli-mirage': {
enabled: false,
}, can turn off the warning. And I find https://github.com/xg-wang/ember-realworld-fastboot/blob/master/config/environment.js#L41 have the same setting; |
That setting is saying to disable ember-cli-mirage completely. By default it is on in tests and on in dev (if --proxy is not passed) and off in production. see https://www.ember-cli-mirage.com/docs/advanced/environment-options#enabled By setting the enable to false you are disabling ember-cli-mirage all the time. Not a fast boot user but somehow two were being created and by setting this you are stopping the one. What Im now curious of is, how is the second one getting created since the setting is telling ember-cli-mirage to be disabled all the time :) |
Woah @bistin that is so weird... Thanks so much for the tip. I've disabled this in our app along with a comment referring readers here. @cah-briangantzler yup so weird. It def seems like some quirk of using Mirage and Fastboot together. Fwiw we're currently using |
Are you able to close this issue? |
Came across this issue while updating ember data from 3.28 to 4.7.1. In the test suite I am seeing
Debugging the app and only changing the ember data version I get to
The instance that I want to start is in a customisable test setup http://emberjs.github.io/rfcs/0637-customizable-test-setups.html e.g. export function setupApplicationTest(hooks, options) {
setupMirage(hooks);
} Looking further up the stack I get to
isTesting is returning false in 4.7.1 but true in 3.28 not sure how to debug any further
|
Using the suggested workaround works for me
|
Where did you get the suggested workaround? |
Earlier in this thread #2212 (comment) |
Thats embarrassing :) That flag is one I hope to remove one day and not have ember-cli-mirage make assumptions about what should happen. By having you specifically call code when you want mirage to run in production or development it will be easier to tell what is happening and why. It will also be easier to specify different routes and data for each environment |
Have tracked my issue with two pretender instances starting due to |
Nice. I have seen this issue reported before. Thanks for all the effort, hope it gets resolved there. Thanks for the link, I will follow the issue |
When converting an Ember 3.24 app to use
embroider
, we can no longer boot the app from the Ember dev server.The app crashes in the browser, with an error:
I will endeavour to create a repo to reproduce the issue - the contributing guidelines pointed me to https://github.com/miragejs-archive/ember-cli-mirage-boilerplate which appears unmaintained, and is using classic Ember code (our issue is on an Octane 3.24 app).
The text was updated successfully, but these errors were encountered: