Skip to content

Commit

Permalink
Use realpath to match transformers (#5000)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
k15a authored and cpojer committed Dec 4, 2017
1 parent 6d0c0f0 commit b0b9387
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions integration_tests/__tests__/transform-linked-modules.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
Original file line number Diff line number Diff line change
@@ -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');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'ignored/normal';
13 changes: 13 additions & 0 deletions integration_tests/transform-linked-modules/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"jest": {
"testEnvironment": "node",
"transformIgnorePatterns": [
"/node_modules/",
"<rootDir>/__tests__",
"<rootDir>/ignored/"
],
"transform": {
"^.+\\.js$": "<rootDir>/preprocessor.js"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'package/index';
5 changes: 5 additions & 0 deletions integration_tests/transform-linked-modules/preprocessor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
process() {
return 'module.exports = "transformed"';
},
};
12 changes: 11 additions & 1 deletion packages/jest-runtime/src/script_transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b0b9387

Please sign in to comment.