diff --git a/react-packager/src/node-haste/Cache/__tests__/Cache-test.js b/react-packager/src/node-haste/Cache/__tests__/Cache-test.js index 2c8b0f6f..c466e50f 100644 --- a/react-packager/src/node-haste/Cache/__tests__/Cache-test.js +++ b/react-packager/src/node-haste/Cache/__tests__/Cache-test.js @@ -153,6 +153,30 @@ describe('Cache', () => { ) ); }); + + it('does not cache rejections', () => { + fs.stat.mockImpl((file, callback) => { + callback(null, { + mtime: { + getTime: () => {}, + }, + }); + }); + + var cache = new Cache({ + cacheKey: 'cache', + }); + var loaderCb = () => Promise.reject('lol'); + + return cache + .get('/rootDir/someFile', 'field', loaderCb) + .catch(() => { + var shouldBeCalled = jest.fn(() => Promise.resolve()); + const assert = value => expect(shouldBeCalled).toBeCalled(); + return cache.get('/rootDir/someFile', 'field', shouldBeCalled) + .then(assert, assert); + }); + }); }); describe('loading cache from disk', () => { diff --git a/react-packager/src/node-haste/Cache/index.js b/react-packager/src/node-haste/Cache/index.js index 4a0349ab..c86544a2 100644 --- a/react-packager/src/node-haste/Cache/index.js +++ b/react-packager/src/node-haste/Cache/index.js @@ -87,7 +87,7 @@ class Cache { this._data[filepath].metadata = Object.create(null); } - record.data[field] = loaderPromise + const cachedPromise = record.data[field] = loaderPromise .then(data => Promise.all([ data, denodeify(fs.stat)(filepath), @@ -106,7 +106,9 @@ class Cache { return data; }); - return record.data[field]; + // don't cache rejected promises + cachedPromise.catch(error => delete record.data[field]); + return cachedPromise; } _persistCache() {