如何测试在开玩笑的对象中返回的函数
How to test a function which is returned within an object in jest
我想测试我的 util 函数,它包含通过 html2canvas、
生成 canvas 的选项
const getCanvasOptions = () => {
const { devicePixelRatio } = window;
const { scrollHeight, scrollWidth } = document.body;
const ratio = devicePixelRatio < 2 ? devicePixelRatio : devicePixelRatio / 2;
const width = scrollWidth * ratio;
const height = scrollHeight * ratio;
return {
allowTaint: true,
letterRendering: 1,
foreignObjectRendering: true,
quality: 1,
width: width,
height: height,
scale: ratio,
useCORS: true,
ignoreElements: (node) => {
return node.nodeName === 'NOSCRIPT';
}
};
};
我通过模拟 document
和 window
对象来测试它,然后 strictEqual
检查 expected
和 actual
返回的对象。但是在我的报道中,它表明下面的行是未经测试的,
return node.nodeName === 'NOSCRIPT'
如何在 jest 中测试上面的行?
你可以在你的测试用例中调用getCanvasOptions
函数后得到ignoreElements
方法。然后,像往常一样调用它并测试它。
例如
index.ts
:
export const getCanvasOptions = () => {
const { devicePixelRatio } = window;
const { scrollHeight, scrollWidth } = document.body;
const ratio = devicePixelRatio < 2 ? devicePixelRatio : devicePixelRatio / 2;
const width = scrollWidth * ratio;
const height = scrollHeight * ratio;
return {
allowTaint: true,
letterRendering: 1,
foreignObjectRendering: true,
quality: 1,
width: width,
height: height,
scale: ratio,
useCORS: true,
ignoreElements: (node) => {
return node.nodeName === 'NOSCRIPT';
},
};
};
index.test.ts
:
import { getCanvasOptions } from './';
describe('67778543', () => {
it('should pass', () => {
const actual = getCanvasOptions();
expect(actual).toEqual({
allowTaint: true,
letterRendering: 1,
foreignObjectRendering: true,
quality: 1,
width: 0,
height: 0,
scale: 1,
useCORS: true,
ignoreElements: expect.any(Function),
});
// test ignoreElements method
const rval = actual.ignoreElements({ nodeName: 'NOSCRIPT' });
expect(rval).toBeTruthy();
});
});
测试结果:
PASS examples/67778543/index.test.ts (8.274 s)
67778543
✓ should pass (3 ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 50 | 100 | 100 |
index.ts | 100 | 50 | 100 | 100 | 4
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 9.31 s, estimated 10 s
我想测试我的 util 函数,它包含通过 html2canvas、
生成 canvas 的选项const getCanvasOptions = () => {
const { devicePixelRatio } = window;
const { scrollHeight, scrollWidth } = document.body;
const ratio = devicePixelRatio < 2 ? devicePixelRatio : devicePixelRatio / 2;
const width = scrollWidth * ratio;
const height = scrollHeight * ratio;
return {
allowTaint: true,
letterRendering: 1,
foreignObjectRendering: true,
quality: 1,
width: width,
height: height,
scale: ratio,
useCORS: true,
ignoreElements: (node) => {
return node.nodeName === 'NOSCRIPT';
}
};
};
我通过模拟 document
和 window
对象来测试它,然后 strictEqual
检查 expected
和 actual
返回的对象。但是在我的报道中,它表明下面的行是未经测试的,
return node.nodeName === 'NOSCRIPT'
如何在 jest 中测试上面的行?
你可以在你的测试用例中调用getCanvasOptions
函数后得到ignoreElements
方法。然后,像往常一样调用它并测试它。
例如
index.ts
:
export const getCanvasOptions = () => {
const { devicePixelRatio } = window;
const { scrollHeight, scrollWidth } = document.body;
const ratio = devicePixelRatio < 2 ? devicePixelRatio : devicePixelRatio / 2;
const width = scrollWidth * ratio;
const height = scrollHeight * ratio;
return {
allowTaint: true,
letterRendering: 1,
foreignObjectRendering: true,
quality: 1,
width: width,
height: height,
scale: ratio,
useCORS: true,
ignoreElements: (node) => {
return node.nodeName === 'NOSCRIPT';
},
};
};
index.test.ts
:
import { getCanvasOptions } from './';
describe('67778543', () => {
it('should pass', () => {
const actual = getCanvasOptions();
expect(actual).toEqual({
allowTaint: true,
letterRendering: 1,
foreignObjectRendering: true,
quality: 1,
width: 0,
height: 0,
scale: 1,
useCORS: true,
ignoreElements: expect.any(Function),
});
// test ignoreElements method
const rval = actual.ignoreElements({ nodeName: 'NOSCRIPT' });
expect(rval).toBeTruthy();
});
});
测试结果:
PASS examples/67778543/index.test.ts (8.274 s)
67778543
✓ should pass (3 ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 50 | 100 | 100 |
index.ts | 100 | 50 | 100 | 100 | 4
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 9.31 s, estimated 10 s