-
Notifications
You must be signed in to change notification settings - Fork 24.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Promises broken in Jest tests #6104
Comments
Summary: We weren't exposing a mock for the batched bridge, which resulted in the WebSocket test failure but only in the open source copy of react-native. public Reviewed By: voideanvalue, vjeux Differential Revision: D2782151 fb-gh-sync-id: e896097dd6060bc26963bc4c23db87b7277b3eba
@cpojer :) |
Ugh, this is so annoying that it keeps popping up. Once the testing docs are rewritten, this will hopefully not be an issue any longer. I'll take a look soon. |
great, sorry for bothering you on a Sunday :) |
Any update on this issue? I've just started getting it and don't really want to touch the library... |
Thank you for your patience. We launched Jest 14.0 with experimental react-native support: Please feel free to create new issues after trying out the new integration if any issues remain. |
@cpojer
I was trying to mock VersionA
var AsyncStorage = {
getItem: jest.fn().mockReturnValue(
new Promise((resolve, reject) => {
resolve(JSON.stringify({
id: 32,
name: "Grace Han"
}))
})
),
setItem: jest.fn(),
}
module.exports = AsyncStorage
import AsyncStorage from './AsyncStorage'
jest.setMock('AsyncStorage', AsyncStorage)
// Some other test code
it('should get user info', () => {
const expectedActions = [
{ type: PATIENT_AUTH.GET_DEFAULT_USERINFO }
]
const store = mockStore({})
//the action: getUserInfo will call AsyncStorage.getItem()
return store.dispatch(actions.getUserInfo())
.then(() => {
expect(store.getActions()[0].type).toEqual(expectedActions[0].type)
})
}) VersionB
var AsyncStorage = {
getItem: jest.fn(),
setItem: jest.fn(),
}
module.exports = AsyncStorage
// Some other test code
it('should get user info', () => {
const expectedActions = [
{ type: PATIENT_AUTH.GET_DEFAULT_USERINFO }
]
const store = mockStore({})
const AsyncStorage = require('react-native').AsyncStorage
AsyncStorage.getItem.mockReturnValue(
new Promise((resolve, reject) => {
resolve(JSON.stringify({
id: 32,
name: "Grace Han"
}))
})
)
//the action: getUserInfo will call AsyncStorage.getItem()
return store.dispatch(actions.getUserInfo())
.then(() => {
expect(store.getActions()[0].type).toEqual(expectedActions[0].type)
})
}) Actually |
Yeah. I'm getting weird results:
both time out instead of hitting the expectation For what it's worth:
|
I'm wondering is this issue still not fixed ? (jest-cli: 14.1.0) first time I noticed that replacing |
looks like it's fixed on "jest-cli": "15.1.1". |
I was still seeing this issue with v15.1.1, but fixed it by saving Promise, then restoring it after requiring (not importing) react-native. //importing 'react-native' seems to break Promise and setTimeout.
//use require() instead of import, so we can save these first, and restore them after require('react-native')
const SavePromise = Promise;
const saveSetTimeout = setTimeout;
import React, { Component } from 'react';
const {
View,
Text,
StyleSheet,
Dimensions,
Platform
} = require('react-native');
Promise = SavePromise;
setTimeout = saveSetTimeout;
it("promises work?", () => {
return Promise.resolve().then(() => {
expect(true).toBe(true)
})
})
it("async works?", async () => {
await Promise.resolve()
expect(true).toBe(true)
})
it('works with async/await', async () => {
const userName = await 'Mark'
expect(userName).toEqual('Mark');
});
it('works after timeout', () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
expect(true).toBe(true)
resolve()
}, 10)
})
}) |
Since this issue has been closed, I opened a new one here with updated details and the latest versions of Jest and React Native. |
We have a very simple test case that reproduces this issue:
result:
expected:
We've tracked it down to this commit:
3ff3987
and this line in file
jestSupport/env.js
When we comment out this line the promise resolves as expected.
The text was updated successfully, but these errors were encountered: