Skip to content

Commit 78af379

Browse files
authored
fix: add integration test using browser-resolve (#11140)
1 parent 711b602 commit 78af379

16 files changed

Lines changed: 218 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
- `[*]` [**BREAKING**] Add `exports` field to all `package.json`s ([#9921](https://github.com/facebook/jest/pull/9921))
6666
- `[*]` Make it easier for Jest's packages to use the VM escape hatch ([#10824](https://github.com/facebook/jest/pull/10824))
6767
- `[*]` [**BREAKING**] Remove deprecated `mapCoverage` ([#9968](https://github.com/facebook/jest/pull/9968))
68+
- `[docs]` Correct example using `browser-resolve` ([#11140](https://github.com/facebook/jest/pull/11140))
6869
- `[jest-config]` [**BREAKING**] Remove `enabledTestsMap` config, use `filter` instead ([#10787](https://github.com/facebook/jest/pull/10787))
6970
- `[jest-console]` [**BREAKING**] Move `root` into `config` and take `GlobalConfig` as mandatory parameter for `getConsoleOutput` ([#10126](https://github.com/facebook/jest/pull/10126))
7071
- `[jest-console]` Export LogEntry ([#11017](https://github.com/facebook/jest/pull/11017))

docs/Configuration.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,11 +784,18 @@ For example, if you want to respect Browserify's [`"browser"` field](https://git
784784
{
785785
...
786786
"jest": {
787-
"resolver": "browser-resolve"
787+
"resolver": "<rootDir>/resolver.js"
788788
}
789789
}
790790
```
791791

792+
```js
793+
// resolver.js
794+
const browserResolve = require('browser-resolve');
795+
796+
module.exports = browserResolve.sync;
797+
```
798+
792799
By combining `defaultResolver` and `packageFilter` we can implement a `package.json` "pre-processor" that allows us to change how the default resolver will resolve modules. For example, imagine we want to use the field `"module"` if it is present, otherwise fallback to `"main"`:
793800

794801
```json

e2e/__tests__/browserResolve.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 * as path from 'path';
9+
import {runYarnInstall} from '../Utils';
10+
import runJest from '../runJest';
11+
12+
test('browser resolver works', () => {
13+
const dir = path.resolve(__dirname, '../browser-resolver');
14+
runYarnInstall(dir);
15+
16+
const {exitCode} = runJest('browser-resolver');
17+
18+
expect(exitCode).toBe(0);
19+
});
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+
/* eslint-env browser */
9+
10+
'use strict';
11+
12+
const div = require('../fake-pkg');
13+
14+
test('dummy test', () => {
15+
expect(div).toBeInstanceOf(HTMLDivElement);
16+
});
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+
/* eslint-env browser */
9+
10+
'use strict';
11+
12+
const div = document.createElement('div');
13+
14+
div.innerText = 'Hello!';
15+
16+
module.exports = div;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "fake-pkg",
3+
"version": "1.0.0",
4+
"main": "./nope.js",
5+
"browser": {
6+
"./nope.js": "./main.js"
7+
},
8+
"dependencies": {
9+
}
10+
}

e2e/browser-resolver/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"jest": {
3+
"resolver": "<rootDir>/resolver.js",
4+
"testEnvironment": "jsdom"
5+
},
6+
"dependencies": {
7+
"browser-resolve": "^2.0.0"
8+
}
9+
}

e2e/browser-resolver/resolver.js

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+
'use strict';
9+
10+
const browserResolve = require('browser-resolve');
11+
12+
module.exports = browserResolve.sync;

e2e/browser-resolver/yarn.lock

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# This file is generated by running "yarn install" inside your project.
2+
# Manual changes might be lost - proceed with caution!
3+
4+
__metadata:
5+
version: 4
6+
cacheKey: 7
7+
8+
"browser-resolve@npm:^2.0.0":
9+
version: 2.0.0
10+
resolution: "browser-resolve@npm:2.0.0"
11+
dependencies:
12+
resolve: ^1.17.0
13+
checksum: 162cf825f0cd2c5921446af7abf6a98a538f41825a8562c354e5d11e49fbdb2917f12888c2560f08df3589902ce6538afc5c30c2778b6e2cd29a0bfa12ce780a
14+
languageName: node
15+
linkType: hard
16+
17+
"function-bind@npm:^1.1.1":
18+
version: 1.1.1
19+
resolution: "function-bind@npm:1.1.1"
20+
checksum: ffad86e7d2010ba179aaa6a3987d2cc0ed48fa92d27f1ed84bfa06d14f77deeed5bfbae7f00bdebc0c54218392cab2b18ecc080e2c72f592431927b87a27d42b
21+
languageName: node
22+
linkType: hard
23+
24+
"has@npm:^1.0.3":
25+
version: 1.0.3
26+
resolution: "has@npm:1.0.3"
27+
dependencies:
28+
function-bind: ^1.1.1
29+
checksum: c686e15300d41364486c099a9259d9c418022c294244843dcd712c4c286ff839d4f23a25413baa28c4d2c1e828afc2aaab70f685400b391533980223c71fa1ca
30+
languageName: node
31+
linkType: hard
32+
33+
"is-core-module@npm:^2.2.0":
34+
version: 2.2.0
35+
resolution: "is-core-module@npm:2.2.0"
36+
dependencies:
37+
has: ^1.0.3
38+
checksum: 2344744de98a3bc22e2bb30895f307d7889f09e963f9bcb1cc321788f508c8b463f75e0a9ca009eeeb8eb9465181b5c15f1ec9299a6bb6921cfbb2423892e0ba
39+
languageName: node
40+
linkType: hard
41+
42+
"path-parse@npm:^1.0.6":
43+
version: 1.0.6
44+
resolution: "path-parse@npm:1.0.6"
45+
checksum: 2eee4b93fb3ae13600e3fca18390d9933bbbcf725a624f6b8df020d87515a74872ff6c58072190d6dc75a5584a683dc6ae5c385ad4e4f4efb6e66af040d56c67
46+
languageName: node
47+
linkType: hard
48+
49+
resolve@^1.17.0:
50+
version: 1.20.0
51+
resolution: "resolve@npm:1.20.0"
52+
dependencies:
53+
is-core-module: ^2.2.0
54+
path-parse: ^1.0.6
55+
checksum: 0f5206d454b30e74d9b2d575b5f8aedf443c4d8b90b84cdf79474ade29bb459075220da3127b682896872a16022ed65cc4db09e0f23849654144d3d75c65cd1b
56+
languageName: node
57+
linkType: hard
58+
59+
"resolve@patch:resolve@^1.17.0#builtin<compat/resolve>":
60+
version: 1.20.0
61+
resolution: "resolve@patch:resolve@npm%3A1.20.0#builtin<compat/resolve>::version=1.20.0&hash=3388aa"
62+
dependencies:
63+
is-core-module: ^2.2.0
64+
path-parse: ^1.0.6
65+
checksum: c4a515b76026806b5b26513fc7bdb80458c532bc91c02ef45ac928d1025585f93bec0b904be39c02131118a37ff7e3f9258f1526850b025d2ec0948bb5fd03d0
66+
languageName: node
67+
linkType: hard
68+
69+
"root-workspace-0b6124@workspace:.":
70+
version: 0.0.0-use.local
71+
resolution: "root-workspace-0b6124@workspace:."
72+
dependencies:
73+
browser-resolve: ^2.0.0
74+
languageName: unknown
75+
linkType: soft

packages/jest-config/src/Deprecated.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ import prettyFormat from 'pretty-format';
1212
const format = (value: unknown) => prettyFormat(value, {min: true});
1313

1414
const deprecatedOptions: DeprecatedOptions = {
15-
browser: () => ` Option ${chalk.bold(
16-
'"browser"',
17-
)} has been deprecated. Please install "browser-resolve" and use the "resolver" option in Jest configuration as follows:
18-
{
19-
${chalk.bold('"resolver"')}: ${chalk.bold('"browser-resolve"')}
20-
}
21-
`,
15+
browser: () =>
16+
` Option ${chalk.bold(
17+
'"browser"',
18+
)} has been deprecated. Please install "browser-resolve" and use the "resolver" option in Jest configuration as shown in the documentation: https://jestjs.io/docs/en/configuration#resolver-string`,
2219

2320
preprocessorIgnorePatterns: (options: {
2421
preprocessorIgnorePatterns?: Array<string>;

0 commit comments

Comments
 (0)