Skip to content

Commit

Permalink
fix(jest-resolve): disable jest-pnp-resolver for Yarn 2
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Nov 19, 2020
1 parent a66eec7 commit f0c5178
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- `[jest-transform]` Show enhanced `SyntaxError` message for all `SyntaxError`s ([#10749](https://github.com/facebook/jest/pull/10749))
- `[jest-transform]` [**BREAKING**] Refactor API to pass an options bag around rather than multiple boolean options ([#10753](https://github.com/facebook/jest/pull/10753))
- `[jest-transform]` [**BREAKING**] Refactor API of transformers to pass an options bag rather than separate `config` and other options
- `[jest-resolve]` Disable `jest-pnp-resolver` for Yarn 2 ([#10847](https://github.com/facebook/jest/pull/10847))

### Chore & Maintenance

Expand Down
18 changes: 9 additions & 9 deletions e2e/__tests__/pnp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ import {json as runWithJson} from '../runJest';
const DIR = path.resolve(__dirname, '..', 'pnp');

beforeEach(() => {
runYarnInstall(DIR, {YARN_NODE_LINKER: 'pnp'});
runYarnInstall(DIR, {
YARN_ENABLE_GLOBAL_CACHE: 'false',
YARN_NODE_LINKER: 'pnp',
});
});

it('successfully runs the tests inside `pnp/`', () => {
// https://github.com/facebook/jest/pull/8094#issuecomment-471220694
if (process.platform === 'win32') {
console.warn('[SKIP] Does not work on Windows');

return;
}

const {json} = runWithJson(DIR, ['--no-cache', '--coverage'], {
env: {YARN_NODE_LINKER: 'pnp'},
env: {
YARN_ENABLE_GLOBAL_CACHE: 'false',
YARN_NODE_LINKER: 'pnp',
},
nodeOptions: `--require ${DIR}/.pnp.js`,
});

expect(json.success).toBe(true);
expect(json.numTotalTestSuites).toBe(2);
});
2 changes: 1 addition & 1 deletion e2e/pnp/__tests__/undeclared-dependency.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
it('should surface pnp errors', () => {
expect(() => {
require('undeclared');
}).toThrow(expect.objectContaining({pnpCode: 'UNDECLARED_DEPENDENCY'}));
}).toThrow(expect.objectContaining({code: 'MODULE_NOT_FOUND'}));
});
4 changes: 2 additions & 2 deletions e2e/pnp/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"dependencies": {
"foo": "link:./lib",
"undeclared": "link:./undeclared-dependency"
"foo": "portal:./lib",
"undeclared": "portal:./undeclared-dependency"
},
"installConfig": {
"pnp": true
Expand Down
12 changes: 6 additions & 6 deletions e2e/pnp/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
__metadata:
version: 4

"foo@link:./lib::locator=root-workspace-0b6124%40workspace%3A.":
"foo@portal:./lib::locator=root-workspace-0b6124%40workspace%3A.":
version: 0.0.0-use.local
resolution: "foo@link:./lib::locator=root-workspace-0b6124%40workspace%3A."
resolution: "foo@portal:./lib::locator=root-workspace-0b6124%40workspace%3A."
languageName: node
linkType: soft

"root-workspace-0b6124@workspace:.":
version: 0.0.0-use.local
resolution: "root-workspace-0b6124@workspace:."
dependencies:
foo: "link:./lib"
undeclared: "link:./undeclared-dependency"
foo: "portal:./lib"
undeclared: "portal:./undeclared-dependency"
languageName: unknown
linkType: soft

"undeclared@link:./undeclared-dependency::locator=root-workspace-0b6124%40workspace%3A.":
"undeclared@portal:./undeclared-dependency::locator=root-workspace-0b6124%40workspace%3A.":
version: 0.0.0-use.local
resolution: "undeclared@link:./undeclared-dependency::locator=root-workspace-0b6124%40workspace%3A."
resolution: "undeclared@portal:./undeclared-dependency::locator=root-workspace-0b6124%40workspace%3A."
languageName: node
linkType: soft
5 changes: 3 additions & 2 deletions packages/jest-resolve/src/defaultResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type {Config} from '@jest/types';
import {tryRealpath} from 'jest-util';

type ResolverOptions = {
allowPnp?: boolean;
basedir: Config.Path;
browser?: boolean;
defaultResolver: typeof defaultResolver;
Expand All @@ -36,7 +35,9 @@ export default function defaultResolver(
path: Config.Path,
options: ResolverOptions,
): Config.Path {
if (process.versions.pnp && options.allowPnp !== false) {
// Yarn 2 adds support to `resolve` automatically so the pnpResolver is only
// needed for Yarn 1 which implements version 1 of the pnp spec
if (process.versions.pnp === '1') {
return pnpResolver(path, options);
}

Expand Down

0 comments on commit f0c5178

Please sign in to comment.