我有一个非常奇怪的情况,我的 Jest 测试通过了我的 Windows 10 桌面和 Macbook Pro,但它们没有通过我其他 friend 的两个 Windows 10 桌面。
正在测试的代码
import { addTerminalItem } from '../../store'
...
class LoginUser extends EventHandler {
...
async handle () {
if (this.isFromOauthRedirect) {
try {
await this._handleOauthRedirect()
} catch (e) {
addTerminalItem(new ErrorMessage(e.message))
}
return
}
if (await zaClient.isUserLoggedIn('testUserId')) {
// TODO: user is already logged in, do something
} else {
const loginStartSecret = uuidv4()
localStorage.setItem(LOGIN_START_SECRET, loginStartSecret)
addTerminalItem(new LoginMessage(loginStartSecret))
}
}
...
}
export const loginUser = new LoginUser()
测试代码执行以下操作:
LOGIN_START_SECRET
以便实际代码在输入第一个 catch
时引发异常. WELCOME_MESSAGE_RENDERED
订阅事件处理程序. store.addTerminalItem
模块功能。 async handle()
功能被触发。 import * as store from '../../../store'
...
test('different login start secret in localstorage', async () => {
localStorage.setItem(LOGIN_START_SECRET, 'different-secret')
zaClient.login = jest.fn(() => true)
store.addTerminalItem = jest.fn()
await pubsub.publish(WELCOME_MESSAGE_RENDERED)
expect(store.addTerminalItem).toHaveBeenCalledWith(expect.any(ErrorMessage))
const errorMessage = store.addTerminalItem.mock.calls[0][0]
expect(errorMessage.message).toBe(loginSecurityErrorMsg)
})
正如我在我的电脑上所说,它正确显示 addTerminalItem
在我家里的两台机器上使用正确的参数调用一次函数。然而,这个模拟函数永远不会在我 friend 的两台机器上被调用并且失败。他们得到的实际错误信息如下:expect(jest.fn()).toHaveBeenCalledWith(...expected)
Expected: Any<ErrorMessage>
Number of calls: 0
以下是我们迄今为止尝试过的以下事情:git clone
, yarn install
, 和 yarn test
.我通过了,他们没有。 addTerminalItem
mock ,我们添加了 console.log
里面 addTerminalItem
它正确地没有记录,但仍然是 0 次调用。 addTerminalItem
发现,我们添加了 console.log
里面 addTerminalItem
它正确记录,但仍然是 0 次调用(这对我来说毫无意义)如果有人可以在这里给我们任何指示,将不胜感激。
最佳答案
没有手头的代码很难确定,但请尝试使用 jest.mock
:
import {addTerminalItem} from "../../../store";
jest.mock('../../../store', () => ({
addTerminalItem: jest.fn()
));
//... stuff ...
test('different login start secret in localstorage', async () => {
localStorage.setItem(LOGIN_START_SECRET, 'different-secret')
zaClient.login = jest.fn(() => true)
await pubsub.publish(WELCOME_MESSAGE_RENDERED)
expect(addTerminalItem).toHaveBeenCalledWith(expect.any(ErrorMessage))
const errorMessage = addTerminalItem.mock.calls[0][0]
expect(errorMessage.message).toBe(loginSecurityErrorMsg)
})
关于javascript - 永远不会调用带有 Jest 的模拟模块函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62646967/