Skip to content

Commit 357a119

Browse files
SimenBcpojer
authored andcommitted
Use fs.realpathSync.native if available (#5031)
* Pass in encoding to `realpath` call Fixes #5030 * Use `fs.realpathSync.native` if it exists * update changelog
1 parent 158ecd3 commit 357a119

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
([#4494](https://github.com/facebook/jest/pull/4494))
1717
* `[jest-cli]` Throw if `maxWorkers` doesn't have a value
1818
([#4591](https://github.com/facebook/jest/pull/4591))
19+
* `[jest-cli]` Use `fs.realpathSync.native` if available
20+
([#5031](https://github.com/facebook/jest/pull/5031))
1921
* `[jest-config]` Fix `--passWithNoTests`
2022
([#4639](https://github.com/facebook/jest/pull/4639))
2123
* `[jest-config]` Support `rootDir` tag in testEnvironment

packages/jest-cli/src/cli/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
Console,
1616
clearLine,
1717
createDirectory,
18+
realpath,
1819
validateCLIOptions,
1920
} from 'jest-util';
2021
import {readConfig} from 'jest-config';
@@ -143,8 +144,7 @@ const getProjectListFromCLIArgs = (argv, project: ?Path) => {
143144

144145
if (!projects.length && process.platform === 'win32') {
145146
try {
146-
// $FlowFixMe
147-
projects.push(process.binding('fs').realpath(process.cwd()));
147+
projects.push(realpath(process.cwd()));
148148
} catch (err) {
149149
// do nothing, just catch error
150150
// process.binding('fs').realpath can throw, e.g. on mapped drives

packages/jest-runtime/src/script_transformer.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
import crypto from 'crypto';
1818
import path from 'path';
1919
import vm from 'vm';
20-
import {createDirectory} from 'jest-util';
20+
import {createDirectory, realpath} from 'jest-util';
2121
import fs from 'graceful-fs';
2222
import {transform as babelTransform} from 'babel-core';
2323
import babelPluginIstanbul from 'babel-plugin-istanbul';
@@ -184,8 +184,7 @@ export default class ScriptTransformer {
184184

185185
_getRealPath(filepath: Path): Path {
186186
try {
187-
// $FlowFixMe
188-
return process.binding('fs').realpath(filepath) || filepath;
187+
return realpath(filepath) || filepath;
189188
} catch (err) {
190189
return filepath;
191190
}

packages/jest-util/src/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
import mkdirp from 'mkdirp';
11+
import fs from 'fs';
1112

1213
import BufferedConsole from './buffered_console';
1314
import clearLine from './clear_line';
@@ -30,6 +31,15 @@ const createDirectory = (path: string) => {
3031
}
3132
};
3233

34+
const realpath = (filepath: string) => {
35+
if (typeof fs.realpathSync.native === 'function') {
36+
return fs.realpathSync.native(filepath);
37+
}
38+
39+
// $FlowFixMe: This is need for node@<9.2
40+
return process.binding('fs').realpath(filepath, 'utf8');
41+
};
42+
3343
module.exports = {
3444
BufferedConsole,
3545
Console,
@@ -40,6 +50,7 @@ module.exports = {
4050
formatTestResults,
4151
getConsoleOutput,
4252
installCommonGlobals,
53+
realpath,
4354
setGlobal,
4455
validateCLIOptions,
4556
};

0 commit comments

Comments
 (0)