From d711ceeee41dc796dbd01f5fec2400aa83f769ca Mon Sep 17 00:00:00 2001 From: Konstantin Pschera Date: Fri, 1 Dec 2017 17:39:40 +0100 Subject: [PATCH 1/5] Use realpath to match transformers This will use the realpath to match the transformers in case the path is a symlink. This will solve an issue where files which are linked by lerna or npm link are not transformed. Currently a solution for this problem would be a configuration like: ```json { "transformIgnorePatterns": [ "/node_modules/" ] } ``` --- packages/jest-runtime/src/script_transformer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/jest-runtime/src/script_transformer.js b/packages/jest-runtime/src/script_transformer.js index 0c97ea28f935..805bb410fb99 100644 --- a/packages/jest-runtime/src/script_transformer.js +++ b/packages/jest-runtime/src/script_transformer.js @@ -183,11 +183,12 @@ export default class ScriptTransformer { } transformSource( - filename: Path, + filepath: Path, content: string, instrument: boolean, mapCoverage: boolean, ) { + const filename = fs.realpathSync(filepath) || filepath; const transform = this._getTransformer(filename); const cacheFilePath = this._getFileCachePath( filename, From 5057ab2503dda07923dee61d6e50b497fb5a9b9a Mon Sep 17 00:00:00 2001 From: Konstantin Pschera Date: Fri, 1 Dec 2017 17:47:41 +0100 Subject: [PATCH 2/5] Add Changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 968f63cea419..057484d2f3e5 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 From 85b3ef93045b59f9d77925a547d0fee0dc601deb Mon Sep 17 00:00:00 2001 From: Konstantin Pschera Date: Mon, 4 Dec 2017 10:40:54 +0100 Subject: [PATCH 3/5] Use native realpath --- packages/jest-runtime/src/script_transformer.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/jest-runtime/src/script_transformer.js b/packages/jest-runtime/src/script_transformer.js index 805bb410fb99..c126dc1f0bc0 100644 --- a/packages/jest-runtime/src/script_transformer.js +++ b/packages/jest-runtime/src/script_transformer.js @@ -19,6 +19,7 @@ import path from 'path'; import vm from 'vm'; import {createDirectory} from 'jest-util'; import fs from 'graceful-fs'; +import nodeFS from 'fs'; import {transform as babelTransform} from 'babel-core'; import babelPluginIstanbul from 'babel-plugin-istanbul'; import convertSourceMap from 'convert-source-map'; @@ -182,13 +183,22 @@ export default class ScriptTransformer { }).code; } + _getRealPath(filepath: Path): Path { + try { + // $FlowFixMe + return process.binding('fs').realpath(filepath) || filepath; + } catch (err) { + return filepath; + } + } + transformSource( filepath: Path, content: string, instrument: boolean, mapCoverage: boolean, ) { - const filename = fs.realpathSync(filepath) || filepath; + const filename = this._getRealPath(filepath); const transform = this._getTransformer(filename); const cacheFilePath = this._getFileCachePath( filename, From 87ed4b3c17fa5b1988a58a62a63e88369b885744 Mon Sep 17 00:00:00 2001 From: Konstantin Pschera Date: Mon, 4 Dec 2017 11:53:59 +0100 Subject: [PATCH 4/5] Add integration test --- .../__tests__/transform-linked-modules.test.js | 12 ++++++++++++ .../__tests__/linked-modules.test.js | 9 +++++++++ .../transform-linked-modules/ignored/normal.js | 1 + .../transform-linked-modules/ignored/symlink.js | 1 + .../transform-linked-modules/package.json | 13 +++++++++++++ .../transform-linked-modules/package/index.js | 1 + .../transform-linked-modules/preprocessor.js | 5 +++++ 7 files changed, 42 insertions(+) create mode 100644 integration_tests/__tests__/transform-linked-modules.test.js create mode 100644 integration_tests/transform-linked-modules/__tests__/linked-modules.test.js create mode 100644 integration_tests/transform-linked-modules/ignored/normal.js create mode 120000 integration_tests/transform-linked-modules/ignored/symlink.js create mode 100644 integration_tests/transform-linked-modules/package.json create mode 100644 integration_tests/transform-linked-modules/package/index.js create mode 100644 integration_tests/transform-linked-modules/preprocessor.js 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"'; + }, +}; From dd763088e6f2674397dabf9e1a8dc41e4ce89165 Mon Sep 17 00:00:00 2001 From: Konstantin Pschera Date: Mon, 4 Dec 2017 11:55:43 +0100 Subject: [PATCH 5/5] Remove unneeded import --- packages/jest-runtime/src/script_transformer.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/jest-runtime/src/script_transformer.js b/packages/jest-runtime/src/script_transformer.js index c126dc1f0bc0..cd7e29aacdeb 100644 --- a/packages/jest-runtime/src/script_transformer.js +++ b/packages/jest-runtime/src/script_transformer.js @@ -19,7 +19,6 @@ import path from 'path'; import vm from 'vm'; import {createDirectory} from 'jest-util'; import fs from 'graceful-fs'; -import nodeFS from 'fs'; import {transform as babelTransform} from 'babel-core'; import babelPluginIstanbul from 'babel-plugin-istanbul'; import convertSourceMap from 'convert-source-map';