Skip to content

Commit

Permalink
Adds native support for PnP to jest (#8094)
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanis authored and SimenB committed Mar 9, 2019
1 parent 562ac18 commit c929ee3
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion e2e/Utils.ts
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions 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);
});
14 changes: 14 additions & 0 deletions 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);
});
9 changes: 9 additions & 0 deletions 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;
3 changes: 3 additions & 0 deletions e2e/pnp/lib/package.json
@@ -0,0 +1,3 @@
{
"version": "0.0.0"
}
8 changes: 8 additions & 0 deletions e2e/pnp/package.json
@@ -0,0 +1,8 @@
{
"dependencies": {
"foo": "link:./lib"
},
"installConfig": {
"pnp": true
}
}
7 changes: 7 additions & 0 deletions 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 ""
2 changes: 2 additions & 0 deletions e2e/runJest.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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 || [])];
Expand Down
1 change: 1 addition & 0 deletions packages/jest-resolve/package.json
Expand Up @@ -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": {
Expand Down
6 changes: 6 additions & 0 deletions packages/jest-resolve/src/defaultResolver.ts
Expand Up @@ -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';
Expand All @@ -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, {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -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"
Expand Down

0 comments on commit c929ee3

Please sign in to comment.