|
8 | 8 | */ |
9 | 9 |
|
10 | 10 | import path from 'path'; |
11 | | -import { |
12 | | - cleanup, |
13 | | - createEmptyPackage, |
14 | | - linkJestPackage, |
15 | | - writeFiles, |
16 | | -} from '../Utils'; |
| 11 | +import {cleanup, createEmptyPackage, writeFiles} from '../Utils'; |
17 | 12 | import runJest from '../runJest'; |
18 | 13 | import os from 'os'; |
19 | | -import mkdirp from 'mkdirp'; |
20 | | -import fs from 'fs'; |
21 | 14 | import {skipSuiteOnWindows} from '../../scripts/ConditionalTest'; |
22 | 15 |
|
23 | 16 | skipSuiteOnWindows(); |
24 | 17 |
|
25 | 18 | // doing test in a temp directory because we don't want jest node_modules affect it |
26 | 19 | 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 | +); |
28 | 23 |
|
29 | 24 | beforeEach(() => { |
30 | 25 | cleanup(tempDir); |
31 | 26 | createEmptyPackage(tempDir); |
32 | | - mkdirp(path.join(thirdPartyDir, 'node_modules')); |
33 | | - linkJestPackage('babel-jest', thirdPartyDir); |
34 | 27 | }); |
35 | 28 |
|
36 | 29 | // This test case is checking that when having both |
37 | 30 | // `invariant` package from npm and `invariant.js` that provides `invariant` |
38 | 31 | // module we can still require the right invariant. This is pretty specific |
39 | 32 | // use case and in the future we should probably delete this test. |
40 | 33 | // 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', () => { |
53 | 35 | writeFiles(tempDir, { |
54 | | - '.babelrc': ` |
55 | | - { |
56 | | - "plugins": [ |
57 | | - "${require.resolve('babel-plugin-transform-flow-strip-types')}" |
58 | | - ] |
59 | | - } |
60 | | - `, |
61 | 36 | '__tests__/test.js': ` |
62 | | - const invariant = require('../invariant'); |
| 37 | + const invariant = require('invariant'); |
63 | 38 | test('haii', () => expect(invariant(false, 'haii')).toBe('haii')); |
64 | 39 | `, |
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; |
69 | 43 | module.exports = invariant; |
70 | 44 | `, |
71 | 45 | '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'}, |
73 | 50 | };`, |
| 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 | + `, |
74 | 79 | }); |
75 | 80 | const {stderr, status} = runJest(tempDir, ['--no-cache', '--no-watchman']); |
76 | 81 | // make sure there are no errors that lead to invariant.js (if we were to |
|
0 commit comments