diff --git a/CHANGELOG.md b/CHANGELOG.md index fc2f6c048665..87a9e3f28eaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,8 @@ issue. ([#4669](https://github.com/facebook/jest/pull/4669)) * `[jest-cli]` Fix `--onlyChanged` path case sensitivity on Windows platform ([#4730](https://github.com/facebook/jest/pull/4730)) +* `[jest-runtime]` Use realpath to match transformers + ([#5000](https://github.com/facebook/jest/pull/5000)) ### Features diff --git a/integration_tests/__tests__/transform-linked-modules.test.js b/integration_tests/__tests__/transform-linked-modules.test.js new file mode 100644 index 000000000000..fe257ad3eb45 --- /dev/null +++ b/integration_tests/__tests__/transform-linked-modules.test.js @@ -0,0 +1,12 @@ +// @flow + +'use strict'; + +const runJest = require('../runJest'); + +it('should transform linked modules', () => { + const result = runJest.json('transform-linked-modules', ['--no-cache']).json; + + expect(result.success).toBe(true); + expect(result.numTotalTests).toBe(2); +}); diff --git a/integration_tests/transform-linked-modules/__tests__/linked-modules.test.js b/integration_tests/transform-linked-modules/__tests__/linked-modules.test.js new file mode 100644 index 000000000000..0b8930cffccc --- /dev/null +++ b/integration_tests/transform-linked-modules/__tests__/linked-modules.test.js @@ -0,0 +1,9 @@ +test('normal file', () => { + const normal = require('../ignored/normal'); + expect(normal).toEqual('ignored/normal'); +}); + +test('symlink', () => { + const symlink = require('../ignored/symlink'); + expect(symlink).toEqual('transformed'); +}); diff --git a/integration_tests/transform-linked-modules/ignored/normal.js b/integration_tests/transform-linked-modules/ignored/normal.js new file mode 100644 index 000000000000..d2135611e150 --- /dev/null +++ b/integration_tests/transform-linked-modules/ignored/normal.js @@ -0,0 +1 @@ +module.exports = 'ignored/normal'; diff --git a/integration_tests/transform-linked-modules/ignored/symlink.js b/integration_tests/transform-linked-modules/ignored/symlink.js new file mode 120000 index 000000000000..296e29907920 --- /dev/null +++ b/integration_tests/transform-linked-modules/ignored/symlink.js @@ -0,0 +1 @@ +../package/index.js \ No newline at end of file diff --git a/integration_tests/transform-linked-modules/package.json b/integration_tests/transform-linked-modules/package.json new file mode 100644 index 000000000000..0887a1756750 --- /dev/null +++ b/integration_tests/transform-linked-modules/package.json @@ -0,0 +1,13 @@ +{ + "jest": { + "testEnvironment": "node", + "transformIgnorePatterns": [ + "/node_modules/", + "/__tests__", + "/ignored/" + ], + "transform": { + "^.+\\.js$": "/preprocessor.js" + } + } +} diff --git a/integration_tests/transform-linked-modules/package/index.js b/integration_tests/transform-linked-modules/package/index.js new file mode 100644 index 000000000000..e2717257ce1d --- /dev/null +++ b/integration_tests/transform-linked-modules/package/index.js @@ -0,0 +1 @@ +module.exports = 'package/index'; diff --git a/integration_tests/transform-linked-modules/preprocessor.js b/integration_tests/transform-linked-modules/preprocessor.js new file mode 100644 index 000000000000..26d81a441699 --- /dev/null +++ b/integration_tests/transform-linked-modules/preprocessor.js @@ -0,0 +1,5 @@ +module.exports = { + process() { + return 'module.exports = "transformed"'; + }, +}; diff --git a/packages/jest-runtime/src/script_transformer.js b/packages/jest-runtime/src/script_transformer.js index 0c97ea28f935..cd7e29aacdeb 100644 --- a/packages/jest-runtime/src/script_transformer.js +++ b/packages/jest-runtime/src/script_transformer.js @@ -182,12 +182,22 @@ export default class ScriptTransformer { }).code; } + _getRealPath(filepath: Path): Path { + try { + // $FlowFixMe + return process.binding('fs').realpath(filepath) || filepath; + } catch (err) { + return filepath; + } + } + transformSource( - filename: Path, + filepath: Path, content: string, instrument: boolean, mapCoverage: boolean, ) { + const filename = this._getRealPath(filepath); const transform = this._getTransformer(filename); const cacheFilePath = this._getFileCachePath( filename,