diff --git a/CHANGELOG.md b/CHANGELOG.md index 54974bd5946b..b381b589687c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,6 +65,7 @@ - `[*]` [**BREAKING**] Add `exports` field to all `package.json`s ([#9921](https://github.com/facebook/jest/pull/9921)) - `[*]` Make it easier for Jest's packages to use the VM escape hatch ([#10824](https://github.com/facebook/jest/pull/10824)) - `[*]` [**BREAKING**] Remove deprecated `mapCoverage` ([#9968](https://github.com/facebook/jest/pull/9968)) +- `[docs]` Correct example using `browser-resolve` ([#11140](https://github.com/facebook/jest/pull/11140)) - `[jest-config]` [**BREAKING**] Remove `enabledTestsMap` config, use `filter` instead ([#10787](https://github.com/facebook/jest/pull/10787)) - `[jest-console]` [**BREAKING**] Move `root` into `config` and take `GlobalConfig` as mandatory parameter for `getConsoleOutput` ([#10126](https://github.com/facebook/jest/pull/10126)) - `[jest-console]` Export LogEntry ([#11017](https://github.com/facebook/jest/pull/11017)) diff --git a/docs/Configuration.md b/docs/Configuration.md index d0f895117465..a419b32977aa 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -784,11 +784,18 @@ For example, if you want to respect Browserify's [`"browser"` field](https://git { ... "jest": { - "resolver": "browser-resolve" + "resolver": "/resolver.js" } } ``` +```js +// resolver.js +const browserResolve = require('browser-resolve'); + +module.exports = browserResolve.sync; +``` + 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"`: ```json diff --git a/e2e/__tests__/browserResolve.ts b/e2e/__tests__/browserResolve.ts new file mode 100644 index 000000000000..8bfc5baadf74 --- /dev/null +++ b/e2e/__tests__/browserResolve.ts @@ -0,0 +1,19 @@ +/** + * 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. + */ + +import * as path from 'path'; +import {runYarnInstall} from '../Utils'; +import runJest from '../runJest'; + +test('browser resolver works', () => { + const dir = path.resolve(__dirname, '../browser-resolver'); + runYarnInstall(dir); + + const {exitCode} = runJest('browser-resolver'); + + expect(exitCode).toBe(0); +}); diff --git a/e2e/browser-resolver/__tests__/test.js b/e2e/browser-resolver/__tests__/test.js new file mode 100644 index 000000000000..92e94fb0ea4b --- /dev/null +++ b/e2e/browser-resolver/__tests__/test.js @@ -0,0 +1,16 @@ +/** + * 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. + */ + +/* eslint-env browser */ + +'use strict'; + +const div = require('../fake-pkg'); + +test('dummy test', () => { + expect(div).toBeInstanceOf(HTMLDivElement); +}); diff --git a/e2e/browser-resolver/fake-pkg/main.js b/e2e/browser-resolver/fake-pkg/main.js new file mode 100644 index 000000000000..eac7e93aced9 --- /dev/null +++ b/e2e/browser-resolver/fake-pkg/main.js @@ -0,0 +1,16 @@ +/** + * 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. + */ + +/* eslint-env browser */ + +'use strict'; + +const div = document.createElement('div'); + +div.innerText = 'Hello!'; + +module.exports = div; diff --git a/e2e/browser-resolver/fake-pkg/package.json b/e2e/browser-resolver/fake-pkg/package.json new file mode 100644 index 000000000000..93cece076b35 --- /dev/null +++ b/e2e/browser-resolver/fake-pkg/package.json @@ -0,0 +1,10 @@ +{ + "name": "fake-pkg", + "version": "1.0.0", + "main": "./nope.js", + "browser": { + "./nope.js": "./main.js" + }, + "dependencies": { + } +} diff --git a/e2e/browser-resolver/package.json b/e2e/browser-resolver/package.json new file mode 100644 index 000000000000..c39853aef7c4 --- /dev/null +++ b/e2e/browser-resolver/package.json @@ -0,0 +1,9 @@ +{ + "jest": { + "resolver": "/resolver.js", + "testEnvironment": "jsdom" + }, + "dependencies": { + "browser-resolve": "^2.0.0" + } +} diff --git a/e2e/browser-resolver/resolver.js b/e2e/browser-resolver/resolver.js new file mode 100644 index 000000000000..66166811a4de --- /dev/null +++ b/e2e/browser-resolver/resolver.js @@ -0,0 +1,12 @@ +/** + * 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. + */ + +'use strict'; + +const browserResolve = require('browser-resolve'); + +module.exports = browserResolve.sync; diff --git a/e2e/browser-resolver/yarn.lock b/e2e/browser-resolver/yarn.lock new file mode 100644 index 000000000000..aa5e7caa6568 --- /dev/null +++ b/e2e/browser-resolver/yarn.lock @@ -0,0 +1,75 @@ +# This file is generated by running "yarn install" inside your project. +# Manual changes might be lost - proceed with caution! + +__metadata: + version: 4 + cacheKey: 7 + +"browser-resolve@npm:^2.0.0": + version: 2.0.0 + resolution: "browser-resolve@npm:2.0.0" + dependencies: + resolve: ^1.17.0 + checksum: 162cf825f0cd2c5921446af7abf6a98a538f41825a8562c354e5d11e49fbdb2917f12888c2560f08df3589902ce6538afc5c30c2778b6e2cd29a0bfa12ce780a + languageName: node + linkType: hard + +"function-bind@npm:^1.1.1": + version: 1.1.1 + resolution: "function-bind@npm:1.1.1" + checksum: ffad86e7d2010ba179aaa6a3987d2cc0ed48fa92d27f1ed84bfa06d14f77deeed5bfbae7f00bdebc0c54218392cab2b18ecc080e2c72f592431927b87a27d42b + languageName: node + linkType: hard + +"has@npm:^1.0.3": + version: 1.0.3 + resolution: "has@npm:1.0.3" + dependencies: + function-bind: ^1.1.1 + checksum: c686e15300d41364486c099a9259d9c418022c294244843dcd712c4c286ff839d4f23a25413baa28c4d2c1e828afc2aaab70f685400b391533980223c71fa1ca + languageName: node + linkType: hard + +"is-core-module@npm:^2.2.0": + version: 2.2.0 + resolution: "is-core-module@npm:2.2.0" + dependencies: + has: ^1.0.3 + checksum: 2344744de98a3bc22e2bb30895f307d7889f09e963f9bcb1cc321788f508c8b463f75e0a9ca009eeeb8eb9465181b5c15f1ec9299a6bb6921cfbb2423892e0ba + languageName: node + linkType: hard + +"path-parse@npm:^1.0.6": + version: 1.0.6 + resolution: "path-parse@npm:1.0.6" + checksum: 2eee4b93fb3ae13600e3fca18390d9933bbbcf725a624f6b8df020d87515a74872ff6c58072190d6dc75a5584a683dc6ae5c385ad4e4f4efb6e66af040d56c67 + languageName: node + linkType: hard + +resolve@^1.17.0: + version: 1.20.0 + resolution: "resolve@npm:1.20.0" + dependencies: + is-core-module: ^2.2.0 + path-parse: ^1.0.6 + checksum: 0f5206d454b30e74d9b2d575b5f8aedf443c4d8b90b84cdf79474ade29bb459075220da3127b682896872a16022ed65cc4db09e0f23849654144d3d75c65cd1b + languageName: node + linkType: hard + +"resolve@patch:resolve@^1.17.0#builtin": + version: 1.20.0 + resolution: "resolve@patch:resolve@npm%3A1.20.0#builtin::version=1.20.0&hash=3388aa" + dependencies: + is-core-module: ^2.2.0 + path-parse: ^1.0.6 + checksum: c4a515b76026806b5b26513fc7bdb80458c532bc91c02ef45ac928d1025585f93bec0b904be39c02131118a37ff7e3f9258f1526850b025d2ec0948bb5fd03d0 + languageName: node + linkType: hard + +"root-workspace-0b6124@workspace:.": + version: 0.0.0-use.local + resolution: "root-workspace-0b6124@workspace:." + dependencies: + browser-resolve: ^2.0.0 + languageName: unknown + linkType: soft diff --git a/packages/jest-config/src/Deprecated.ts b/packages/jest-config/src/Deprecated.ts index f43ec9d4d7a2..6c54b59e7db3 100644 --- a/packages/jest-config/src/Deprecated.ts +++ b/packages/jest-config/src/Deprecated.ts @@ -12,13 +12,10 @@ import prettyFormat from 'pretty-format'; const format = (value: unknown) => prettyFormat(value, {min: true}); const deprecatedOptions: DeprecatedOptions = { - browser: () => ` Option ${chalk.bold( - '"browser"', - )} has been deprecated. Please install "browser-resolve" and use the "resolver" option in Jest configuration as follows: - { - ${chalk.bold('"resolver"')}: ${chalk.bold('"browser-resolve"')} - } - `, + browser: () => + ` Option ${chalk.bold( + '"browser"', + )} 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`, preprocessorIgnorePatterns: (options: { preprocessorIgnorePatterns?: Array; diff --git a/website/blog/2020-05-05-jest-26.md b/website/blog/2020-05-05-jest-26.md index 7a4b7e268d0a..b5818a0a3641 100644 --- a/website/blog/2020-05-05-jest-26.md +++ b/website/blog/2020-05-05-jest-26.md @@ -22,11 +22,18 @@ We are now beginning to address this shortcoming and are working on reducing Jes ```json { "jest": { - "resolver": "browser-resolve" + "resolver": "/resolver.js" } } ``` + ```js + // resolver.js + const browserResolve = require('browser-resolve'); + + module.exports = browserResolve.sync; + ``` + - TypeScript definitions requires a minimum of TypeScript v3.8 ([#9823](https://github.com/facebook/jest/pull/9823)) With the above changes Jest 26 is now 4 MiB smaller than Jest 25.5.4 (53 → 49 MiB). Please keep in mind that many dependencies like Babel are likely already part of your project. Jest's own size was reduced by 1.2 MiB (4.3 -> 3.1 MiB). diff --git a/website/versioned_docs/version-26.0/Configuration.md b/website/versioned_docs/version-26.0/Configuration.md index 068de2987fea..99e7844b38fb 100644 --- a/website/versioned_docs/version-26.0/Configuration.md +++ b/website/versioned_docs/version-26.0/Configuration.md @@ -721,11 +721,18 @@ For example, if you want to respect Browserify's [`"browser"` field](https://git { ... "jest": { - "resolver": "browser-resolve" + "resolver": "/resolver.js" } } ``` +```js +// resolver.js +const browserResolve = require('browser-resolve'); + +module.exports = browserResolve.sync; +``` + ### `restoreMocks` [boolean] Default: `false` diff --git a/website/versioned_docs/version-26.2/Configuration.md b/website/versioned_docs/version-26.2/Configuration.md index 6a604b00d6f6..3d8bcec639ec 100644 --- a/website/versioned_docs/version-26.2/Configuration.md +++ b/website/versioned_docs/version-26.2/Configuration.md @@ -729,11 +729,18 @@ For example, if you want to respect Browserify's [`"browser"` field](https://git { ... "jest": { - "resolver": "browser-resolve" + "resolver": "/resolver.js" } } ``` +```js +// resolver.js +const browserResolve = require('browser-resolve'); + +module.exports = browserResolve.sync; +``` + ### `restoreMocks` [boolean] Default: `false` diff --git a/website/versioned_docs/version-26.4/Configuration.md b/website/versioned_docs/version-26.4/Configuration.md index 8f485c9149ff..ee56944dc720 100644 --- a/website/versioned_docs/version-26.4/Configuration.md +++ b/website/versioned_docs/version-26.4/Configuration.md @@ -730,11 +730,18 @@ For example, if you want to respect Browserify's [`"browser"` field](https://git { ... "jest": { - "resolver": "browser-resolve" + "resolver": "/resolver.js" } } ``` +```js +// resolver.js +const browserResolve = require('browser-resolve'); + +module.exports = browserResolve.sync; +``` + 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"`: ```json diff --git a/website/versioned_docs/version-26.5/Configuration.md b/website/versioned_docs/version-26.5/Configuration.md index 9ddbbd9ca9ff..123522961cae 100644 --- a/website/versioned_docs/version-26.5/Configuration.md +++ b/website/versioned_docs/version-26.5/Configuration.md @@ -748,11 +748,18 @@ For example, if you want to respect Browserify's [`"browser"` field](https://git { ... "jest": { - "resolver": "browser-resolve" + "resolver": "/resolver.js" } } ``` +```js +// resolver.js +const browserResolve = require('browser-resolve'); + +module.exports = browserResolve.sync; +``` + 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"`: ```json diff --git a/website/versioned_docs/version-26.6/Configuration.md b/website/versioned_docs/version-26.6/Configuration.md index c309eb22a005..81a8f0047a56 100644 --- a/website/versioned_docs/version-26.6/Configuration.md +++ b/website/versioned_docs/version-26.6/Configuration.md @@ -768,11 +768,18 @@ For example, if you want to respect Browserify's [`"browser"` field](https://git { ... "jest": { - "resolver": "browser-resolve" + "resolver": "/resolver.js" } } ``` +```js +// resolver.js +const browserResolve = require('browser-resolve'); + +module.exports = browserResolve.sync; +``` + 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"`: ```json