Skip to content

Commit 26bd03a

Browse files
authored
fix: Fix user override of customExportConditions in custom env subclasses (#13989)
1 parent 67aa05c commit 26bd03a

File tree

10 files changed

+64
-6
lines changed

10 files changed

+64
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Fixes
66

7+
- `[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))
8+
79
### Chore & Maintenance
810

911
### Performance
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @jest-environment <rootDir>/custom-env-conditions-method-override.js
8+
*/
9+
10+
import {fn} from 'fake-dual-dep';
11+
12+
test('returns correct message', () => {
13+
expect(fn()).toBe('hello from deno');
14+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @jest-environment <rootDir>/custom-env.js
8+
* @jest-environment-options {"customExportConditions": ["react-native"]}
9+
*/
10+
11+
import {fn} from 'fake-dual-dep';
12+
13+
test('returns correct message', () => {
14+
expect(fn()).toBe('hello from react-native');
15+
});

e2e/resolve-conditions/__tests__/deno.test.mjs renamed to e2e/resolve-conditions/__tests__/custom-env.test.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @jest-environment <rootDir>/deno-env.js
7+
* @jest-environment <rootDir>/custom-env.js
88
*/
99

1010
import {fn} from 'fake-dual-dep';

e2e/resolve-conditions/deno-env.js renamed to e2e/resolve-conditions/custom-env-conditions-method-override.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
const NodeEnv = require('jest-environment-node').TestEnvironment;
1111

12-
module.exports = class DenoEnvWithConditions extends NodeEnv {
12+
module.exports = class CustomEnvWithConditions extends NodeEnv {
1313
exportConditions() {
1414
return ['deno'];
1515
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
const NodeEnv = require('jest-environment-node').TestEnvironment;
11+
12+
module.exports = class CustomEnvWithConditions extends NodeEnv {
13+
customExportConditions = ['deno'];
14+
};

e2e/resolve-conditions/node_modules/fake-dual-dep/package.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/resolve-conditions/node_modules/fake-dual-dep/react-native.js

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/jest-environment-jsdom/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
3838
private errorEventListener: ((event: Event & {error: Error}) => void) | null;
3939
moduleMocker: ModuleMocker | null;
4040
customExportConditions = ['browser'];
41+
private _configuredExportConditions?: Array<string>;
4142

4243
constructor(config: JestEnvironmentConfig, context: EnvironmentContext) {
4344
const {projectConfig} = config;
@@ -119,7 +120,7 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
119120
Array.isArray(customExportConditions) &&
120121
customExportConditions.every(isString)
121122
) {
122-
this.customExportConditions = customExportConditions;
123+
this._configuredExportConditions = customExportConditions;
123124
} else {
124125
throw new Error(
125126
'Custom export conditions specified but they are not an array of strings',
@@ -170,7 +171,7 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
170171
}
171172

172173
exportConditions(): Array<string> {
173-
return this.customExportConditions;
174+
return this._configuredExportConditions ?? this.customExportConditions;
174175
}
175176

176177
getVmContext(): Context | null {

packages/jest-environment-node/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
6464
global: Global.Global;
6565
moduleMocker: ModuleMocker | null;
6666
customExportConditions = ['node', 'node-addons'];
67+
private _configuredExportConditions?: Array<string>;
6768

6869
// while `context` is unused, it should always be passed
6970
constructor(config: JestEnvironmentConfig, _context: EnvironmentContext) {
@@ -147,7 +148,7 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
147148
Array.isArray(customExportConditions) &&
148149
customExportConditions.every(isString)
149150
) {
150-
this.customExportConditions = customExportConditions;
151+
this._configuredExportConditions = customExportConditions;
151152
} else {
152153
throw new Error(
153154
'Custom export conditions specified but they are not an array of strings',
@@ -201,7 +202,7 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
201202
}
202203

203204
exportConditions(): Array<string> {
204-
return this.customExportConditions;
205+
return this._configuredExportConditions ?? this.customExportConditions;
205206
}
206207

207208
getVmContext(): Context | null {

0 commit comments

Comments
 (0)