question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReferenceError: MutationObserver is not defined

See original GitHub issue

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)
  })
})

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

38reactions
damonbauercommented, Oct 22, 2019

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';
4reactions
Matt-Yorkleycommented, Jan 24, 2022

In case anyone lands here looking for test setup tips with StimulusJS and Jest; you don’t need that MutationObserver shim anymore, newer versions of JSDOM support it by default.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing MutationObserver with Jest - Stack Overflow
You can use Jest mock functions to mock MutationObserver . This allows you to test instances of it in your code. See my...
Read more >
ReferenceError: MutationObserver is not defined - LWC Jest test
I am facing this error while executing lwc jest tests. My source api version is 52 and package.json devdependencies looks like this:
Read more >
MutationObserver or regeneratorRuntime error solve in testing ...
ReferenceError : regeneratorRuntime is not defined. regeneratorRuntime error screenshot. TypeError: MutationObserver is not a constructor.
Read more >
jest mutationobserver polyfill
referenceerror: mutationobserver is not defined jest. You may be in some scope where MutationObserver is not defined. It is not some inherent feature...
Read more >
MutationObserver not working - Mozilla Discourse
You may be in some scope where MutationObserver is not defined. It is not some inherent feature of javascript, but a property of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found