[英]How to force Jest unit test assertion to execute before test ends?
我在我的反應本機應用程序下面的測試,但測試應該會失敗(因為返回的動作不等於我把行動expectedActions
。我的猜測是,它是通過因為expect
測試的測試完成后運行。
如何強制測試等待,直到諾言完成並且期望測試運行? 還有另一種方法嗎?
describe('authorize actions', () => {
beforeEach(() => {
store = mockStore({});
});
it('should create an action to signify successful auth', () => {
auth.authorize.mockImplementation(() => Promise.resolve({"something": "value"}));
const expectedActions = [{"type":"AUTHORIZE_RESPONSE","payload":{"something":"sdklfjsdf"}}];
authorizeUser(store.dispatch, store.state).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
})
});
好的,看來我只是錯過了一些Jest文檔-如果您返回諾言,即return auth.authorize.mockImplementation(() => Promise.resolve...
那么Jest將等待直到完成為止再繼續。
有多種測試異步代碼的方法。 檢查文檔以獲取示例: https : //facebook.github.io/jest/docs/en/asynchronous.html
一個人可能正在兌現承諾:
describe('authorize actions', () => {
beforeEach(() => {
store = mockStore({});
});
it('should create an action to signify successful auth', () => {
auth.authorize.mockImplementation(() => Promise.resolve({"something": "value"}));
const expectedActions = [{"type":"AUTHORIZE_RESPONSE","payload":{"something":"sdklfjsdf"}}];
return authorizeUser(store.dispatch, store.state).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
})
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.