[英]jest test. How do you test function call within a function from same module?
我正在使用 Jest 在我的項目中進行測試。
這是我的情況。
//myModule.js
export const funcA = () =>{};
export const funcB = ()=>{
funcA();
}
//myModule.test.js
import {funcA,funcB} from './myModule'
describe("funcB",()=>{
it ("should call funcA",
()=>{
funcB(); // execute b function
expect(funcB).toHaveBeenCalled(); // hypothetically I would like to do this. check funcA
}
)
});
有沒有辦法實現這一目標? 我目前的情況不允許我將 funcA 作為 funcB 的參數傳遞。
任何想法和想法都會有所幫助! 謝謝
這是單元測試解決方案:
myModule.ts
:
export let funcA = () => {};
export const funcB = () => {
exports.funcA();
};
myModule.test.ts
:
import * as mod from './myModule';
describe('funcB', () => {
it('should call funcA', () => {
jest.spyOn(mod, 'funcA');
mod.funcB();
expect(mod.funcA).toHaveBeenCalled();
});
});
帶有覆蓋率報告的單元測試結果:
PASS stackoverflow/60221752/myModule.test.ts (8.763s)
funcB
✓ should call funcA (3ms)
-------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
myModule.ts | 100 | 100 | 100 | 100 |
-------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 10.308s
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.