javascript - 开玩笑 - 如何检测是否调用了模块依赖项中的命名导出?
问题描述
// 依赖.js
export const getFontSize = () => {
// expensive computation...
};
// 设备.js
import { getFontSize } from './dependancy';
...
const initPxToEm = () => {
let fontSize;
return px => {
if (!fontSize) {
fontSize = getFontSize();
}
return stripUnit(em(px, fontSize));
};
};
const pxToEm = initPxToEm();
const doSomething = () => pxToEm(window.innerWidth);
我想测试 getFontSize 只被调用一次,即函数被记忆:
// test.js
import device from '../device';
import { getFontSize } from '../dependancy';
jest.mock('../dependancy');
it('should memoize the font size', () => {
device.doSomething();
expect(getFontSize).toHaveBeenCalled();
device.doSomething();
expect(getFontSize).not.toHaveBeenCalled();
});
但我明白了:
预期的模拟函数不会被调用,但它被调用了:[],[]
解决方案
您应该使用.toHaveBeenCalledTimes(number)来检查该getFontSize
函数是否仅被调用一次。此外,您忘记模拟函数的返回值getFontSize
,这将导致您的fontSize
变量永远不会有一个真正的值(undefined
)。所以每次调用device.doSomething()
方法都会调用getFontSize
被调用的函数。
例如
dependancy.js
export const getFontSize = () => {
// expensive computation...
};
device.js
:
import { getFontSize } from './dependancy';
const stripUnit = (val) => val;
const em = (px, fontSize) => px + fontSize;
const initPxToEm = () => {
let fontSize;
return (px) => {
if (!fontSize) {
fontSize = getFontSize();
}
return stripUnit(em(px, fontSize));
};
};
const pxToEm = initPxToEm();
const doSomething = () => pxToEm(window.innerWidth);
export default { doSomething };
device.test.js
:
import device from './device';
import { getFontSize } from './dependancy';
jest.mock('./dependancy');
it('should memoize the font size', () => {
getFontSize.mockReturnValue(16);
device.doSomething();
device.doSomething();
device.doSomething();
expect(getFontSize).toBeCalledTimes(1);
});
单元测试结果:
PASS examples/66557208/device.test.js
✓ should memoize the font size (3 ms)
---------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 83.33 | 100 |
dependancy.js | 100 | 100 | 0 | 100 |
device.js | 100 | 100 | 100 | 100 |
---------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 3.665 s, estimated 4 s
推荐阅读
- python-3.x - 如何让命令行连续打印日志信息?
- r - 有没有办法将线性判别系数提取到数据框中?
- mysql - 特定列的 MySQL 内部联接
- c++ - 在 Windows (OS) 中的 QListView 上没有正确省略
- anylogic - AnyLogic - 密度图可以更准确吗?
- c++ - 为什么 g++ 试图链接错误的 libstdc++.so 库?
- graph - Neo4j GraphSage gds.beta.graphSage.train 仅使用 4 个内核,尽管存在更多可用内核
- autodesk-forge - 如何从建筑物的每个角落找出 x,y,z 的位置?
- ruby-on-rails - 我如何在 JSON 中传递 API 的结果
- python-3.x - pandas/numpy - 基于时间的平均值