我已经实现了使用Jest和酶测试画布文件,但是每当我运行测试时,它都在说 TypeError:无法读取null的属性“ getContext”
这是我的测试代码:
import React, { Component } from "react"
import { shallow } from "enzyme"
import { ReferenceImageCanvas } from "./../index"
describe("<ReferenceImageCanvas />", () => {
const props = {
dispatch: jest.fn(),
canvasID: "canvas-1",
reset: true,
scale: 1,
imgSrc: ""
}
it("renders when loading true", () => {
const wrapper = shallow(<ReferenceImageCanvas {...props} />)
console.log(wrapper.debug())
})
})
运行此npm测试时,出现以下错误:
<ReferenceImageCanvas /> › renders when loading true
TypeError: Cannot read property 'getContext' of null
32 | const imageSrc = this.props.imgSrc
33 | const canvas = document.getElementById(this.state.canvasId)
> 34 | const ctx = canvas.getContext("2d")
| ^
35 | const baseImage = new Image()
36 | baseImage.src = imageSrc
37 | this.setState({
at ReferenceImageCanvas.componentDidMount (src/canvas/referenceImageCanvas/index.js:34:24)
at Object.it (src/canvas/referenceImageCanvas/__test__/index.test.js:16:21)
尽管我的react程序运行正常。
这是测试错误还是我做错了无法理解的错误