Skip to content

Commit

Permalink
Fix TestSequencer to use proper cache object. (jestjs#3481)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer authored May 5, 2017
1 parent d80bbbf commit 492755b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/jest-cli/src/TestSequencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,24 @@ class TestSequencer {
_getCache(test: Test) {
const {context} = test;
if (!this._cache.has(context) && context.config.cache) {
try {
this._cache.set(
context,
JSON.parse(fs.readFileSync(this._getCachePath(context), 'utf8')),
);
} catch (e) {}
const cachePath = this._getCachePath(context);
if (fs.existsSync(cachePath)) {
try {
this._cache.set(
context,
JSON.parse(fs.readFileSync(cachePath, 'utf8')),
);
} catch (e) {}
}
}

let cache = this._cache.get(context);
if (!cache) {
cache = {};
this._cache.set(context, cache);
}

return this._cache.get(context) || {};
return cache;
}

// When running more tests than we have workers available, sort the tests
Expand Down
40 changes: 40 additions & 0 deletions packages/jest-cli/src/__tests__/TestSequencer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ beforeEach(() => {
sequencer = new TestSequencer();

fs.readFileSync = jest.fn(() => '{}');
fs.existsSync = () => true;
fs.statSync = jest.fn(filePath => ({size: filePath.length}));
fs.writeFileSync = jest.fn();
});
Expand Down Expand Up @@ -120,6 +121,45 @@ test('sorts based on failures, timing information and file size', () => {
]);
});

test('writes the cache based on results without existing cache', () => {
fs.readFileSync = jest.fn(() => {
throw new Error('File does not exist.');
});

const testPaths = ['/test-a.js', '/test-b.js', '/test-c.js'];
const tests = sequencer.sort(toTests(testPaths));
sequencer.cacheResults(tests, {
testResults: [
{
numFailingTests: 0,
perfStats: {end: 2, start: 1},
testFilePath: '/test-a.js',
},
{
numFailingTests: 0,
perfStats: {end: 0, start: 0},
skipped: true,
testFilePath: '/test-b.js',
},
{
numFailingTests: 1,
perfStats: {end: 4, start: 1},
testFilePath: '/test-c.js',
},
{
numFailingTests: 1,
perfStats: {end: 2, start: 1},
testFilePath: '/test-x.js',
},
],
});
const fileData = JSON.parse(fs.writeFileSync.mock.calls[0][1]);
expect(fileData).toEqual({
'/test-a.js': [SUCCESS, 1],
'/test-c.js': [FAIL, 3],
});
});

test('writes the cache based on the results', () => {
fs.readFileSync = jest.fn(() =>
JSON.stringify({
Expand Down

0 comments on commit 492755b

Please sign in to comment.