Skip to content

Commit a188b9c

Browse files
Refactored the mocking logic to use named functions with comments. Also added descriptions to the "beforeEach" and "afterEach" hooks.
1 parent 6c78ceb commit a188b9c

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

test/specs/absolute-root/absolute-root.spec.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,37 @@ const dereferencedSchema = require("./dereferenced");
1010
const bundledSchema = require("./bundled");
1111

1212
describe("When executed in the context of root directory", () => {
13-
const { cwd } = url;
14-
const { cwd: processCwd } = process;
13+
// Store references to the original methods
14+
const originalProcessCwd = process.cwd;
15+
const originalUrlCwd = url.cwd;
1516

16-
beforeEach(() => {
17-
url.cwd = function () {
18-
try {
19-
process.cwd = () => "/";
20-
return cwd.apply(null, arguments);
21-
}
22-
finally {
23-
process.cwd = processCwd;
24-
}
25-
};
17+
/**
18+
* A mock `process.cwd()` implementation that always returns the root diretory
19+
*/
20+
function mockProcessCwd () {
21+
return "/";
22+
}
23+
24+
/**
25+
* Temporarily mocks `process.cwd()` while calling the real `url.cwd()` implemenation
26+
*/
27+
function mockUrlCwd () {
28+
try {
29+
process.cwd = mockProcessCwd;
30+
return originalUrlCwd.apply(null, arguments);
31+
}
32+
finally {
33+
process.cwd = originalProcessCwd;
34+
}
35+
}
36+
37+
beforeEach("Mock process.cwd and url.cwd", () => {
38+
url.cwd = mockUrlCwd;
2639
});
2740

28-
afterEach(() => {
29-
url.cwd = cwd;
30-
process.cwd = processCwd; // already restored at line 19, but just in case
41+
afterEach("Restore process.cwd and url.cwd", () => {
42+
url.cwd = originalUrlCwd;
43+
process.cwd = originalProcessCwd; // already restored by the finally block above, but just in case
3144
});
3245

3346

0 commit comments

Comments
 (0)