I have created a class called AddUserToWaitlist that takes user's email address from the input in the react component and inserts it to the waitlist table in the rethinkdb database. It also returns a callback function which callback(userid) if the operation was successful.
AddUserToWaitlist is my first class to begin using Jest. I wrote a small test function as follows (i am not sure this is the right approach to test database insertion):
//@flow
jest.unmock('../waitlist')
describe('AddUserToWaitlist', () => {
it('saves user to the db table waitlist and returns the user id as an arg of the callback', () => {
const AddUserToWaitlistClass = require('../waitlist')
const AddUserToWaitlist = AddUserToWaitlistClass.AddUserToWaitlist
const time = new Date()
const schema = {
join_date: time.toISOString(),
email: '[email protected]'
}
const callback = jest.fn()
AddUserToWaitlist('waitlist', schema, callback)
expect(callback).toBeCalled()
})
})
At first try it gave me:
- TypeError: Cannot set property 'noConflict' of undefined
at Object.<anonymous> (node_modules/bluebird/js/main/bluebird.js:10:21)
at Object.<anonymous> (node_modules/rethinkdb/cursor.js:14:11)
...
I have added bluebird to my unmockedModulePathPatterns list. Then same error occurs for rethinkdb package. I added rethinkdb package to unmockedModulePathPatterns list also.
Then at my third try:
- Expected to be called at least once
at stack (node_modules/jest-jasmine2/vendor/jasmine-2.4.1.js:1580:17)
at Object.buildExpectationResult (node_modules/jest-jasmine2/vendor/jasmine-2.4.1.js:1550:14)
...
After all of that i have questions:
Hey! This doesn't seem like a problem with Jest and you are heading down the right path. I'll close this issue but if it turns out to be an issue with Jest, I'd be happy to reopen and fix whatever issue Jest may have.
Unmocking bluebird and rethinkdb makes sense based on what you are trying to do, however Jest is a unit test framework and the idea is usually to not make network requests (or write to databases) in a unit test. It makes a lot of sense to create a manual mock ( facebook.github.io/jest/docs/manual-mocks.html#content ) for RethinkDB and keep the database contents in memory during your test.
As to why your test is failing currently: I will expect the callback is executed asynchronously when the operation has finished, so you need to wait for that. To test promises, see this: http://facebook.github.io/jest/docs/tutorial-async.html#content
Jasmine 2 also passes a "done" argument to the spec that you can use to wait for the completion of an async operation – see http://jasmine.github.io/2.0/introduction.html#section-Asynchronous_Support
I hope this helps :)
Most helpful comment
Hey! This doesn't seem like a problem with Jest and you are heading down the right path. I'll close this issue but if it turns out to be an issue with Jest, I'd be happy to reopen and fix whatever issue Jest may have.
Unmocking bluebird and rethinkdb makes sense based on what you are trying to do, however Jest is a unit test framework and the idea is usually to not make network requests (or write to databases) in a unit test. It makes a lot of sense to create a manual mock ( facebook.github.io/jest/docs/manual-mocks.html#content ) for RethinkDB and keep the database contents in memory during your test.
As to why your test is failing currently: I will expect the callback is executed asynchronously when the operation has finished, so you need to wait for that. To test promises, see this: http://facebook.github.io/jest/docs/tutorial-async.html#content
Jasmine 2 also passes a "done" argument to the spec that you can use to wait for the completion of an async operation – see http://jasmine.github.io/2.0/introduction.html#section-Asynchronous_Support
I hope this helps :)