Skip to content

Commit a0bf671

Browse files
authored
feat: pass conditions when resolving modules (#11859)
1 parent 03ff659 commit a0bf671

File tree

17 files changed

+262
-24
lines changed

17 files changed

+262
-24
lines changed

CHANGELOG.md

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

33
### Features
44

5+
- `[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+
57
### Fixes
68

79
- `[@jest/reporters]` Use async transform if available to transform files with no coverage ([#11852](https://github.com/facebook/jest/pull/11852))

docs/Configuration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,11 +788,13 @@ This option allows the use of a custom resolver. This resolver must be a node mo
788788
```json
789789
{
790790
"basedir": string,
791+
"conditions": [string],
791792
"defaultResolver": "function(request, options)",
792793
"extensions": [string],
793794
"moduleDirectory": [string],
794795
"paths": [string],
795796
"packageFilter": "function(pkg, pkgdir)",
797+
"pathFilter": "function(pkg, path, relativePath)",
796798
"rootDir": [string]
797799
}
798800
```
@@ -849,6 +851,8 @@ module.exports = (request, options) => {
849851
};
850852
```
851853

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.
855+
852856
### `restoreMocks` \[boolean]
853857

854858
Default: `false`

e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ FAIL __tests__/index.js
4141
12 | module.exports = () => 'test';
4242
13 |
4343
44-
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:566:17)
44+
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:577:17)
4545
at Object.require (index.js:10:1)
4646
`;
4747

@@ -70,6 +70,6 @@ FAIL __tests__/index.js
7070
12 | module.exports = () => 'test';
7171
13 |
7272
73-
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:566:17)
73+
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:577:17)
7474
at Object.require (index.js:10:1)
7575
`;

e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ FAIL __tests__/test.js
3737
| ^
3838
9 |
3939
40-
at Resolver.resolveModule (../../packages/jest-resolve/build/resolver.js:318:11)
40+
at Resolver.resolveModule (../../packages/jest-resolve/build/resolver.js:322:11)
4141
at Object.require (index.js:8:18)
4242
`;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
import {resolve} from 'path';
9+
import {onNodeVersions} from '@jest/test-utils';
10+
import {runYarnInstall} from '../Utils';
11+
import runJest from '../runJest';
12+
13+
const dir = resolve(__dirname, '..', 'resolve-conditions');
14+
15+
beforeAll(() => {
16+
runYarnInstall(dir);
17+
});
18+
19+
// The versions where vm.Module exists and commonjs with "exports" is not broken
20+
onNodeVersions('^12.16.0 || >=13.7.0', () => {
21+
test('resolves package exports correctly with custom resolver', () => {
22+
// run multiple times to ensure there are no caching errors
23+
for (let i = 0; i < 5; i++) {
24+
const {exitCode} = runJest(dir, [], {
25+
nodeOptions: '--experimental-vm-modules',
26+
});
27+
try {
28+
expect(exitCode).toBe(0);
29+
} catch (error) {
30+
console.log(`Test failed on iteration ${i + 1}`);
31+
throw error;
32+
}
33+
}
34+
});
35+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
const {fn} = require('../fake-dep');
9+
10+
test('returns correct message', () => {
11+
expect(fn()).toEqual('hello from CJS');
12+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
import {fn} from '../fake-dep';
9+
10+
test('returns correct message', () => {
11+
expect(fn()).toEqual('hello from ESM');
12+
});
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+
module.exports = {
9+
fn: () => 'hello from CJS',
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 ESM';
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "fake-dep",
3+
"version": "1.0.0",
4+
"exports": {
5+
".": {
6+
"import": "./module.mjs",
7+
"require": "./module.cjs"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)