mrdoob / three.js

JavaScript 3D Library.
https://threejs.org/
MIT License
102.73k stars 35.38k forks source link

How to mock webglrenderer using jest unit testing framework #17752

Closed Swathikasoju closed 5 years ago

Swathikasoju commented 5 years ago
jest.mock('three', () => {
  const THREE = require.requireActual('three')
  return {
    ...THREE,
    WebGLRenderer: class WebGlRenderer {
      WebGlRenderer () {
        this.renderer = new THREE.WebGLRenderer({ antialias: true, logarithmicDepthBuffer: true, shadowMap: { enabled: true } })
        return this.renderer
      }
      setSize () { return jest.fn() }
      setPixelRatio () { return jest.fn() }
      setClearColor () { return jest.fn() }
    }

  }
})

does not create the webgl context or the htmlCanvas element. Everytime I try to append renderer,domElement I get error Error in mounted hook: "TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'."

Swathikasoju commented 5 years ago

Here is the stackoverflow url for the help: https://stackoverflow.com/questions/58431117/mocking-webglrenderer-and-other-three-js-libraries-to-test-using-jest?noredirect=1#comment103205546_58431117

could you please help