Skip to content

Commit

Permalink
fix: Fix user override of customExportConditions in custom env subcla…
Browse files Browse the repository at this point in the history
…sses
  • Loading branch information
huntie committed Mar 9, 2023
1 parent 39f3bed commit 67be705
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
@@ -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 <rootDir>/custom-env-conditions-method-override.js
*/

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

test('returns correct message', () => {
expect(fn()).toBe('hello from deno');
});
@@ -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 <rootDir>/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');
});
Expand Up @@ -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 <rootDir>/deno-env.js
* @jest-environment <rootDir>/custom-env.js
*/

import {fn} from 'fake-dual-dep';
Expand Down
Expand Up @@ -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'];
}
Expand Down
14 changes: 14 additions & 0 deletions 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'];
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions e2e/resolve-conditions/node_modules/fake-dual-dep/react-native.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/jest-environment-jsdom/src/index.ts
Expand Up @@ -38,6 +38,7 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
private errorEventListener: ((event: Event & {error: Error}) => void) | null;
moduleMocker: ModuleMocker | null;
customExportConditions = ['browser'];
private _configuredExportConditions?: Array<string>;

constructor(config: JestEnvironmentConfig, context: EnvironmentContext) {
const {projectConfig} = config;
Expand Down Expand Up @@ -119,7 +120,7 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
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',
Expand Down Expand Up @@ -170,7 +171,7 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
}

exportConditions(): Array<string> {
return this.customExportConditions;
return this._configuredExportConditions ?? this.customExportConditions;
}

getVmContext(): Context | null {
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-environment-node/src/index.ts
Expand Up @@ -64,6 +64,7 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
global: Global.Global;
moduleMocker: ModuleMocker | null;
customExportConditions = ['node', 'node-addons'];
private _configuredExportConditions?: Array<string>;

// while `context` is unused, it should always be passed
constructor(config: JestEnvironmentConfig, _context: EnvironmentContext) {
Expand Down Expand Up @@ -147,7 +148,7 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
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',
Expand Down Expand Up @@ -201,7 +202,7 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
}

exportConditions(): Array<string> {
return this.customExportConditions;
return this._configuredExportConditions ?? this.customExportConditions;
}

getVmContext(): Context | null {
Expand Down

0 comments on commit 67be705

Please sign in to comment.