【问题标题】:How to test a function which is returned within an object in jest如何测试在开玩笑的对象中返回的函数
【发布时间】:2021-08-19 01:02:57
【问题描述】:

我想测试我的 util 函数,其中包含通过 html2canvas 生成画布的选项,

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';
    }
  };
};

我通过模拟documentwindow 对象,然后strictEqual 检查expectedactual 返回的对象来测试它。但在我的报道中,它表明以下行未经测试,

return node.nodeName === 'NOSCRIPT'

我怎样才能开玩笑地测试上面的行?

【问题讨论】:

  • 如果代码覆盖率是目标,那么调用ignoreElements 方法的测试用例可能会有所帮助

标签: javascript unit-testing canvas jestjs html2canvas


【解决方案1】:

在您的测试用例中调用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

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 2018-12-28
    • 2018-05-25
    • 1970-01-01
    • 2018-11-24
    • 2020-10-24
    • 2018-08-12
    相关资源
    最近更新 更多