Skip to content

Commit a8f28fe

Browse files
Use which sync interface
1 parent c3997c2 commit a8f28fe

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

packages/jest-haste-map/src/crawlers/__tests__/node.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ jest.mock('fs', () => {
124124
};
125125
});
126126

127-
jest.mock('which', () => jest.fn().mockResolvedValue());
127+
jest.mock('which');
128128

129129
const pearMatcher = path => /pear/.test(path);
130130
const createMap = obj => new Map(Object.keys(obj).map(key => [key, obj[key]]));
@@ -296,7 +296,9 @@ describe('node crawler', () => {
296296
it('uses node fs APIs on Unix based OS without find binary', () => {
297297
process.platform = 'linux';
298298
const which = require('which');
299-
which.mockReturnValueOnce(Promise.reject());
299+
which.sync.mockImplementation(() => {
300+
throw new Error();
301+
});
300302

301303
nodeCrawl = require('../node');
302304

@@ -316,7 +318,7 @@ describe('node crawler', () => {
316318
}),
317319
);
318320
expect(removedFiles).toEqual(new Map());
319-
expect(which).toBeCalledWith('find');
321+
expect(which.sync).toBeCalledWith('find');
320322
});
321323
});
322324

packages/jest-haste-map/src/crawlers/node.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ type Result = Array<[/* id */ string, /* mtime */ number, /* size */ number]>;
2222

2323
type Callback = (result: Result) => void;
2424

25-
function hasNativeFindSupport(forceNodeFilesystemAPI: boolean): Promise<void> {
26-
if (forceNodeFilesystemAPI) return Promise.reject();
25+
function hasNativeFindSupport(forceNodeFilesystemAPI: boolean): boolean {
26+
if (forceNodeFilesystemAPI || process.platform === 'win32') {
27+
return false;
28+
}
2729

28-
return process.platform === 'win32'
29-
? Promise.reject()
30-
: ((which('find') as unknown) as Promise<void>);
30+
try {
31+
which.sync('find');
32+
return true;
33+
} catch {
34+
return false;
35+
}
3136
}
3237

3338
function find(
@@ -202,8 +207,10 @@ export = function nodeCrawl(
202207
});
203208
};
204209

205-
hasNativeFindSupport(forceNodeFilesystemAPI)
206-
.then(() => findNative(roots, extensions, ignore, callback))
207-
.catch(() => find(roots, extensions, ignore, callback));
210+
if (hasNativeFindSupport(forceNodeFilesystemAPI)) {
211+
findNative(roots, extensions, ignore, callback);
212+
} else {
213+
find(roots, extensions, ignore, callback);
214+
}
208215
});
209216
};

0 commit comments

Comments
 (0)