Skip to content

Always overriding global value breaks some tests #36

@jlowcs

Description

@jlowcs

I have a suggestion to improve the current implementation and to avoid issues like #32 and other similar issues (e.g. I have one related to Blob).

const JSDOMEnvironment = require('jest-environment-jsdom').default

class FixedJSDOMEnvironment extends JSDOMEnvironment {
  constructor(...args) {
    super(...args)

    /**
     * @note Opt-out from JSDOM using browser-style resolution
     * for dependencies. This is simply incorrect, as JSDOM is
     * not a browser, and loading browser-oriented bundles in
     * Node.js will break things.
     *
     * Consider migrating to a more modern test runner if you
     * don't want to deal with this.
     */
    this.customExportConditions = args.customExportConditions || ['']

    setIfNonDefined.call(this, 'TextDecoder', TextDecoder)
    setIfNonDefined.call(this, 'TextEncoder', TextEncoder)
    setIfNonDefined.call(this, 'TextDecoderStream', TextDecoderStream)
    setIfNonDefined.call(this, 'TextEncoderStream', TextEncoderStream)
    setIfNonDefined.call(this, 'ReadableStream', ReadableStream)

    setIfNonDefined.call(this, 'Blob', Blob)
    setIfNonDefined.call(this, 'Headers', Headers)
    setIfNonDefined.call(this, 'FormData', FormData)
    setIfNonDefined.call(this, 'Request', Request)
    setIfNonDefined.call(this, 'Response', Response)
    setIfNonDefined.call(this, 'fetch', fetch)
    setIfNonDefined.call(this, 'structuredClone', structuredClone)
    setIfNonDefined.call(this, 'URL', URL)
    setIfNonDefined.call(this, 'URLSearchParams', URLSearchParams)

    setIfNonDefined.call(this, 'BroadcastChannel', BroadcastChannel)
    setIfNonDefined.call(this, 'TransformStream', TransformStream)
  }
}

function setIfNonDefined(key, value) {
	if (!this.global[key]) {
		this.global[key] = value
	}
}

module.exports = FixedJSDOMEnvironment

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