Skip to content

Commit b0b9387

Browse files
k15acpojer
authored andcommitted
Use realpath to match transformers (#5000)
* 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": [ "<rootDir>/node_modules/" ] } ``` * Add Changelog * Use native realpath * Add integration test * Remove unneeded import
1 parent 6d0c0f0 commit b0b9387

File tree

9 files changed

+55
-1
lines changed

9 files changed

+55
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
issue. ([#4669](https://github.com/facebook/jest/pull/4669))
4949
* `[jest-cli]` Fix `--onlyChanged` path case sensitivity on Windows platform
5050
([#4730](https://github.com/facebook/jest/pull/4730))
51+
* `[jest-runtime]` Use realpath to match transformers
52+
([#5000](https://github.com/facebook/jest/pull/5000))
5153

5254
### Features
5355

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @flow
2+
3+
'use strict';
4+
5+
const runJest = require('../runJest');
6+
7+
it('should transform linked modules', () => {
8+
const result = runJest.json('transform-linked-modules', ['--no-cache']).json;
9+
10+
expect(result.success).toBe(true);
11+
expect(result.numTotalTests).toBe(2);
12+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
test('normal file', () => {
2+
const normal = require('../ignored/normal');
3+
expect(normal).toEqual('ignored/normal');
4+
});
5+
6+
test('symlink', () => {
7+
const symlink = require('../ignored/symlink');
8+
expect(symlink).toEqual('transformed');
9+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'ignored/normal';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../package/index.js
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "node",
4+
"transformIgnorePatterns": [
5+
"/node_modules/",
6+
"<rootDir>/__tests__",
7+
"<rootDir>/ignored/"
8+
],
9+
"transform": {
10+
"^.+\\.js$": "<rootDir>/preprocessor.js"
11+
}
12+
}
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'package/index';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
process() {
3+
return 'module.exports = "transformed"';
4+
},
5+
};

packages/jest-runtime/src/script_transformer.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,22 @@ export default class ScriptTransformer {
182182
}).code;
183183
}
184184

185+
_getRealPath(filepath: Path): Path {
186+
try {
187+
// $FlowFixMe
188+
return process.binding('fs').realpath(filepath) || filepath;
189+
} catch (err) {
190+
return filepath;
191+
}
192+
}
193+
185194
transformSource(
186-
filename: Path,
195+
filepath: Path,
187196
content: string,
188197
instrument: boolean,
189198
mapCoverage: boolean,
190199
) {
200+
const filename = this._getRealPath(filepath);
191201
const transform = this._getTransformer(filename);
192202
const cacheFilePath = this._getFileCachePath(
193203
filename,

0 commit comments

Comments
 (0)