Skip to content

Commit e4f79b5

Browse files
committed
Fix dependency clash test modified in jestjs#6104
1 parent 8a7b4a9 commit e4f79b5

File tree

1 file changed

+41
-36
lines changed

1 file changed

+41
-36
lines changed

e2e/__tests__/dependency_clash.test.js

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,69 +8,74 @@
88
*/
99

1010
import path from 'path';
11-
import {
12-
cleanup,
13-
createEmptyPackage,
14-
linkJestPackage,
15-
writeFiles,
16-
} from '../Utils';
11+
import {cleanup, createEmptyPackage, writeFiles} from '../Utils';
1712
import runJest from '../runJest';
1813
import os from 'os';
19-
import mkdirp from 'mkdirp';
20-
import fs from 'fs';
2114
import {skipSuiteOnWindows} from '../../scripts/ConditionalTest';
2215

2316
skipSuiteOnWindows();
2417

2518
// doing test in a temp directory because we don't want jest node_modules affect it
2619
const tempDir = path.resolve(os.tmpdir(), 'clashing-dependencies-test');
27-
const thirdPartyDir = path.resolve(tempDir, 'third-party');
20+
const hasteImplModulePath = path.resolve(
21+
'./packages/jest-haste-map/src/__tests__/haste_impl.js',
22+
);
2823

2924
beforeEach(() => {
3025
cleanup(tempDir);
3126
createEmptyPackage(tempDir);
32-
mkdirp(path.join(thirdPartyDir, 'node_modules'));
33-
linkJestPackage('babel-jest', thirdPartyDir);
3427
});
3528

3629
// This test case is checking that when having both
3730
// `invariant` package from npm and `invariant.js` that provides `invariant`
3831
// module we can still require the right invariant. This is pretty specific
3932
// use case and in the future we should probably delete this test.
4033
// see: https://github.com/facebook/jest/pull/6687
41-
test('fails with syntax error on flow types', () => {
42-
const babelFileThatRequiresInvariant = require.resolve(
43-
'babel-traverse/lib/path/index.js',
44-
);
45-
46-
expect(fs.existsSync(babelFileThatRequiresInvariant)).toBe(true);
47-
// make sure the babel depenency that depends on `invariant` from npm still
48-
// exists, otherwise the test will pass regardless of whether the bug still
49-
// exists or no.
50-
expect(fs.readFileSync(babelFileThatRequiresInvariant).toString()).toMatch(
51-
/invariant/,
52-
);
34+
test('does not require project modules from inside node_modules', () => {
5335
writeFiles(tempDir, {
54-
'.babelrc': `
55-
{
56-
"plugins": [
57-
"${require.resolve('babel-plugin-transform-flow-strip-types')}"
58-
]
59-
}
60-
`,
6136
'__tests__/test.js': `
62-
const invariant = require('../invariant');
37+
const invariant = require('invariant');
6338
test('haii', () => expect(invariant(false, 'haii')).toBe('haii'));
6439
`,
65-
'invariant.js': `/**
66-
* @flow
67-
*/
68-
const invariant = (condition: boolean, message: string) => message;
40+
'invariant.js': `
41+
INVALID CODE FRAGMENT THAT WILL BE REMOVED BY THE TRANSFORMER
42+
const invariant = (condition, message) => message;
6943
module.exports = invariant;
7044
`,
7145
'jest.config.js': `module.exports = {
72-
transform: {'.*\\.js': './third-party/node_modules/babel-jest'},
46+
haste: {
47+
hasteImplModulePath: '${hasteImplModulePath}',
48+
},
49+
transform: {'.*\\.js': './third-party/node_modules/transform'},
7350
};`,
51+
'third-party/node_modules/invariant/index.js': `
52+
const invariant = (condition, message) => {
53+
if (!condition) {
54+
throw new Error(message);
55+
}
56+
};
57+
module.exports = invariant;
58+
`,
59+
'third-party/node_modules/transform/index.js': `
60+
const invariant = require('invariant');
61+
module.exports = {
62+
process: script => {
63+
let threw = false;
64+
try {
65+
invariant(false, 'this should throw');
66+
} catch (e) {
67+
threw = true;
68+
}
69+
if (!threw) {
70+
throw new Error('It used the wrong invariant module!');
71+
}
72+
return script.replace(
73+
'INVALID CODE FRAGMENT THAT WILL BE REMOVED BY THE TRANSFORMER',
74+
''
75+
);
76+
},
77+
};
78+
`,
7479
});
7580
const {stderr, status} = runJest(tempDir, ['--no-cache', '--no-watchman']);
7681
// make sure there are no errors that lead to invariant.js (if we were to

0 commit comments

Comments
 (0)