Skip to content

Commit 8c35c4d

Browse files
authored
feat: support conditions from test environments (#11863)
1 parent ea8b199 commit 8c35c4d

File tree

14 files changed

+135
-48
lines changed

14 files changed

+135
-48
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Features
44

55
- `[jest-resolver, jest-runtime]` Pass `conditions` to custom resolvers to enable them to implement support for package.json `exports` field ([#11859](https://github.com/facebook/jest/pull/11859))
6+
- `[jest-runtime]` Allow custom envs to specify `exportConditions` which is passed together with Jest's own conditions to custom resolvers ([#11863](https://github.com/facebook/jest/pull/11863))
67

78
### Fixes
89

docs/Configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ module.exports = (request, options) => {
851851
};
852852
```
853853

854-
While Jest does not support [package `exports`](https://nodejs.org/api/packages.html#packages_package_entry_points) (beyond `main`), Jest will provide `conditions` as an option when calling custom resolvers, which can be used to implement support for `exports` in userland. Jest will pass `['import', 'default']` when running a test in ESM mode, and `['require', 'default']` when running with CJS.
854+
While Jest does not support [package `exports`](https://nodejs.org/api/packages.html#packages_package_entry_points) (beyond `main`), Jest will provide `conditions` as an option when calling custom resolvers, which can be used to implement support for `exports` in userland. Jest will pass `['import', 'default']` when running a test in ESM mode, and `['require', 'default']` when running with CJS. Additionally, custom test environments can specify an `exportConditions` method which returns an array of conditions that will be passed along with Jest's defaults.
855855

856856
### `restoreMocks` \[boolean]
857857

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
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>/browser-env.js
8+
*/
9+
10+
import {fn} from '../fake-dual-dep';
11+
12+
test('returns correct message', () => {
13+
expect(fn()).toEqual('hello from browser');
14+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
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>/node-env.js
8+
*/
9+
10+
import {fn} from '../fake-dual-dep';
11+
12+
test('returns correct message', () => {
13+
expect(fn()).toEqual('hello from node');
14+
});

e2e/resolve-conditions/browser-env.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
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 BrowserEnv = require('jest-environment-jsdom');
11+
12+
module.exports = class BrowserEnvWithConditions extends BrowserEnv {
13+
exportConditions() {
14+
return ['browser'];
15+
}
16+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
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+
export function fn() {
9+
return 'hello from browser';
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
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+
export function fn() {
9+
return 'hello from node';
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "fake-dual-dep",
3+
"version": "1.0.0",
4+
"exports": {
5+
".": {
6+
"node": "./node.mjs",
7+
"browser": "./browser.mjs"
8+
}
9+
}
10+
}

e2e/resolve-conditions/node-env.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
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');
11+
12+
module.exports = class NodeEnvWithConditions extends NodeEnv {
13+
exportConditions() {
14+
return ['node'];
15+
}
16+
};

e2e/resolve-conditions/resolver.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ function createPathFilter(conditions) {
2525

2626
return (
2727
resolveExports(pkg, path, {
28+
// `resolve.exports adds `node` unless `browser` is `false`, so let's add this ugly thing
29+
browser: conditions.includes('browser'),
2830
conditions,
2931
// `resolve.exports adds `import` unless `require` is `false`, so let's add this ugly thing
30-
require: !conditions.includes('import'),
32+
require: conditions.includes('require'),
3133
}) || relativePath
3234
);
3335
};

0 commit comments

Comments
 (0)