Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ All absolute paths will now be converted and saved in snapshots like so:

`/path/to/user-home/nested/home` => `<HOME_DIR>/nested/home`

`/path/to/my-proj/node_modules/some-package/` => `<NODE_MODULES>/some-package/`

#### Caveats

* All single backslashes (`\`) will be replaced by a forward slash (`/`).
Expand Down
9 changes: 9 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ function normalizePaths(value) {
const homeRealRelativeToTemp = path.relative(tempDir, homeDirReal);

const runner = [
// replace node_modules with NODE_MODULES
val => {
if (/node_modules/.test(val) === false) {
return val;
}

return `<NODE_MODULES>${val.split("node_modules").pop()}`;
},

// Replace process.cwd with <PROJECT_ROOT>
val => val.split(cwdReal).join("<PROJECT_ROOT>"),
val => val.split(cwd).join("<PROJECT_ROOT>"),
Expand Down
17 changes: 17 additions & 0 deletions lib/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,23 @@ describe("normalizePaths", () => {
expect(normalized).toEqual("<HOME_DIR>/some/nested/dir");
});

it("replaces node_modules with <NODE_MODULES>", () => {
const value = path.resolve(process.cwd(), "node_modules/some/nested/dir");

const normalized = normalizePaths(value);
expect(normalized).toEqual("<NODE_MODULES>/some/nested/dir");
});

it("replaces node_modules nested inside node_modules with single <NODE_MODULES>", () => {
const value = path.resolve(
process.cwd(),
"node_modules/one/node_modules/some/nested/dir"
);

const normalized = normalizePaths(value);
expect(normalized).toEqual("<NODE_MODULES>/some/nested/dir");
});

describe("symlinks", () => {
const tempDir = path.resolve(os.tmpdir(), "jest-serializer-path");

Expand Down