Skip to content

Commit

Permalink
fix script_transformer test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Lauliac committed Jul 20, 2017
1 parent eb5224b commit bef5880
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
39 changes: 28 additions & 11 deletions packages/jest-runtime/src/__tests__/script_transformer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
*/
'use strict';

const crypto = require('crypto');
const slash = require('slash');

jest
.mock('fs')
.mock('graceful-fs')
.mock('jest-haste-map', () => ({
getCacheFilePath: (cacheDir, baseDir, version) => cacheDir + baseDir,
Expand Down Expand Up @@ -100,6 +102,14 @@ let fs;
let mockFs;
let object;
let vm;
let writeFileAtomic;

jest.mock('write-file-atomic', () => ({
sync: jest.fn().mockImplementation((filePath, data) => {
const normalizedPath = require('slash')(filePath);
mockFs[normalizedPath] = data;
}),
}));

describe('ScriptTransformer', () => {
const reset = () => {
Expand Down Expand Up @@ -145,6 +155,8 @@ describe('ScriptTransformer', () => {

fs.existsSync = jest.fn(path => !!mockFs[path]);

writeFileAtomic = require('write-file-atomic');

config = {
cache: true,
cacheDirectory: '/cache/',
Expand Down Expand Up @@ -290,10 +302,13 @@ describe('ScriptTransformer', () => {
mapCoverage: true,
});
expect(result.sourceMapPath).toEqual(expect.any(String));
expect(fs.writeFileSync).toBeCalledWith(
const mapStr = JSON.stringify(map);
expect(
writeFileAtomic.sync,
).toBeCalledWith(
result.sourceMapPath,
JSON.stringify(map),
'utf8',
crypto.createHash('md5').update(mapStr).digest('hex') + mapStr,
{encoding: 'utf8'},
);
});

Expand All @@ -320,10 +335,12 @@ describe('ScriptTransformer', () => {
mapCoverage: true,
});
expect(result.sourceMapPath).toEqual(expect.any(String));
expect(fs.writeFileSync).toBeCalledWith(
expect(
writeFileAtomic.sync,
).toBeCalledWith(
result.sourceMapPath,
sourceMap,
'utf8',
crypto.createHash('md5').update(sourceMap).digest('hex') + sourceMap,
{encoding: 'utf8'},
);
});

Expand All @@ -348,7 +365,7 @@ describe('ScriptTransformer', () => {
mapCoverage: false,
});
expect(result.sourceMapPath).toBeFalsy();
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
expect(writeFileAtomic.sync).toHaveBeenCalledTimes(1);
});

it('reads values from the cache', () => {
Expand All @@ -359,8 +376,8 @@ describe('ScriptTransformer', () => {
scriptTransformer.transform('/fruits/banana.js', {});

const cachePath = getCachePath(mockFs, config);
expect(fs.writeFileSync).toBeCalled();
expect(fs.writeFileSync.mock.calls[0][0]).toBe(cachePath);
expect(writeFileAtomic.sync).toBeCalled();
expect(writeFileAtomic.sync.mock.calls[0][0]).toBe(cachePath);

// Cache the state in `mockFsCopy`
const mockFsCopy = mockFs;
Expand All @@ -375,7 +392,7 @@ describe('ScriptTransformer', () => {
expect(fs.readFileSync.mock.calls.length).toBe(2);
expect(fs.readFileSync).toBeCalledWith('/fruits/banana.js', 'utf8');
expect(fs.readFileSync).toBeCalledWith(cachePath, 'utf8');
expect(fs.writeFileSync).not.toBeCalled();
expect(writeFileAtomic.sync).not.toBeCalled();

// Don't read from the cache when `config.cache` is false.
jest.resetModuleRegistry();
Expand All @@ -388,6 +405,6 @@ describe('ScriptTransformer', () => {
expect(fs.readFileSync.mock.calls.length).toBe(1);
expect(fs.readFileSync).toBeCalledWith('/fruits/banana.js', 'utf8');
expect(fs.readFileSync).not.toBeCalledWith(cachePath, 'utf8');
expect(fs.writeFileSync).toBeCalled();
expect(writeFileAtomic.sync).toBeCalled();
});
});
11 changes: 9 additions & 2 deletions packages/jest-runtime/src/script_transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ class ScriptTransformer {
: null;

if (code) {
// This is broken: we return the code, and a path for the source map
// directly from the cache. But, nothing ensures the source map actually
// matches that source code. They could have gotten out-of-sync in case
// two separate processes write concurrently to the same cache files.
return {
code,
sourceMapPath,
Expand Down Expand Up @@ -385,8 +389,11 @@ const readCacheFile = (filename: Path, cachePath: Path): ?string => {
return null;
}
} catch (e) {
e.message = 'jest: failed to read cache file: ' + cachePath +
'\nFailure message: ' + e.message;
e.message =
'jest: failed to read cache file: ' +
cachePath +
'\nFailure message: ' +
e.message;
removeFile(cachePath);
throw e;
}
Expand Down

0 comments on commit bef5880

Please sign in to comment.