Ember + Mirage: response mock errors

892 views Asked by At
  // Some code from router
  return user.save().catch(e => {
    debugger; // says: ErrorClass {isAdapterError: true, stack: "Error: The adapter rejected the commit because it was invalid}
  });

  // Test
  import { Response } from 'miragejs';

  test('Show error page when failed uniqueness validation', async function(assert) {
    server.post('/users', () => {
      return new Response(422, {}, { errors: [{ detail: 'has already been taken' }] });
    });

    await visit('/users/[email protected]');
  });

I'm using Ember 3.16 and ember-cli-mirage 1.1.8. I have a test which is not working properly. I'm mocking a 422 Response from mirage. This response also contains some errors. In the router, where the request is made, the request fails (enters the catch) but when I inspect the error it doesn't tell me the status code or the details of it

1

There are 1 answers

1
xploshioOn On

looking at the documentation from emberCLI Mirage V1.1.8, it shows this example to test errors

test('the user sees an error if the save attempt fails', async function(assert) {
   this.server.post('/questions', () => ({
     errors: [ 'The database went on vacation' ]
   }), 500);

   await visit('/');
   await click('.new');
   await fillIn('input', 'New question');
   await click('.save');

   assert.dom('h2').hasText('The database went on vacation');
});

it's a different syntax to specify the error and the status response