Skip to content

Commit

Permalink
Fix dependency clash test modified in jestjs#6104
Browse files Browse the repository at this point in the history
  • Loading branch information
rubennorte committed Oct 18, 2018
1 parent 8a7b4a9 commit e5c4502
Showing 1 changed file with 37 additions and 28 deletions.
65 changes: 37 additions & 28 deletions e2e/__tests__/dependency_clash.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,52 +24,61 @@ skipSuiteOnWindows();

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

beforeEach(() => {
cleanup(tempDir);
createEmptyPackage(tempDir);
mkdirp(path.join(thirdPartyDir, 'node_modules'));
linkJestPackage('babel-jest', thirdPartyDir);
});

// This test case is checking that when having both
// `invariant` package from npm and `invariant.js` that provides `invariant`
// module we can still require the right invariant. This is pretty specific
// use case and in the future we should probably delete this test.
// see: https://github.com/facebook/jest/pull/6687
test('fails with syntax error on flow types', () => {
const babelFileThatRequiresInvariant = require.resolve(
'babel-traverse/lib/path/index.js',
);

expect(fs.existsSync(babelFileThatRequiresInvariant)).toBe(true);
// make sure the babel depenency that depends on `invariant` from npm still
// exists, otherwise the test will pass regardless of whether the bug still
// exists or no.
expect(fs.readFileSync(babelFileThatRequiresInvariant).toString()).toMatch(
/invariant/,
);
test('does not require project modules from inside node_modules', () => {
writeFiles(tempDir, {
'.babelrc': `
{
"plugins": [
"${require.resolve('babel-plugin-transform-flow-strip-types')}"
]
}
`,
'__tests__/test.js': `
const invariant = require('../invariant');
const invariant = require('invariant');
test('haii', () => expect(invariant(false, 'haii')).toBe('haii'));
`,
'invariant.js': `/**
* @flow
*/
const invariant = (condition: boolean, message: string) => message;
'invariant.js': `
INVALID CODE FRAGMENT THAT WILL BE REMOVED BY THE TRANSFORMER
const invariant = (condition, message) => message;
module.exports = invariant;
`,
'third-party/node_modules/invariant/index.js': `
const invariant = (condition, message) => {
if (!condition) {
throw new Error(message);
}
};
module.exports = invariant;
`,
'third-party/node_modules/transform/index.js': `
const invariant = require('invariant');
module.exports = {
process: script => {
let threw = false;
try {
invariant(false, 'this should throw');
} catch (e) {
threw = true;
}
if (!threw) {
throw new Error('It used the wrong invariant module!');
}
return script.replace('INVALID CODE FRAGMENT THAT WILL BE REMOVED BY THE TRANSFORMER', '');
},
};
`,
'jest.config.js': `module.exports = {
transform: {'.*\\.js': './third-party/node_modules/babel-jest'},
haste: {
hasteImplModulePath: '${hasteImplModulePath}',
},
transform: {'.*\\.js': './third-party/node_modules/transform'},
};`,
});
const {stderr, status} = runJest(tempDir, ['--no-cache', '--no-watchman']);
Expand Down

0 comments on commit e5c4502

Please sign in to comment.