2
votes

I am trying to make use of the api-stubs provided in ember-app-kit for testing.

The stubs work correctly when I view the site in the browser, and when I run the tests by visting /tests in the browser.

However, when I run the tests from the command line with testem (by running grunt test:server) the tests fail as the stub data isn't returned.

You can find a minimal example demonstrating the problem here: https://github.com/tomclose/minimal_eak_test_stub_problem, created from the most recent version of ember-app-kit.

What am I doing wrong?

2
I've been banging my head against a wall to accomplish this as well, but with ember-simple-auth login forms. It doesn't look like EAK stubs are available for Testem. I'm not sure how to fix this.David

2 Answers

1
votes

Because the testem stuff runs on port (7239 is it?) you a) can't run the apistub server ont he same port (already bound).

Thus, you have to run the normal app (port 8000), let testem run on its own port.. and allow testem via the 'host' param in an adapter go find the right host:port combination to actually get data.

As well, the addition of the cors() filter will make the :8000 test server willing to send data back to a "cross domain request" from something on port 7239

The use of sinon is a different case (don't want to use apistub at all). I actually settled on NOT trying to use apistub for testing, but certainly the above approach works great.

2
votes

rstudner contacted me on irc and helped me to solve this problem. The basic idea is to keep a grunt server running and to use config/environments/test.js to configure your test api requests to hit this server.

Here's what worked:

  1. Change app/adapters/application.js to point towards a configurable host:

    export default DS.RESTAdapter.extend({
      namespace: 'api',
      host: window.ENV.host
    });
    
  2. Edit config/environments/test.js to configure tests to hit the instance of your app running as grunt server:

    window.ENV.host = 'http://localhost:8000'
    
  3. You then run into CORS problems. To solve these, add the dependency "cors": "2.2.0", to package.json and then run npm install. Then ..

  4. In tasks/express-server.js on line 8 add:

    var cors = require('cors');
    

    and on line 25 add:

    app.use(cors());
    

You can find an updated working version at https://github.com/tomclose/minimal_eak_test_stub_problem.

rstudner also mentioned that he actually uses sinon.js to fake the server 100% of the time (and then uses selenium for some full-stack tests), rather than following this approach.