diff --git a/CHANGELOG.md b/CHANGELOG.md index bc7613793067..56c2049ed880 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[jest-environment-jsdom]` Allow specifying `customExportConditions` instead of the default `'browser'` ([#12774](https://github.com/facebook/jest/pull/12774)) + ### Fixes ### Chore & Maintenance diff --git a/e2e/resolve-conditions/.gitignore b/e2e/resolve-conditions/.gitignore new file mode 100644 index 000000000000..cf4bab9ddde9 --- /dev/null +++ b/e2e/resolve-conditions/.gitignore @@ -0,0 +1 @@ +!node_modules diff --git a/e2e/resolve-conditions/__tests__/jsdom-custom-export-conditions.test.mjs b/e2e/resolve-conditions/__tests__/jsdom-custom-export-conditions.test.mjs new file mode 100644 index 000000000000..e417009db3e6 --- /dev/null +++ b/e2e/resolve-conditions/__tests__/jsdom-custom-export-conditions.test.mjs @@ -0,0 +1,15 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @jest-environment jest-environment-jsdom + * @jest-environment-options {"customExportConditions": ["special"]} + */ + +import {fn} from 'fake-dual-dep'; + +test('returns correct message', () => { + expect(fn()).toEqual('hello from special'); +}); 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 f7274342df9a..f868e6f55426 100644 --- a/e2e/resolve-conditions/node_modules/fake-dual-dep/package.json +++ b/e2e/resolve-conditions/node_modules/fake-dual-dep/package.json @@ -5,7 +5,8 @@ ".": { "deno": "./deno.mjs", "node": "./node.mjs", - "browser": "./browser.mjs" + "browser": "./browser.mjs", + "special": "./special.mjs" } } } diff --git a/e2e/resolve-conditions/node_modules/fake-dual-dep/special.mjs b/e2e/resolve-conditions/node_modules/fake-dual-dep/special.mjs new file mode 100644 index 000000000000..d24f08be168c --- /dev/null +++ b/e2e/resolve-conditions/node_modules/fake-dual-dep/special.mjs @@ -0,0 +1,10 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +export function fn() { + return 'hello from special'; +} diff --git a/packages/jest-environment-jsdom/src/index.ts b/packages/jest-environment-jsdom/src/index.ts index f16e876ecefe..8b513796c91c 100644 --- a/packages/jest-environment-jsdom/src/index.ts +++ b/packages/jest-environment-jsdom/src/index.ts @@ -33,6 +33,7 @@ export default class JSDOMEnvironment implements JestEnvironment { global: Win; private errorEventListener: ((event: Event & {error: Error}) => void) | null; moduleMocker: ModuleMocker | null; + customExportConditions: Array; constructor(config: JestEnvironmentConfig, context: EnvironmentContext) { const {projectConfig} = config; @@ -109,6 +110,20 @@ export default class JSDOMEnvironment implements JestEnvironment { return originalRemoveListener.apply(this, args); }; + if ('customExportConditions' in projectConfig.testEnvironmentOptions) { + const {customExportConditions} = projectConfig.testEnvironmentOptions; + if ( + Array.isArray(customExportConditions) && + customExportConditions.every(item => typeof item === 'string') + ) { + this.customExportConditions = customExportConditions; + } else { + throw new Error( + 'Custom export conditions specified but they are not an array of strings', + ); + } + } + this.moduleMocker = new ModuleMocker(global as any); this.fakeTimers = new LegacyFakeTimers({ @@ -158,7 +173,7 @@ export default class JSDOMEnvironment implements JestEnvironment { } exportConditions(): Array { - return ['browser']; + return this.customExportConditions ?? ['browser']; } getVmContext(): Context | null {