[英]How to test if a wrapper in Jest calls skip and it correctly?
我有一個名為assertTruthy
for Jest的函數。 assertTruthy(msg, fn, args)
,它期望消息,函數和參數,並且如果使用參數調用函數時返回的內容為真,則應通過,否則為false。
我想擴展它以支持Jest's only
和skip
。
這是我寫的:
assertTruthy.skip = ({
message = '',
fn = undefined,
args,
} = {}) => {
it.skip(message, () => {
expect(fn(args)).toBeTruthy();
});
};
assertTruthy.only = ({
message = '',
fn = undefined,
args,
} = {}) => {
it.only(message, () => {
expect(fn(args)).toBeTruthy();
});
};
我將如何測試這些功能?
這是我嘗試過的方法,可以起作用,但是我不確定這是否正確。
describe('skip()', () => {
test('it skips the function', () => {
it.skip = jest.fn();
assertTruthy.skip({
message: 'something',
fn: () => true,
args: undefined,
});
expect(it.skip).toHaveBeenCalledTimes(1);
});
});
這看起來像一個足夠公平的測試,您的assertTruthy
skip
並且only
調用Jest's it
skip
and only
方法。
您可能想斷言它還會使用toHaveBeenCalledWith
使用您期望的參數調用它們。
您能否詳細說明一下您想要實現的目標,而我卻沒有得到正確的解釋,為什么您要擴展Jest skip和僅實現已測試目標的方法。
但是,如果您只想根據是否為真/假的參數來測試是否未使用toHaveBeenCalledTimes()
調用/執行某個函數,那么您就做對了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.