mjackson / expect

Write better assertions
MIT License
2.29k stars 117 forks source link

not getting proper error messages inside of promise #172

Closed falconmick closed 6 years ago

falconmick commented 7 years ago

Hey.

I am currently writing some React Mocha tests. When I call the following my test times out rather than giving me an error message of which expect failed.

      store.dispatch(actions.updateStudentProjects(studentName))
        .then(() => {
          const actions = store.getActions();
          expect(actions.length).toEqual(3);
          expect(actions[0].type).toEqual(types.BEGIN_AJAX_CALL);
          expect(actions[1].type).toEqual(types.UPDATE_STUDENT_SUCCESS);
          expect(actions[2].type).toEqual(types.END_AJAX_CALL);
          done();
        });

any ideas?

entire test code:

import expect from 'expect';
import thunk from 'redux-thunk';
import nock from 'nock';
import configureMockStore from 'redux-mock-store';
import {loremIpsum} from '../utils/loremIpsum';
import * as actions from './studentActions';
import * as types from '../constants/actionTypes';

const middleware = [thunk];
const mockStore = configureMockStore(middleware);
const studentName = 'studenta';
const dummyProject = {
  "description": loremIpsum(150),
  "instructions": 'Do Stuff!',
  "history": {
    "created": "2007-03-07T14:48:22.000Z",
    "modified": "2007-03-07T14:48:22.000Z",
    "shared": "2007-03-07T14:48:22.000Z"
  },
  "stats": {
    "views": 669,
    "loves": 19,
    "favorites": 19,
    "comments": 58
  }
};

let idIncrementor = 0;

describe('Student Actions', () => {
  afterEach(() => {
    nock.cleanAll();
  });

  describe('Update Student Project List', () => {
    it('should dispatch individual updates then a complete action', (done) => {
      // ARRANGE
      // nock api call
      nock('https://api.scratch.mit.edu')
        // .log(console.log)
        .get(`/user/${studentName}/projects`)
        .reply(200, uri => {
          const proj1 = Object.assign({}, dummyProject, {
            "id": idIncrementor++,
            "title": `${studentName} - Projecti 1`,
          });
          const proj2 = Object.assign({}, dummyProject, {
            "id": idIncrementor++,
            "title": `${studentName} - Projecti 2`,
          });
          return {
              proj1, proj2
          }
        });

      const expectedActions = [
        { type: types.BEGIN_AJAX_CALL},
        { type: types.UPDATE_STUDENT_SUCCESS },
        { type: types.END_AJAX_CALL }
      ];
      const store = mockStore({projects: [], users: []}, expectedActions, done);

      // ACT
      store.dispatch(actions.updateStudentProjects(studentName))
        .then(() => {
          const actions = store.getActions();

          // ASSERT
          expect(actions.length).toEqual(3);
          expect(actions[0].type).toEqual(types.BEGIN_AJAX_CALL);
          expect(actions[1].type).toEqual(types.UPDATE_STUDENT_SUCCESS);
          expect(actions[2].type).toEqual(types.END_AJAX_CALL);
          done();
        });
    });
  });
});
ljharb commented 7 years ago

Typically you want to just return a promise in mocha tests, and not use done at all. Otherwise, you need .catch(done) on the promise, or else mocha won't be able to know that expect threw.

falconmick commented 7 years ago

I will have a go later, thanks.

ljharb commented 6 years ago

Happy to reopen if this is still an issue.