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

Add notice for testing with fake servers #2

Closed
zeppelin opened this issue Aug 23, 2018 · 16 comments
Closed

Add notice for testing with fake servers #2

zeppelin opened this issue Aug 23, 2018 · 16 comments

Comments

@zeppelin
Copy link

For example, Mirage needs this, otherwise requesting /__fastboot-testing fails:

// mirage/config.js
export default function() {
  this.passthrough('/__fastboot-testing');
  // ...
}
@ryanto
Copy link
Member

ryanto commented Aug 31, 2018

Thanks for the report!

I don't think this should be needed, since in the fastboot test you should not be starting the mirage server. Is your fastboot test turning on mirage?

cc @samselikoff

@ryanto
Copy link
Member

ryanto commented Sep 19, 2018

Just wanted to follow up on this. Mind sharing your fastboot test with me that's requiring you to do this?

@CvX
Copy link
Collaborator

CvX commented Sep 20, 2018

I had the same "issue", i.e. had to define the passthrough path in mirage config. Here's my test:

import { module, test } from 'qunit';
import { setup, visit } from 'ember-cli-fastboot-testing/test-support';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';

module('FastBoot | product list test', function(hooks) {
  setup(hooks);
  setupMirage(hooks);

  hooks.beforeEach(function() {
    this.server.create('home');
  });

  test('it has the correct page title', async function(assert) {
    let { htmlDocument } = await visit('/some/path');

    assert.dom('head title', htmlDocument).hasText('The title');
  });
});

@ryanto
Copy link
Member

ryanto commented Sep 22, 2018

Ah I see. You should remove setupMirage(hooks) from this test because it's not doing what you think it's doing... let me explain why.

Mirage runs in the browser. It works by intercepting XHR requests and returning the data you've created with factories.

Fastboot testing runs inside of ember-cli. When you run fastboot testing's visit('/some/path'), you're asking ember-cli's node instance to generate the fastboot html for /some/path. Since this request runs inside of node, and not the browser, there's no way mirage can intercept and serve those AJAX requests.

That means any factory data you create will inside of these tests cannot be use, since the browser cannot intercept XHR requests made by node. There's currently some work being done to get mirage running inside of fastboot. When that happens we'll make sure this addon and mirage play nicely together!

I'm going to close this issue, but feel free to reopen with any questions.

@ryanto ryanto closed this as completed Sep 22, 2018
@zeppelin
Copy link
Author

Yes, so it's not about mocking HTTP requests, but if Mirage is started automatically (which is likely the case with older Mirage & testing setups), it'll prevent ECFT from accessing the middleware from the browser.

@ryanto
Copy link
Member

ryanto commented Sep 24, 2018

Yup, good point. I would recommend using the generator to create the fastboot tests, they'll ensure the new style test that won't autostart mirage. it works great, even inside of 2.x apps.

I'm a little torn on adding direct knowledge of mirage into this addon. Not everyone uses mirage and having this addon aware of all the ways XHR requests get intercepted could be tricky, that's mainly where my pushback is coming from.

At the end of the day if it helps users get unstuck then you can convince me it's good for the project. Would having this in the docs have helped you to find the issue faster?

@ryanto ryanto reopened this Sep 24, 2018
@ryanto
Copy link
Member

ryanto commented Sep 24, 2018

Ok after discussing this with Sam I think we can detect the XHR request failing and log an error that tells the user they might be running a fake server. That allows us to not worry about detecting mirage, but gives the user valuable info on why the test is failing.

I'll get a few test cases wired up that use mirage and make sure an error is shown.

@williamweckl
Copy link

Is there a way to mock http requests when testing fastboot? Tried using mirage, pretender, factory guy and had no success...

@williamweckl
Copy link

Now I've tried to use ember-fetch AdapterFetch mixin at application adapter too...
Is there any instructions to mock http requests? Could be added to readme?
Thank you.

@samselikoff
Copy link
Contributor

@williamweckl It's on Mirage's roadmap for sure, but hasn't been built yet. Focused on shipping Mirage 1.0 first.

@williamweckl
Copy link

@samselikoff but is there a way to work this out yet? I don't think I can use this lib if I can not mock http requests... Any uggly way works for me now until mirage don't release this feature.

@samselikoff
Copy link
Contributor

@ryanto Isn't there an ugly way that we mock HTTP requests in fastboot during tests? Any code we can share?

@ryanto
Copy link
Member

ryanto commented Apr 18, 2019

Hi @williamweckl - Since fastboot runs in a node process, it's best to use a node http mocking library like nock to accomplish this.

There's currently work in #36 that's attempting to wire all this up for you. It's unfinished, but you can poke around that branch. We're pretty happy with where we landed, but since it hasn't been merged the API might change.

Also, I created a new issue to discuss network mocking here. This original issue was opened to discuss mirage intercepting ECFT's call to ember-cli when it shouldn't be. I'd like to keep this issue focused on that.

@williamweckl
Copy link

@ryanto thanks for answering this! I'll probably wait for the feature release, tried to use nock but I don't know how to import non ember dependencies to my project, sorry but I'm a beginner in this kind of stuff. I'm using ember for a wile but never had to do that.

If someone have some examples to show me how to use nock with this lib would be great, if not I can wait for the release.

Thanks a lot for the amazing job done so far!

@ryanto
Copy link
Member

ryanto commented Apr 27, 2019

Sounds good @williamweckl. I'd say if you can, wait for the release. I don't have a timeline for it, but it's a high priority for me since I need it in one of my apps :)

@ryanto
Copy link
Member

ryanto commented Apr 27, 2019

Hey @zeppelin @CvX - This has been fixed and released as 0.0.13. For now I'm only printing a nice error message when we detect a fake server. Maybe we can improve this with some better docs in the future.

@ryanto ryanto closed this as completed Apr 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants