简体   繁体   English

开玩笑测试。 您如何测试来自同一模块的函数内的函数调用?

[英]jest test. How do you test function call within a function from same module?

I am using Jest to test in my project.我正在使用 Jest 在我的项目中进行测试。

Here is my situation.这是我的情况。

//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
    }
  )
});

Is there a way to achieve this?有没有办法实现这一目标? My current situation does not allow me to pass funcA as argument of funcB.我目前的情况不允许我将 funcA 作为 funcB 的参数传递。

Any thoughts and ideas will be helpful!任何想法和想法都会有所帮助! Thanks谢谢

Here is the unit test solution:这是单元测试解决方案:

myModule.ts : myModule.ts

export let funcA = () => {};
export const funcB = () => {
  exports.funcA();
};

myModule.test.ts : myModule.test.ts

import * as mod from './myModule';

describe('funcB', () => {
  it('should call funcA', () => {
    jest.spyOn(mod, 'funcA');
    mod.funcB();
    expect(mod.funcA).toHaveBeenCalled();
  });
});

unit test results with coverage report:带有覆盖率报告的单元测试结果:

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM