我正在 Jest 测试我的 vue.js 应用程序。 我意识到 jest 刚刚通过了所有这些测试,即使它应该失败。 例如,这个应该失败,因为输入类型密码是 true 但我所有的测试代码都通过了 true 或 false。 ..
test('password input field type is password at the beginning', done => {
setTimeout(() => {
const pw = wrapper.find({ ref: 'pw' });
expect(pw.contains("[type='password']")).toBe(false);
}, 10);
done();
});
beforeEach(() => {
// Create the Store mock
state = {
auth: {
something: false,
},
data: {},
};
getters = {
somethingg: jest.fn(),
};
actions = {
'auth/login': jest.fn(),
};
store = new Store({
state,
getters,
actions
});
wrapper = shallowMount(TheLoginComponent, {
propsData: {},
mocks: {},
stubs: {
},
methods: {
},
data: {},
localVue,
store
});
});
afterEach(() => {
wrapper.destroy();
});
最佳答案
在这种情况下,setTimeout 不适用于 jest。它是异步的,不保证它在 Jest 完成之前执行。
如果您有异步调用让您编写它,请像下面那样刷新 Promises 并尝试:
const flushPromises = new Promise(resolve=> setImmediate(resolve)) ;
内部测试:(注意方法是异步的)
it('dummyTest', async() => {
//在这里渲染
等待 flushPromises() ;
//在这里断言
}) ;
关于javascript - 开 Jest 刚刚通过了所有测试,即使它是假的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58430847/