1

I'm stuck with writing unit tests for my code that uses rsvp promises. I tried to use pit tests but had no luck in making tests pass for rsvp, however embedded promises work just fine:

//jest.autoMockOff(); - even with this rsvp test is failing
jest.unmock('rsvp');

import rsvp from 'rsvp';

describe('my rsvp tests', () => {

  // this test fails
  pit('testing rsvp promise', () => {
    return new rsvp.Promise((resolve) => {
      resolve("getting something");
    }).then(()=> { expect(1).toBe(1); });
  });

  // this test passes
  pit('testing pure promise', () => {
    return new Promise((resolve) => {
      resolve("getting something");
    }).then(()=> { expect(1).toBe(1); });
  });
});

Relevant details from my package.json:

"rsvp": "^3.2.1",
"babelify": "^7.2.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
"babel-jest": "^9.0.0",
"jest-cli": "*"
 ...
"scripts": {
  "test": "jest"
},

"jest": {
  "unmockedModulePathPatterns": [
    "<rootDir>/node_modules/react",
    "<rootDir>/node_modules/react-dom",
    "<rootDir>/node_modules/react-addons-test-utils"
  ]
}

.babelrc:

{
  "presets": ["es2015", "react", "stage-0"]
}

I see two possible workarounds but like none of them:

  1. Mock rsvp promise by using embedded promise. Disadvantage: unit tests will become more verbose, I'll need to mock another functions such as rsvp.all that I don't want to do.
  2. Migrate from rsvp to embedded promises. It is possible, however I already use one library that depends on rsvp that makes ajax requests. I'm also not sure that embedded promises can replace everything that rsvp provides (such as 'all' and the other helper functions).
Anton Sarov
  • 3,712
  • 3
  • 31
  • 48
Alex
  • 2,916
  • 3
  • 22
  • 27

1 Answers1

1

I found a solution - or, better say, a workaround. In short: there is no need to use pit methods, they can be replaced with 'it', but before completion all timers should be run, it can be done by calling jest.runAllTimers().

It is not an elegant solution as I don't see any reason why rsvp promises shouldn't work in jest as opposed to 'embedded' ones, but at least it works.

Alex
  • 2,916
  • 3
  • 22
  • 27