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:
- 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.
- 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).