From d235839da874ba5769e8ac19aba21ccb38df661f Mon Sep 17 00:00:00 2001 From: Emilien MURATON Date: Thu, 30 Aug 2018 13:59:03 +0100 Subject: [PATCH] fix: stop search if files names are undefined (#6761) --- CHANGELOG.md | 1 + .../src/crawlers/__tests__/node.test.js | 18 ++++++++++++++++++ packages/jest-haste-map/src/crawlers/node.js | 5 ++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f55fe489469..2271ce48f605 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - `[jest-each]` Prevent done callback being supplied to describe ([#6843](https://github.com/facebook/jest/pull/6843)) - `[jest-config]` Better error message for a case when a preset module was found, but no `jest-preset.js` or `jest-preset.json` at the root ([#6863](https://github.com/facebook/jest/pull/6863)) - `[jest-mock]` Fix inheritance of static properties and methods in mocks ([#6921](https://github.com/facebook/jest/pull/6921)) +- `[jest-haste-map]` Catch crawler error when unsuccessfully reading directories ([#6761](https://github.com/facebook/jest/pull/6761)) ### Chore & Maintenance diff --git a/packages/jest-haste-map/src/crawlers/__tests__/node.test.js b/packages/jest-haste-map/src/crawlers/__tests__/node.test.js index 64362a1d0515..599075bb3269 100644 --- a/packages/jest-haste-map/src/crawlers/__tests__/node.test.js +++ b/packages/jest-haste-map/src/crawlers/__tests__/node.test.js @@ -59,6 +59,8 @@ jest.mock('fs', () => { setTimeout(() => callback(null, ['directory', 'tomato.js']), 0); } else if (dir === '/fruits/directory') { setTimeout(() => callback(null, ['strawberry.js']), 0); + } else if (dir == '/error') { + setTimeout(() => callback({code: 'ENOTDIR'}, undefined), 0); } }), stat: jest.fn(stat), @@ -217,4 +219,20 @@ describe('node crawler', () => { expect(data.files).toEqual({}); }); }); + + it('completes with fs.readdir throwing an error', () => { + process.platform = 'win32'; + + nodeCrawl = require('../node'); + + const files = Object.create(null); + return nodeCrawl({ + data: {files}, + extensions: ['js'], + ignore: pearMatcher, + roots: ['/error'], + }).then(data => { + expect(data.files).toEqual({}); + }); + }); }); diff --git a/packages/jest-haste-map/src/crawlers/node.js b/packages/jest-haste-map/src/crawlers/node.js index 17ce3452e757..96ce13d6e076 100644 --- a/packages/jest-haste-map/src/crawlers/node.js +++ b/packages/jest-haste-map/src/crawlers/node.js @@ -30,7 +30,10 @@ function find( activeCalls++; fs.readdir(directory, (err, names) => { activeCalls--; - + if (err) { + callback(result); + return; + } names.forEach(file => { file = path.join(directory, file); if (ignore(file)) {