I'm creating a test for a controller. But got this error when I do Application.start(). I'm using jest for testing.
Here's the code
import {Application} from 'stimulus'
import SomeController from '../controllers/some_controller'
describe('SomeController', () => {
beforeEach(() => {
const stimulusApp = Application.start()
stimulusApp.register('some', SomeController)
})
})
I would suggest to have a look here: https://github.com/stimulusjs/stimulus/tree/master/packages/%40stimulus/mutation-observers
You'll need to run your tests in a browser or a simulated environment with MutationObserver support. I believe jest uses jsdom by default, which doesn't appear to support it: https://github.com/jsdom/jsdom/issues/639.
thanks @javan make sense.
For anyone who comes across this:
Add mutationobserver-shim to your project:
yarn add mutationobserver-shim --dev
Include it in Jest setup. In your jest.config.js, update setupFilesAfterEnv array to include a setup file, like so:
// jest.config.js
setupFilesAfterEnv: [
'<rootDir>/setup-jest.js'
],
Then, in the setup file, import the shim:
// setup-jest.js
import 'mutationobserver-shim';
Most helpful comment
For anyone who comes across this:
Add
mutationobserver-shimto your project:Include it in Jest setup. In your
jest.config.js, updatesetupFilesAfterEnvarray to include a setup file, like so:Then, in the setup file, import the shim: