add mutation observer polyfill for jest test env (#20996) (#21409)

* add mutation observer polyfill for jest test env

* use polyfill from npm

* set bluebird scheduler

* use correct version in comment about bluebird
This commit is contained in:
Nathan Reese 2018-07-30 10:05:03 -06:00 committed by GitHub
parent 45d754e4b0
commit a4e0fa1bd4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 54 additions and 13 deletions

View file

@ -324,6 +324,7 @@
"mocha": "3.3.0",
"mock-fs": "^4.4.2",
"murmurhash3js": "3.0.1",
"mutation-observer": "^1.0.3",
"ncp": "2.0.0",
"nock": "8.0.0",
"node-sass": "^4.9.0",

View file

@ -96,18 +96,6 @@ test('renders ControlsTab', () => {
});
describe('behavior', () => {
// Mock MutationObserver used in EuiAccordion
beforeAll(() => {
global.MutationObserver = class {
constructor() {}
disconnect() {}
observe() {}
};
});
afterAll(() => {
delete global.MutationObserver;
});
test('add control button', () => {
const component = mount(<ControlsTab

View file

@ -48,6 +48,7 @@ export default {
},
setupFiles: [
'<rootDir>/src/dev/jest/setup/babel_polyfill.js',
'<rootDir>/src/dev/jest/setup/polyfills.js',
'<rootDir>/src/dev/jest/setup/enzyme.js',
'<rootDir>/src/dev/jest/setup/throw_on_console_error.js',
],

View file

@ -0,0 +1,26 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
// bluebird < v3.3.5 does not work with MutationObserver polyfill
// when MutationObserver exists, bluebird avoids using node's builtin async schedulers
const bluebird = require('bluebird');
bluebird.Promise.setScheduler(function (fn) { global.setImmediate.call(global, fn); });
const MutationObserver = require('mutation-observer');
Object.defineProperty(window, 'MutationObserver', { value: MutationObserver });

View file

@ -28,7 +28,8 @@ export function createJestConfig({
},
setupFiles: [
`${kibanaDirectory}/src/dev/jest/setup/babel_polyfill.js`,
`<rootDir>/dev-tools/jest/setup/enzyme.js`
`<rootDir>/dev-tools/jest/setup/polyfills.js`,
`<rootDir>/dev-tools/jest/setup/enzyme.js`,
],
testMatch: [
"**/*.test.{js,ts,tsx}"

View file

@ -0,0 +1,15 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
// bluebird < v3.3.5 does not work with MutationObserver polyfill
// when MutationObserver exists, bluebird avoids using node's builtin async schedulers
// x-pack has a different version of bluebird so it needs its own polyfills.js to ensure
// the scheduler is set on the right bluebird instance
const bluebird = require('bluebird');
bluebird.Promise.setScheduler(function (fn) { global.setImmediate.call(global, fn); });
const MutationObserver = require('mutation-observer');
Object.defineProperty(window, 'MutationObserver', { value: MutationObserver });

View file

@ -57,6 +57,7 @@
"jest-styled-components": "^5.0.1",
"mocha": "3.3.0",
"mustache": "^2.3.0",
"mutation-observer": "^1.0.3",
"node-fetch": "^2.1.2",
"pdf-image": "1.1.0",
"pixelmatch": "4.0.2",

View file

@ -5455,6 +5455,10 @@ mustache@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/mustache/-/mustache-2.3.0.tgz#4028f7778b17708a489930a6e52ac3bca0da41d0"
mutation-observer@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/mutation-observer/-/mutation-observer-1.0.3.tgz#42e9222b101bca82e5ba9d5a7acf4a14c0f263d0"
mute-stream@0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.6.tgz#48962b19e169fd1dfc240b3f1e7317627bbc47db"

View file

@ -9216,6 +9216,10 @@ mustache@2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/mustache/-/mustache-2.3.0.tgz#4028f7778b17708a489930a6e52ac3bca0da41d0"
mutation-observer@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/mutation-observer/-/mutation-observer-1.0.3.tgz#42e9222b101bca82e5ba9d5a7acf4a14c0f263d0"
mute-stream@0.0.5:
version "0.0.5"
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.5.tgz#8fbfabb0a98a253d3184331f9e8deb7372fac6c0"