ReferenceError: MutationObserver is not defined
See original GitHub issueI’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:
- Created 5 years ago
- Reactions:3
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For anyone who comes across this:
Add
mutationobserver-shim
to your project:Include it in Jest setup. In your
jest.config.js
, updatesetupFilesAfterEnv
array to include a setup file, like so:Then, in the setup file, import the shim:
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.