|
| 1 | +/* |
| 2 | + * Copyright Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Same as jest-environment-jsdom, but does not forward jsdom CSS parse errors to |
| 21 | + * Jest's console. Those come from modern @backstage/ui CSS (@layer, etc.) that jsdom's |
| 22 | + * parser cannot handle; they are harmless for unit tests. |
| 23 | + * |
| 24 | + * Derived from jest-environment-jsdom@29.7.0 (MIT). |
| 25 | + */ |
| 26 | +function isString(value) { |
| 27 | + return typeof value === 'string'; |
| 28 | +} |
| 29 | + |
| 30 | +function _jsdom() { |
| 31 | + const data = require('jsdom'); |
| 32 | + _jsdom = function () { |
| 33 | + return data; |
| 34 | + }; |
| 35 | + return data; |
| 36 | +} |
| 37 | +function _fakeTimers() { |
| 38 | + const data = require('@jest/fake-timers'); |
| 39 | + _fakeTimers = function () { |
| 40 | + return data; |
| 41 | + }; |
| 42 | + return data; |
| 43 | +} |
| 44 | +function _jestMock() { |
| 45 | + const data = require('jest-mock'); |
| 46 | + _jestMock = function () { |
| 47 | + return data; |
| 48 | + }; |
| 49 | + return data; |
| 50 | +} |
| 51 | +function _jestUtil() { |
| 52 | + const data = require('jest-util'); |
| 53 | + _jestUtil = function () { |
| 54 | + return data; |
| 55 | + }; |
| 56 | + return data; |
| 57 | +} |
| 58 | + |
| 59 | +class JSDOMEnvironmentSuppressCss { |
| 60 | + dom; |
| 61 | + fakeTimers; |
| 62 | + fakeTimersModern; |
| 63 | + global; |
| 64 | + errorEventListener; |
| 65 | + moduleMocker; |
| 66 | + customExportConditions = ['browser']; |
| 67 | + _configuredExportConditions; |
| 68 | + constructor(config, context) { |
| 69 | + const { projectConfig } = config; |
| 70 | + const virtualConsole = new (_jsdom().VirtualConsole)(); |
| 71 | + // jsdom 21+ uses forwardTo; older stacks (jest-environment-jsdom 29 + jsdom 20) use sendTo. |
| 72 | + if (typeof virtualConsole.forwardTo === 'function') { |
| 73 | + virtualConsole.forwardTo(context.console, { jsdomErrors: 'none' }); |
| 74 | + } else if (typeof virtualConsole.sendTo === 'function') { |
| 75 | + virtualConsole.sendTo(context.console, { omitJSDOMErrors: true }); |
| 76 | + } else { |
| 77 | + throw new TypeError( |
| 78 | + 'Unsupported jsdom VirtualConsole: expected forwardTo or sendTo', |
| 79 | + ); |
| 80 | + } |
| 81 | + virtualConsole.on('jsdomError', error => { |
| 82 | + if ( |
| 83 | + error && |
| 84 | + typeof error.message === 'string' && |
| 85 | + error.message.includes('Could not parse CSS stylesheet') |
| 86 | + ) { |
| 87 | + return; |
| 88 | + } |
| 89 | + context.console.error(error); |
| 90 | + }); |
| 91 | + this.dom = new (_jsdom().JSDOM)( |
| 92 | + typeof projectConfig.testEnvironmentOptions.html === 'string' |
| 93 | + ? projectConfig.testEnvironmentOptions.html |
| 94 | + : '<!DOCTYPE html>', |
| 95 | + { |
| 96 | + pretendToBeVisual: true, |
| 97 | + resources: |
| 98 | + typeof projectConfig.testEnvironmentOptions.userAgent === 'string' |
| 99 | + ? new (_jsdom().ResourceLoader)({ |
| 100 | + userAgent: projectConfig.testEnvironmentOptions.userAgent, |
| 101 | + }) |
| 102 | + : undefined, |
| 103 | + runScripts: 'dangerously', |
| 104 | + url: 'http://localhost/', |
| 105 | + virtualConsole, |
| 106 | + ...projectConfig.testEnvironmentOptions, |
| 107 | + }, |
| 108 | + ); |
| 109 | + const global = (this.global = this.dom.window); |
| 110 | + if (global == null) { |
| 111 | + throw new TypeError('JSDOM did not return a Window object'); |
| 112 | + } |
| 113 | + |
| 114 | + global.global = global; |
| 115 | + |
| 116 | + this.global.Error.stackTraceLimit = 100; |
| 117 | + (0, _jestUtil().installCommonGlobals)(global, projectConfig.globals); |
| 118 | + |
| 119 | + global.Buffer = Buffer; |
| 120 | + |
| 121 | + this.errorEventListener = event => { |
| 122 | + if (userErrorListenerCount === 0 && event.error != null) { |
| 123 | + process.emit('uncaughtException', event.error); |
| 124 | + } |
| 125 | + }; |
| 126 | + global.addEventListener('error', this.errorEventListener); |
| 127 | + |
| 128 | + const originalAddListener = global.addEventListener.bind(global); |
| 129 | + const originalRemoveListener = global.removeEventListener.bind(global); |
| 130 | + let userErrorListenerCount = 0; |
| 131 | + global.addEventListener = function (...args) { |
| 132 | + if (args[0] === 'error') { |
| 133 | + userErrorListenerCount++; |
| 134 | + } |
| 135 | + return originalAddListener.apply(this, args); |
| 136 | + }; |
| 137 | + global.removeEventListener = function (...args) { |
| 138 | + if (args[0] === 'error') { |
| 139 | + userErrorListenerCount--; |
| 140 | + } |
| 141 | + return originalRemoveListener.apply(this, args); |
| 142 | + }; |
| 143 | + if ('customExportConditions' in projectConfig.testEnvironmentOptions) { |
| 144 | + const { customExportConditions } = projectConfig.testEnvironmentOptions; |
| 145 | + if ( |
| 146 | + Array.isArray(customExportConditions) && |
| 147 | + customExportConditions.every(isString) |
| 148 | + ) { |
| 149 | + this._configuredExportConditions = customExportConditions; |
| 150 | + } else { |
| 151 | + throw new TypeError( |
| 152 | + 'Custom export conditions specified but they are not an array of strings', |
| 153 | + ); |
| 154 | + } |
| 155 | + } |
| 156 | + this.moduleMocker = new (_jestMock().ModuleMocker)(global); |
| 157 | + this.fakeTimers = new (_fakeTimers().LegacyFakeTimers)({ |
| 158 | + config: projectConfig, |
| 159 | + global: global, |
| 160 | + moduleMocker: this.moduleMocker, |
| 161 | + timerConfig: { |
| 162 | + idToRef: id => id, |
| 163 | + refToId: ref => ref, |
| 164 | + }, |
| 165 | + }); |
| 166 | + this.fakeTimersModern = new (_fakeTimers().ModernFakeTimers)({ |
| 167 | + config: projectConfig, |
| 168 | + global: global, |
| 169 | + }); |
| 170 | + } |
| 171 | + |
| 172 | + setup() { |
| 173 | + return Promise.resolve(); |
| 174 | + } |
| 175 | + async teardown() { |
| 176 | + if (this.fakeTimers) { |
| 177 | + this.fakeTimers.dispose(); |
| 178 | + } |
| 179 | + if (this.fakeTimersModern) { |
| 180 | + this.fakeTimersModern.dispose(); |
| 181 | + } |
| 182 | + if (this.global != null) { |
| 183 | + if (this.errorEventListener) { |
| 184 | + this.global.removeEventListener('error', this.errorEventListener); |
| 185 | + } |
| 186 | + this.global.close(); |
| 187 | + } |
| 188 | + this.errorEventListener = null; |
| 189 | + this.global = null; |
| 190 | + this.dom = null; |
| 191 | + this.fakeTimers = null; |
| 192 | + this.fakeTimersModern = null; |
| 193 | + } |
| 194 | + exportConditions() { |
| 195 | + return this._configuredExportConditions ?? this.customExportConditions; |
| 196 | + } |
| 197 | + getVmContext() { |
| 198 | + if (this.dom) { |
| 199 | + return this.dom.getInternalVMContext(); |
| 200 | + } |
| 201 | + return null; |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +module.exports = JSDOMEnvironmentSuppressCss; |
| 206 | +module.exports.default = JSDOMEnvironmentSuppressCss; |
| 207 | +module.exports.TestEnvironment = JSDOMEnvironmentSuppressCss; |
0 commit comments