diff --git a/CHANGELOG.md b/CHANGELOG.md index a27d408edf8e..eaa379867814 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[jest-environment-jsdom, jest-environment-node]` Fix assignment of `customExportConditions` via `testEnvironmentOptions` when custom env subclass defines a default value ([#13989](https://github.com/facebook/jest/pull/13989)) + ### Chore & Maintenance ### Performance diff --git a/e2e/resolve-conditions/__tests__/custom-env-conditions-method-override.test.mjs b/e2e/resolve-conditions/__tests__/custom-env-conditions-method-override.test.mjs new file mode 100644 index 000000000000..b8318bb79192 --- /dev/null +++ b/e2e/resolve-conditions/__tests__/custom-env-conditions-method-override.test.mjs @@ -0,0 +1,14 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @jest-environment /custom-env-conditions-method-override.js + */ + +import {fn} from 'fake-dual-dep'; + +test('returns correct message', () => { + expect(fn()).toBe('hello from deno'); +}); diff --git a/e2e/resolve-conditions/__tests__/custom-env-override-export-conditions.test.mjs b/e2e/resolve-conditions/__tests__/custom-env-override-export-conditions.test.mjs new file mode 100644 index 000000000000..fe084ea54525 --- /dev/null +++ b/e2e/resolve-conditions/__tests__/custom-env-override-export-conditions.test.mjs @@ -0,0 +1,15 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @jest-environment /custom-env.js + * @jest-environment-options {"customExportConditions": ["react-native"]} + */ + +import {fn} from 'fake-dual-dep'; + +test('returns correct message', () => { + expect(fn()).toBe('hello from react-native'); +}); diff --git a/e2e/resolve-conditions/__tests__/deno.test.mjs b/e2e/resolve-conditions/__tests__/custom-env.test.mjs similarity index 87% rename from e2e/resolve-conditions/__tests__/deno.test.mjs rename to e2e/resolve-conditions/__tests__/custom-env.test.mjs index 75fac0e3ea33..9f48b4071007 100644 --- a/e2e/resolve-conditions/__tests__/deno.test.mjs +++ b/e2e/resolve-conditions/__tests__/custom-env.test.mjs @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @jest-environment /deno-env.js + * @jest-environment /custom-env.js */ import {fn} from 'fake-dual-dep'; diff --git a/e2e/resolve-conditions/deno-env.js b/e2e/resolve-conditions/custom-env-conditions-method-override.js similarity index 83% rename from e2e/resolve-conditions/deno-env.js rename to e2e/resolve-conditions/custom-env-conditions-method-override.js index 137dbf487eb6..69fe9f135dd1 100644 --- a/e2e/resolve-conditions/deno-env.js +++ b/e2e/resolve-conditions/custom-env-conditions-method-override.js @@ -9,7 +9,7 @@ const NodeEnv = require('jest-environment-node').TestEnvironment; -module.exports = class DenoEnvWithConditions extends NodeEnv { +module.exports = class CustomEnvWithConditions extends NodeEnv { exportConditions() { return ['deno']; } diff --git a/e2e/resolve-conditions/custom-env.js b/e2e/resolve-conditions/custom-env.js new file mode 100644 index 000000000000..74f83a770ea0 --- /dev/null +++ b/e2e/resolve-conditions/custom-env.js @@ -0,0 +1,14 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +'use strict'; + +const NodeEnv = require('jest-environment-node').TestEnvironment; + +module.exports = class CustomEnvWithConditions extends NodeEnv { + customExportConditions = ['deno']; +}; diff --git a/e2e/resolve-conditions/node_modules/fake-dual-dep/package.json b/e2e/resolve-conditions/node_modules/fake-dual-dep/package.json index f868e6f55426..d574ed50d85b 100644 --- a/e2e/resolve-conditions/node_modules/fake-dual-dep/package.json +++ b/e2e/resolve-conditions/node_modules/fake-dual-dep/package.json @@ -6,6 +6,7 @@ "deno": "./deno.mjs", "node": "./node.mjs", "browser": "./browser.mjs", + "react-native": "./react-native.js", "special": "./special.mjs" } } diff --git a/e2e/resolve-conditions/node_modules/fake-dual-dep/react-native.js b/e2e/resolve-conditions/node_modules/fake-dual-dep/react-native.js new file mode 100644 index 000000000000..7f9d697e613e --- /dev/null +++ b/e2e/resolve-conditions/node_modules/fake-dual-dep/react-native.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +module.exports.fn = function fn() { + return 'hello from react-native'; +} diff --git a/packages/jest-environment-jsdom/src/index.ts b/packages/jest-environment-jsdom/src/index.ts index dec78d95edf4..2dbc4caaad5d 100644 --- a/packages/jest-environment-jsdom/src/index.ts +++ b/packages/jest-environment-jsdom/src/index.ts @@ -38,6 +38,7 @@ export default class JSDOMEnvironment implements JestEnvironment { private errorEventListener: ((event: Event & {error: Error}) => void) | null; moduleMocker: ModuleMocker | null; customExportConditions = ['browser']; + private _configuredExportConditions?: Array; constructor(config: JestEnvironmentConfig, context: EnvironmentContext) { const {projectConfig} = config; @@ -119,7 +120,7 @@ export default class JSDOMEnvironment implements JestEnvironment { Array.isArray(customExportConditions) && customExportConditions.every(isString) ) { - this.customExportConditions = customExportConditions; + this._configuredExportConditions = customExportConditions; } else { throw new Error( 'Custom export conditions specified but they are not an array of strings', @@ -170,7 +171,7 @@ export default class JSDOMEnvironment implements JestEnvironment { } exportConditions(): Array { - return this.customExportConditions; + return this._configuredExportConditions ?? this.customExportConditions; } getVmContext(): Context | null { diff --git a/packages/jest-environment-node/src/index.ts b/packages/jest-environment-node/src/index.ts index 6d7da8c2f5d5..1b387aaffee5 100644 --- a/packages/jest-environment-node/src/index.ts +++ b/packages/jest-environment-node/src/index.ts @@ -64,6 +64,7 @@ export default class NodeEnvironment implements JestEnvironment { global: Global.Global; moduleMocker: ModuleMocker | null; customExportConditions = ['node', 'node-addons']; + private _configuredExportConditions?: Array; // while `context` is unused, it should always be passed constructor(config: JestEnvironmentConfig, _context: EnvironmentContext) { @@ -147,7 +148,7 @@ export default class NodeEnvironment implements JestEnvironment { Array.isArray(customExportConditions) && customExportConditions.every(isString) ) { - this.customExportConditions = customExportConditions; + this._configuredExportConditions = customExportConditions; } else { throw new Error( 'Custom export conditions specified but they are not an array of strings', @@ -201,7 +202,7 @@ export default class NodeEnvironment implements JestEnvironment { } exportConditions(): Array { - return this.customExportConditions; + return this._configuredExportConditions ?? this.customExportConditions; } getVmContext(): Context | null {