Skip to content

JSDom does not mock window.MutationObserver. #6617

Closed
@deskoh

Description

@deskoh

Is this a bug report?

Yes.

Did you try recovering your dependencies?

Yes.

Which terms did you search for in User Guide?

jsdom
testing

Environment

Environment Info:

  System:
    OS: Windows 10
    CPU: x64 Intel(R) Xeon(R) CPU E3-1270 v6 @ 3.80GHz
  Binaries:
    npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: 44.17763.1.0
    Internet Explorer: 11.0.17763.1
  npmPackages:
    react: ^16.8.4 => 16.8.4
    react-dom: ^16.8.4 => 16.8.4
    react-scripts: 2.1.8 => 2.1.8
  npmGlobalPackages:
    create-react-app: Not Found

Steps to Reproduce

  1. create-react-app jsdom-test
  2. Add expect(window.MutationObserver).not.toBeUndefined(); to generated App.test.js.
  3. Run npm run test.

Expected Behavior

npm run test should pass as window.MutationObserver should be defined by jsdom as tested using the following code:

// Mock DOM (https://airbnb.io/enzyme/docs/guides/jsdom.html)
const { JSDOM } = require('jsdom');
const jsdom = new JSDOM('<!doctype html><html><body></body></html>');
const { window } = jsdom;

const assert = require('assert');

assert(window.MutationObserver);

Actual Behavior

Test fails as window.MutationObserver is undefined.

  ● test MutationObserver

    expect(received).not.toBeUndefined()

    Received: undefined

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions