【问题标题】:How to set up jest for testing canvas如何设置 jest 以测试画布
【发布时间】:2019-07-05 18:14:05
【问题描述】:

无法使用画布运行开玩笑测试。我有适用于画布、上下文、dom 等的函数。如何正确设置 jest?

我尝试过使用jest-canvas-mock,但是如何正确安装呢?

package.json

"devDependencies": {
    ***
    "jest": "^24.8.0",
    "jsdom": "^15.1.1",
    ***
  },
  "dependencies": {
    ***
  },
  "jest": {
    "setupFiles": [
      "jest-canvas-mock"
    ]
  }
}

jest.config.js

module.exports = {
  roots: [
    '<rootDir>/src',
  ],
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
  moduleFileExtensions: [
    'js',
  ],
};

console.log:

 TypeError: Cannot set property 'imageSmoothingEnabled' of null

      15 |       const canvas = document.createElement('canvas');
      16 |       const context = canvas.getContext('2d');
    > 17 |       context.imageSmoothingEnabled = false;
         |       ^

【问题讨论】:

    标签: javascript testing dom canvas jestjs


    【解决方案1】:

    尝试将jest-canvas-mock库安装到package.json中。

    npm install --dev jest-canvas-mock
    

    然后将其导入到您的测试文件中。

    import 'jest-canvas-mock'
    

    【讨论】:

    猜你喜欢
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-24
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 2021-12-19
    相关资源
    最近更新 更多