@@ -10,24 +10,37 @@ const dereferencedSchema = require("./dereferenced");
10
10
const bundledSchema = require ( "./bundled" ) ;
11
11
12
12
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 ;
15
16
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 ;
26
39
} ) ;
27
40
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
31
44
} ) ;
32
45
33
46
0 commit comments