Skip to content

Commit 5a3bc14

Browse files
committed
simplify Jest snapshotPathNormalizer.ts
1 parent e5ab150 commit 5a3bc14

File tree

1 file changed

+7
-36
lines changed

1 file changed

+7
-36
lines changed

jest/snapshotPathNormalizer.ts

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import os from 'os';
1212
import path from 'path';
13-
import fs from 'fs';
1413
import _ from 'lodash';
1514
import {escapePath} from '@docusaurus/utils';
1615
import {version} from '@docusaurus/core/package.json';
@@ -65,48 +64,34 @@ function normalizePaths<T>(value: T): T {
6564
}
6665

6766
const cwd = process.cwd();
68-
const cwdReal = getRealPath(cwd);
6967
const tempDir = os.tmpdir();
70-
const tempDirReal = getRealPath(tempDir);
7168
const homeDir = os.homedir();
72-
const homeDirReal = getRealPath(homeDir);
7369

7470
const homeRelativeToTemp = path.relative(tempDir, homeDir);
75-
const homeRelativeToTempReal = path.relative(tempDirReal, homeDir);
76-
const homeRealRelativeToTempReal = path.relative(tempDirReal, homeDirReal);
77-
const homeRealRelativeToTemp = path.relative(tempDir, homeDirReal);
7871

7972
const runner: ((val: string) => string)[] = [
73+
// Remove win32 drive letters, C:\ -> \
74+
(val) => val.replace(/[a-zA-Z]:\\/g, '\\'),
75+
76+
// Convert win32 backslash's to forward slashes, \ -> /;
77+
// ignore some that look like escape sequences.
78+
(val) => val.replace(/\\(?!")/g, '/'),
79+
8080
(val) => (val.includes('keepAnsi') ? val : stripAnsi(val)),
8181
// Replace process.cwd with <PROJECT_ROOT>
82-
(val) => val.split(cwdReal).join('<PROJECT_ROOT>'),
8382
(val) => val.split(cwd).join('<PROJECT_ROOT>'),
8483

8584
// Replace temp directory with <TEMP_DIR>
86-
(val) => val.split(tempDirReal).join('<TEMP_DIR>'),
8785
(val) => val.split(tempDir).join('<TEMP_DIR>'),
8886

8987
// Replace home directory with <HOME_DIR>
90-
(val) => val.split(homeDirReal).join('<HOME_DIR>'),
9188
(val) => val.split(homeDir).join('<HOME_DIR>'),
9289

9390
// Handle HOME_DIR nested inside TEMP_DIR
9491
(val) =>
9592
val
9693
.split(`<TEMP_DIR>${path.sep + homeRelativeToTemp}`)
9794
.join('<HOME_DIR>'),
98-
(val) =>
99-
val
100-
.split(`<TEMP_DIR>${path.sep + homeRelativeToTempReal}`)
101-
.join('<HOME_DIR>'),
102-
(val) =>
103-
val
104-
.split(`<TEMP_DIR>${path.sep + homeRealRelativeToTempReal}`)
105-
.join('<HOME_DIR>'),
106-
(val) =>
107-
val
108-
.split(`<TEMP_DIR>${path.sep + homeRealRelativeToTemp}`)
109-
.join('<HOME_DIR>'),
11095

11196
// Replace the Docusaurus version with a stub
11297
(val) => val.split(version).join('<CURRENT_VERSION>'),
@@ -119,10 +104,6 @@ function normalizePaths<T>(value: T): T {
119104

120105
// Remove duplicate backslashes created from escapePath
121106
(val) => val.replace(/\\\\/g, '\\'),
122-
123-
// Convert win32 backslash's to forward slashes, \ -> /;
124-
// ignore some that look like escape sequences.
125-
(val) => val.replace(/\\(?!")/g, '/'),
126107
];
127108

128109
let result = value as string;
@@ -136,13 +117,3 @@ function normalizePaths<T>(value: T): T {
136117
function shouldUpdate(value: unknown) {
137118
return typeof value === 'string' && normalizePaths(value) !== value;
138119
}
139-
140-
function getRealPath(pathname: string) {
141-
try {
142-
// eslint-disable-next-line no-restricted-properties
143-
const realPath = fs.realpathSync(pathname);
144-
return realPath;
145-
} catch (err) {
146-
return pathname;
147-
}
148-
}

0 commit comments

Comments
 (0)