From c8df3a7bc086507589fd3b4ffa78e61653564b9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Sat, 9 Mar 2019 12:23:06 +0000 Subject: [PATCH 1/9] Adds native support for PnP to jest --- packages/jest-resolve/package.json | 1 + packages/jest-resolve/src/defaultResolver.ts | 5 +++++ yarn.lock | 5 +++++ 3 files changed, 11 insertions(+) diff --git a/packages/jest-resolve/package.json b/packages/jest-resolve/package.json index a0005bd1820b..f094ca84f9b4 100644 --- a/packages/jest-resolve/package.json +++ b/packages/jest-resolve/package.json @@ -13,6 +13,7 @@ "@jest/types": "^24.3.0", "browser-resolve": "^1.11.3", "chalk": "^2.0.1", + "jest-pnp-resolver": "^1.2.0", "realpath-native": "^1.1.0" }, "devDependencies": { diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index 6cda0840d98e..1a29a031bb4e 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -8,6 +8,7 @@ import fs from 'fs'; import path from 'path'; import browserResolve from 'browser-resolve'; +import pnpResolver from 'jest-pnp-resolver'; import {Config} from '@jest/types'; import isBuiltinModule from './isBuiltinModule'; import nodeModulesPaths from './nodeModulesPaths'; @@ -26,6 +27,10 @@ export default function defaultResolver( path: Config.Path, options: ResolverOptions, ): Config.Path { + if (process.versions.pnp) { + return pnpResolver(path, options); + } + const resolve = options.browser ? browserResolve.sync : resolveSync; return resolve(path, { diff --git a/yarn.lock b/yarn.lock index 340a5d50f880..8dfc558088de 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7654,6 +7654,11 @@ jest-junit@^6.2.1: strip-ansi "^4.0.0" xml "^1.0.1" +jest-pnp-resolver@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/jest-pnp-resolver/-/jest-pnp-resolver-1.2.0.tgz#3e378643176fda5999efe18b61f5221dfe65fe3f" + integrity sha512-ol/Eg87R5D1jQ13e0anFV2B6i4AEFyBnY8XQnKVPSxpVBRtGVv8OYaZSbhYRu7bRdFJXj4dDaSSgEoZUj81OYA== + jest-serializer@24.0.0-alpha.6: version "24.0.0-alpha.6" resolved "https://registry.yarnpkg.com/jest-serializer/-/jest-serializer-24.0.0-alpha.6.tgz#27d2fee4b1a85698717a30c3ec2ab80767312597" From ca6d13d5e10272cf244ddb189e724cbf6af2f1ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Sat, 9 Mar 2019 13:25:24 +0000 Subject: [PATCH 2/9] Adds a test --- .gitignore | 2 ++ e2e/__tests__/pnp.test.ts | 22 ++++++++++++++++++++ e2e/pnp/__tests__/index.js | 6 ++++++ e2e/pnp/lib/index.js | 1 + e2e/pnp/lib/package.json | 3 +++ e2e/pnp/package.json | 8 +++++++ e2e/pnp/yarn.lock | 7 +++++++ e2e/runJest.ts | 2 ++ packages/jest-resolve/src/defaultResolver.ts | 1 + 9 files changed, 52 insertions(+) create mode 100644 e2e/__tests__/pnp.test.ts create mode 100644 e2e/pnp/__tests__/index.js create mode 100644 e2e/pnp/lib/index.js create mode 100644 e2e/pnp/lib/package.json create mode 100644 e2e/pnp/package.json create mode 100644 e2e/pnp/yarn.lock diff --git a/.gitignore b/.gitignore index ee3195b8c97a..aa61f944a03a 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,8 @@ /examples/*/node_modules/ /e2e/*/node_modules +/e2e/*/.pnp +/e2e/*/.pnp.js !/e2e/presets/json/node_modules !/e2e/presets/js/node_modules /e2e/transform/*/coverage diff --git a/e2e/__tests__/pnp.test.ts b/e2e/__tests__/pnp.test.ts new file mode 100644 index 000000000000..a51860edc104 --- /dev/null +++ b/e2e/__tests__/pnp.test.ts @@ -0,0 +1,22 @@ +/** + * 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 path from 'path'; +import {json as runWithJson} from '../runJest'; +import {run} from '../Utils'; + +const DIR = path.resolve(__dirname, '..', 'pnp'); + +beforeEach(() => { + run('yarn', DIR); +}); + +it('sucessfully runs the tests inside `pnp/`', () => { + const {json} = runWithJson(DIR, ['--no-cache', '--coverage'], {nodeOptions: `--require ${DIR}/.pnp.js`}); + expect(json.success).toBe(true); + expect(json.numTotalTestSuites).toBe(1); +}); diff --git a/e2e/pnp/__tests__/index.js b/e2e/pnp/__tests__/index.js new file mode 100644 index 000000000000..6cea8f56f84a --- /dev/null +++ b/e2e/pnp/__tests__/index.js @@ -0,0 +1,6 @@ +const lib = require('foo'); + +it('should work', () => { + expect(process.versions.pnp).toBeTruthy(); + expect(lib()).toEqual(42); +}); diff --git a/e2e/pnp/lib/index.js b/e2e/pnp/lib/index.js new file mode 100644 index 000000000000..e9f55a023ebc --- /dev/null +++ b/e2e/pnp/lib/index.js @@ -0,0 +1 @@ +module.exports = () => 42; diff --git a/e2e/pnp/lib/package.json b/e2e/pnp/lib/package.json new file mode 100644 index 000000000000..1be1b18fe5a8 --- /dev/null +++ b/e2e/pnp/lib/package.json @@ -0,0 +1,3 @@ +{ + "version": "0.0.0" +} diff --git a/e2e/pnp/package.json b/e2e/pnp/package.json new file mode 100644 index 000000000000..e25e02bd2111 --- /dev/null +++ b/e2e/pnp/package.json @@ -0,0 +1,8 @@ +{ + "dependencies": { + "foo": "link:./lib" + }, + "installConfig": { + "pnp": true + } +} diff --git a/e2e/pnp/yarn.lock b/e2e/pnp/yarn.lock new file mode 100644 index 000000000000..3f73f0689ff3 --- /dev/null +++ b/e2e/pnp/yarn.lock @@ -0,0 +1,7 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"foo@link:./lib": + version "0.0.0" + uid "" diff --git a/e2e/runJest.ts b/e2e/runJest.ts index 8e6f615eeacf..9ef973a499c6 100644 --- a/e2e/runJest.ts +++ b/e2e/runJest.ts @@ -16,6 +16,7 @@ import {normalizeIcons} from './Utils'; const JEST_PATH = path.resolve(__dirname, '../packages/jest-cli/bin/jest.js'); type RunJestOptions = { + nodeOptions?: string; nodePath?: string; skipPkgJsonCheck?: boolean; // don't complain if can't find package.json stripAnsi?: boolean; // remove colors from stdout and stderr, @@ -72,6 +73,7 @@ function spawnJest( } const env = Object.assign({}, process.env, {FORCE_COLOR: '0'}); + if (options.nodeOptions) env['NODE_OPTIONS'] = options.nodeOptions; if (options.nodePath) env['NODE_PATH'] = options.nodePath; const spawnArgs = [JEST_PATH, ...(args || [])]; diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index 1a29a031bb4e..44d94620b2b6 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -27,6 +27,7 @@ export default function defaultResolver( path: Config.Path, options: ResolverOptions, ): Config.Path { + // @ts-ignore: the "pnp" version named isn't in DefinitelyTyped if (process.versions.pnp) { return pnpResolver(path, options); } From 256612e04d301bb12cd92baf2adb64e27dfb9747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Sat, 9 Mar 2019 13:29:52 +0000 Subject: [PATCH 3/9] Runs prettier --- e2e/__tests__/pnp.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/e2e/__tests__/pnp.test.ts b/e2e/__tests__/pnp.test.ts index a51860edc104..b1181ced02e2 100644 --- a/e2e/__tests__/pnp.test.ts +++ b/e2e/__tests__/pnp.test.ts @@ -16,7 +16,9 @@ beforeEach(() => { }); it('sucessfully runs the tests inside `pnp/`', () => { - const {json} = runWithJson(DIR, ['--no-cache', '--coverage'], {nodeOptions: `--require ${DIR}/.pnp.js`}); + const {json} = runWithJson(DIR, ['--no-cache', '--coverage'], { + nodeOptions: `--require ${DIR}/.pnp.js`, + }); expect(json.success).toBe(true); expect(json.numTotalTestSuites).toBe(1); }); From 283d11d659f8b7e5be05ef5d9b1e8262d8e93031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Sat, 9 Mar 2019 13:30:43 +0000 Subject: [PATCH 4/9] Adds an entry into the changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cb6f47f709c..c66deca4bee1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[jest-resolve]` Now supports PnP environment without plugins ([#8094](https://github.com/facebook/jest/pull/8094)) + ### Fixes - `[expect]` Compare DOM nodes even if there are multiple Node classes ([#8064](https://github.com/facebook/jest/pull/8064)) From b7524cf69d00a245d7dcde013d24f78cf9c954b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Sat, 9 Mar 2019 13:41:57 +0000 Subject: [PATCH 5/9] Uses jest-pnp-resolver 1.2.1 w/ ts types --- packages/jest-resolve/package.json | 2 +- yarn.lock | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/jest-resolve/package.json b/packages/jest-resolve/package.json index f094ca84f9b4..47cd54fa5624 100644 --- a/packages/jest-resolve/package.json +++ b/packages/jest-resolve/package.json @@ -13,7 +13,7 @@ "@jest/types": "^24.3.0", "browser-resolve": "^1.11.3", "chalk": "^2.0.1", - "jest-pnp-resolver": "^1.2.0", + "jest-pnp-resolver": "^1.2.1", "realpath-native": "^1.1.0" }, "devDependencies": { diff --git a/yarn.lock b/yarn.lock index 8dfc558088de..74432a485572 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7654,10 +7654,10 @@ jest-junit@^6.2.1: strip-ansi "^4.0.0" xml "^1.0.1" -jest-pnp-resolver@^1.2.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/jest-pnp-resolver/-/jest-pnp-resolver-1.2.0.tgz#3e378643176fda5999efe18b61f5221dfe65fe3f" - integrity sha512-ol/Eg87R5D1jQ13e0anFV2B6i4AEFyBnY8XQnKVPSxpVBRtGVv8OYaZSbhYRu7bRdFJXj4dDaSSgEoZUj81OYA== +jest-pnp-resolver@^1.2.1: + version "1.2.1" + resolved "https://registry.yarnpkg.com/jest-pnp-resolver/-/jest-pnp-resolver-1.2.1.tgz#ecdae604c077a7fbc70defb6d517c3c1c898923a" + integrity sha512-pgFw2tm54fzgYvc/OHrnysABEObZCUNFnhjoRjaVOCN8NYc032/gVjPaHD4Aq6ApkSieWtfKAFQtmDKAmhupnQ== jest-serializer@24.0.0-alpha.6: version "24.0.0-alpha.6" From a3d5e2769acae88488acb015860f997d96896e79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Sat, 9 Mar 2019 13:51:41 +0000 Subject: [PATCH 6/9] Fixes headers --- e2e/pnp/__tests__/index.js | 8 ++++++++ e2e/pnp/lib/index.js | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/e2e/pnp/__tests__/index.js b/e2e/pnp/__tests__/index.js index 6cea8f56f84a..d6d40a896254 100644 --- a/e2e/pnp/__tests__/index.js +++ b/e2e/pnp/__tests__/index.js @@ -1,3 +1,11 @@ +/** + * 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. + * + */ + const lib = require('foo'); it('should work', () => { diff --git a/e2e/pnp/lib/index.js b/e2e/pnp/lib/index.js index e9f55a023ebc..e878c1f6af16 100644 --- a/e2e/pnp/lib/index.js +++ b/e2e/pnp/lib/index.js @@ -1 +1,9 @@ +/** + * 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. + * + */ + module.exports = () => 42; From 86b09af7dd26ef6ae8c7a7ae4285e7168f9ed476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Sat, 9 Mar 2019 19:45:24 +0000 Subject: [PATCH 7/9] Stops execa from "preferLocal" --- e2e/Utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/Utils.ts b/e2e/Utils.ts index 1f5daba040ce..c54e6a69bf9d 100644 --- a/e2e/Utils.ts +++ b/e2e/Utils.ts @@ -20,7 +20,7 @@ export type RunResult = ExecaReturns & { }; export const run = (cmd: string, cwd?: Config.Path): RunResult => { const args = cmd.split(/\s/).slice(1); - const spawnOptions = {cwd, reject: false}; + const spawnOptions = {cwd, preferLocal: false, reject: false}; const result = spawnSync(cmd.split(/\s/)[0], args, spawnOptions) as RunResult; // For compat with cross-spawn From 29cd69913b9f8bb9d0bff9ec6b53df442ee5913f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Sat, 9 Mar 2019 20:52:39 +0000 Subject: [PATCH 8/9] Disables the pnp test on windows --- e2e/__tests__/pnp.test.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/e2e/__tests__/pnp.test.ts b/e2e/__tests__/pnp.test.ts index b1181ced02e2..177100619c1e 100644 --- a/e2e/__tests__/pnp.test.ts +++ b/e2e/__tests__/pnp.test.ts @@ -16,9 +16,12 @@ beforeEach(() => { }); it('sucessfully runs the tests inside `pnp/`', () => { - const {json} = runWithJson(DIR, ['--no-cache', '--coverage'], { - nodeOptions: `--require ${DIR}/.pnp.js`, - }); - expect(json.success).toBe(true); - expect(json.numTotalTestSuites).toBe(1); + // https://github.com/facebook/jest/pull/8094#issuecomment-471220694 + if (process.platform !== 'win32') { + const {json} = runWithJson(DIR, ['--no-cache', '--coverage'], { + nodeOptions: `--require ${DIR}/.pnp.js`, + }); + expect(json.success).toBe(true); + expect(json.numTotalTestSuites).toBe(1); + } }); From 80b65fb71fdc82067a18b75b637de76b6a707feb Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 9 Mar 2019 23:30:22 +0100 Subject: [PATCH 9/9] Update pnp.test.ts --- e2e/__tests__/pnp.test.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/e2e/__tests__/pnp.test.ts b/e2e/__tests__/pnp.test.ts index 177100619c1e..2df3f75172fd 100644 --- a/e2e/__tests__/pnp.test.ts +++ b/e2e/__tests__/pnp.test.ts @@ -6,22 +6,23 @@ */ import path from 'path'; +import {skipSuiteOnWindows} from '@jest/test-utils'; import {json as runWithJson} from '../runJest'; import {run} from '../Utils'; const DIR = path.resolve(__dirname, '..', 'pnp'); +// https://github.com/facebook/jest/pull/8094#issuecomment-471220694 +skipSuiteOnWindows(); + beforeEach(() => { run('yarn', DIR); }); it('sucessfully runs the tests inside `pnp/`', () => { - // https://github.com/facebook/jest/pull/8094#issuecomment-471220694 - if (process.platform !== 'win32') { - const {json} = runWithJson(DIR, ['--no-cache', '--coverage'], { - nodeOptions: `--require ${DIR}/.pnp.js`, - }); - expect(json.success).toBe(true); - expect(json.numTotalTestSuites).toBe(1); - } + const {json} = runWithJson(DIR, ['--no-cache', '--coverage'], { + nodeOptions: `--require ${DIR}/.pnp.js`, + }); + expect(json.success).toBe(true); + expect(json.numTotalTestSuites).toBe(1); });