Search code examples
ember.jsember-cli-miragepretender.js

ember-cli-mirage error: Nothing returned by handler, but handler exists


In the app I'm working with, we have a GET route that validates a user's email address. If the email is invalid, the server responds with:

  • a 200 status code
  • response headers with Content-Type:application/json; charset=utf-8
  • and the response data itself is just a string of "This email is invalid"

I'm trying to simulate this in ember-cli-mirage by doing:

this.get('/ember_api/v1/validations/validate_email', function() {
  return [200, { 'Content-Type': 'application/json' }, "This email is invalid"];

  // also tried this:
  // return new Mirage.Response(200, { 'Content-Type': 'application/json' }, JSON.stringify({"message":"This email is invalid"}));

  // and tried this:
  // return "This email is invalid";
});

The test itself is a button click that fires off this request:

GET "/ember_api/v1/validations/validate_email?email=fakey%40fakefakefake.com&skip_uniq=true"

...and the error I'm getting is:

Pretender intercepted GET /ember_api/v1/validations/validate_email?email=tom%40gmail.com&skip_uniq=true but encountered an error: Nothing returned by handler for /ember_api/v1/validations/validate_email?email=tom%40gmail.com&skip_uniq=true. Remember to return [status, headers, body]; in your route handler.`

It's asking me to return [status, headers, body], but I'm doing this in my handler, and it still throws the error.

Is this actually an issue with the response? Do I need to edit my API to actually return a JSON API formatted object, so I can write the test that way?

I feel like I should be able to return a string in my test since that's what the app is doing. Any help is appreciated.


Solution

  • The this.get you are using is the Mirage version. You can also use this.pretender.get which should work with your current code sample ...