Skip to content

Commit

Permalink
feat(jest-environment-jsdom): allow specifying `customExportCondition…
Browse files Browse the repository at this point in the history
…s` instead of the default `'browser'`
  • Loading branch information
jeysal committed Apr 29, 2022
1 parent e0bc54d commit 6ba206f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions e2e/resolve-conditions/.gitignore
@@ -0,0 +1 @@
!node_modules
@@ -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');
});

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/special.mjs

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

17 changes: 16 additions & 1 deletion packages/jest-environment-jsdom/src/index.ts
Expand Up @@ -33,6 +33,7 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
global: Win;
private errorEventListener: ((event: Event & {error: Error}) => void) | null;
moduleMocker: ModuleMocker | null;
customExportConditions: Array<string>;

constructor(config: JestEnvironmentConfig, context: EnvironmentContext) {
const {projectConfig} = config;
Expand Down Expand Up @@ -109,6 +110,20 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
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({
Expand Down Expand Up @@ -158,7 +173,7 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
}

exportConditions(): Array<string> {
return ['browser'];
return this.customExportConditions ?? ['browser'];
}

getVmContext(): Context | null {
Expand Down

0 comments on commit 6ba206f

Please sign in to comment.