From c929ee3bc7c5d42ea0785003a72616c40556cc71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Sat, 9 Mar 2019 23:30:37 +0000 Subject: [PATCH] Adds native support for PnP to jest (#8094) --- .gitignore | 2 ++ CHANGELOG.md | 2 ++ e2e/Utils.ts | 2 +- e2e/__tests__/pnp.test.ts | 28 ++++++++++++++++++++ e2e/pnp/__tests__/index.js | 14 ++++++++++ e2e/pnp/lib/index.js | 9 +++++++ e2e/pnp/lib/package.json | 3 +++ e2e/pnp/package.json | 8 ++++++ e2e/pnp/yarn.lock | 7 +++++ e2e/runJest.ts | 2 ++ packages/jest-resolve/package.json | 1 + packages/jest-resolve/src/defaultResolver.ts | 6 +++++ yarn.lock | 5 ++++ 13 files changed, 88 insertions(+), 1 deletion(-) 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/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)) 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 diff --git a/e2e/__tests__/pnp.test.ts b/e2e/__tests__/pnp.test.ts new file mode 100644 index 000000000000..2df3f75172fd --- /dev/null +++ b/e2e/__tests__/pnp.test.ts @@ -0,0 +1,28 @@ +/** + * 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 {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/`', () => { + 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..d6d40a896254 --- /dev/null +++ b/e2e/pnp/__tests__/index.js @@ -0,0 +1,14 @@ +/** + * 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', () => { + 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..e878c1f6af16 --- /dev/null +++ b/e2e/pnp/lib/index.js @@ -0,0 +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; 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/package.json b/packages/jest-resolve/package.json index a0005bd1820b..47cd54fa5624 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.1", "realpath-native": "^1.1.0" }, "devDependencies": { diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index 6cda0840d98e..44d94620b2b6 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,11 @@ 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); + } + const resolve = options.browser ? browserResolve.sync : resolveSync; return resolve(path, { diff --git a/yarn.lock b/yarn.lock index 340a5d50f880..74432a485572 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.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" resolved "https://registry.yarnpkg.com/jest-serializer/-/jest-serializer-24.0.0-alpha.6.tgz#27d2fee4b1a85698717a30c3ec2ab80767312597"