Skip to content
/ jest Public
forked from jestjs/jest

Commit

Permalink
--wip-- [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Oct 31, 2020
1 parent 3db82bc commit 31d67e2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 43 deletions.
6 changes: 2 additions & 4 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,10 @@ export default class ScriptTransformer {
filename: Config.Path,
options: Options,
transformOptions: TransformOptions,
fileSource?: string,
fileSource: string,
): TransformResult {
const {isCoreModule, isInternalModule} = options;
const content = stripShebang(
fileSource || fs.readFileSync(filename, 'utf8'),
);
const content = stripShebang(fileSource);

let code = content;
let sourceMapPath: string | null = null;
Expand Down
122 changes: 83 additions & 39 deletions packages/jest-transform/src/__tests__/script_transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ let mockFs: Record<Config.Path, string>;
let object: <T>(input: T) => T;
let writeFileAtomic: typeof import('write-file-atomic');

const filename = '/fruits/banana.js';

jest.mock('write-file-atomic', () => ({
sync: jest.fn().mockImplementation((filePath, data) => {
mockFs[filePath] = data;
Expand Down Expand Up @@ -228,20 +230,18 @@ describe('ScriptTransformer', () => {
it('transforms a file properly', () => {
const scriptTransformer = new ScriptTransformer(config);
const transformedBananaWithCoverage = scriptTransformer.transform(
'/fruits/banana.js',
filename,
getCoverageOptions({collectCoverage: true}),
mockFs[filename],
);

expect(wrap(transformedBananaWithCoverage.code)).toMatchSnapshot();

// no-cache case
expect(fs.readFileSync).toHaveBeenCalledTimes(1);
expect(fs.readFileSync).toBeCalledWith('/fruits/banana.js', 'utf8');

// in-memory cache
const transformedBananaWithCoverageAgain = scriptTransformer.transform(
'/fruits/banana.js',
filename,
getCoverageOptions({collectCoverage: true}),
mockFs[filename],
);
expect(transformedBananaWithCoverageAgain).toBe(
transformedBananaWithCoverage,
Expand All @@ -250,6 +250,7 @@ describe('ScriptTransformer', () => {
const transformedKiwiWithCoverage = scriptTransformer.transform(
'/fruits/kiwi.js',
getCoverageOptions({collectCoverage: true}),
mockFs['/fruits/kiwi.js'],
);
expect(wrap(transformedKiwiWithCoverage.code)).toMatchSnapshot();

Expand All @@ -262,6 +263,7 @@ describe('ScriptTransformer', () => {
const transformedKiwiWithoutCoverage = scriptTransformer.transform(
'/fruits/kiwi.js',
getCoverageOptions({collectCoverage: false}),
mockFs['/fruits/kiwi.js'],
);

expect(transformedKiwiWithoutCoverage.code).not.toEqual(
Expand Down Expand Up @@ -299,7 +301,7 @@ describe('ScriptTransformer', () => {
const scriptTransformer = new ScriptTransformer(config);

const incorrectReturnValues = [
[undefined, '/fruits/banana.js'],
[undefined, filename],
[{a: 'a'}, '/fruits/kiwi.js'],
[[], '/fruits/grapefruit.js'],
];
Expand All @@ -310,12 +312,16 @@ describe('ScriptTransformer', () => {
returnValue,
);
expect(() =>
scriptTransformer.transform(filePath, getCoverageOptions()),
scriptTransformer.transform(
filePath,
getCoverageOptions(),
mockFs[filename],
),
).toThrow('must return a string');
});

const correctReturnValues = [
['code', '/fruits/banana.js'],
['code', filename],
[{code: 'code'}, '/fruits/kiwi.js'],
];

Expand All @@ -325,7 +331,11 @@ describe('ScriptTransformer', () => {
returnValue,
);
expect(() =>
scriptTransformer.transform(filePath, getCoverageOptions()),
scriptTransformer.transform(
filePath,
getCoverageOptions(),
mockFs[filename],
),
).not.toThrow();
});
},
Expand All @@ -338,7 +348,7 @@ describe('ScriptTransformer', () => {
};
const scriptTransformer = new ScriptTransformer(config);
expect(() =>
scriptTransformer.transformSource('sample.js', '', false),
scriptTransformer.transformSource('sample.js', '', {instrument: false}),
).toThrow('Jest: a transform must export a `process` function.');
});

Expand All @@ -355,7 +365,7 @@ describe('ScriptTransformer', () => {
};
const scriptTransformer = new ScriptTransformer(config);
expect(() =>
scriptTransformer.transformSource('sample.js', '', false),
scriptTransformer.transformSource('sample.js', '', {instrument: false}),
).toThrow('Jest: a transform must export a `process` function.');
});

Expand All @@ -366,16 +376,17 @@ describe('ScriptTransformer', () => {
};
const scriptTransformer = new ScriptTransformer(config);
expect(() =>
scriptTransformer.transformSource('sample.js', '', false),
scriptTransformer.transformSource('sample.js', '', {instrument: false}),
).not.toThrow();
});

it('uses the supplied preprocessor', () => {
config = {...config, transform: [['\\.js$', 'test_preprocessor', {}]]};
const scriptTransformer = new ScriptTransformer(config);
const res1 = scriptTransformer.transform(
'/fruits/banana.js',
filename,
getCoverageOptions(),
mockFs[filename],
);

expect(require('test_preprocessor').getCacheKey).toBeCalled();
Expand All @@ -385,6 +396,7 @@ describe('ScriptTransformer', () => {
const res2 = scriptTransformer.transform(
'/node_modules/react.js',
getCoverageOptions(),
mockFs['/node_modules/react.js'],
);
// ignores preprocessor
expect(wrap(res2.code)).toMatchSnapshot();
Expand All @@ -401,12 +413,14 @@ describe('ScriptTransformer', () => {
const scriptTransformer = new ScriptTransformer(config);

const res1 = scriptTransformer.transform(
'/fruits/banana.js',
filename,
getCoverageOptions(),
mockFs[filename],
);
const res2 = scriptTransformer.transform(
'/styles/App.css',
getCoverageOptions(),
mockFs['/styles/App.css'],
);

expect(require('test_preprocessor').getCacheKey).toBeCalled();
Expand All @@ -417,6 +431,7 @@ describe('ScriptTransformer', () => {
const res3 = scriptTransformer.transform(
'/node_modules/react.js',
getCoverageOptions(),
mockFs['/node_modules/react.js'],
);
// ignores preprocessor
expect(wrap(res3.code)).toMatchSnapshot();
Expand All @@ -440,8 +455,9 @@ describe('ScriptTransformer', () => {
});

const result = scriptTransformer.transform(
'/fruits/banana.js',
filename,
getCoverageOptions(),
mockFs[filename],
);
expect(result.sourceMapPath).toEqual(expect.any(String));
const mapStr = JSON.stringify(map);
Expand Down Expand Up @@ -472,8 +488,9 @@ describe('ScriptTransformer', () => {
require('preprocessor-with-sourcemaps').process.mockReturnValue(content);

const result = scriptTransformer.transform(
'/fruits/banana.js',
filename,
getCoverageOptions(),
mockFs[filename],
);
expect(result.sourceMapPath).toEqual(expect.any(String));
expect(writeFileAtomic.sync).toBeCalledTimes(2);
Expand Down Expand Up @@ -508,8 +525,9 @@ describe('ScriptTransformer', () => {
require('preprocessor-with-sourcemaps').process.mockReturnValue(content);

const result = scriptTransformer.transform(
'/fruits/banana.js',
filename,
getCoverageOptions({collectCoverage: true}),
mockFs[filename],
);
expect(result.sourceMapPath).toBeNull();
expect(writeFileAtomic.sync).toBeCalledTimes(1);
Expand Down Expand Up @@ -537,8 +555,9 @@ describe('ScriptTransformer', () => {
});

const result = scriptTransformer.transform(
'/fruits/banana.js',
filename,
getCoverageOptions(),
mockFs[filename],
);
expect(result.sourceMapPath).toEqual(expect.any(String));
expect(writeFileAtomic.sync).toBeCalledTimes(2);
Expand All @@ -565,8 +584,9 @@ describe('ScriptTransformer', () => {
});

const result = scriptTransformer.transform(
'/fruits/banana.js',
filename,
getCoverageOptions({collectCoverage: true}),
mockFs[filename],
);
expect(result.sourceMapPath).toBeFalsy();
expect(writeFileAtomic.sync).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -602,8 +622,9 @@ describe('ScriptTransformer', () => {
});

const result = scriptTransformer.transform(
'/fruits/banana.js',
filename,
getCoverageOptions({collectCoverage: true}),
mockFs[filename],
);
expect(result.sourceMapPath).toEqual(expect.any(String));
expect(writeFileAtomic.sync).toBeCalledTimes(2);
Expand Down Expand Up @@ -638,8 +659,9 @@ describe('ScriptTransformer', () => {
});

const result = scriptTransformer.transform(
'/fruits/banana.js',
filename,
getCoverageOptions({collectCoverage: true}),
mockFs[filename],
);
expect(result.sourceMapPath).toEqual(expect.any(String));
expect(writeFileAtomic.sync).toBeCalledTimes(2);
Expand All @@ -658,8 +680,9 @@ describe('ScriptTransformer', () => {
const scriptTransformer = new ScriptTransformer(config);

scriptTransformer.transform(
'/fruits/banana.js',
filename,
getCoverageOptions({collectCoverage: true}),
mockFs[filename],
);

const {getCacheKey} = require('test_preprocessor');
Expand All @@ -673,7 +696,11 @@ describe('ScriptTransformer', () => {
});

const scriptTransformer = new ScriptTransformer(config);
scriptTransformer.transform('/fruits/banana.js', {});
scriptTransformer.transform(
filename,
getCoverageOptions(),
mockFs[filename],
);
expect(
require('configureable-preprocessor').createTransformer,
).toHaveBeenCalledWith(transformerConfig);
Expand All @@ -685,7 +712,11 @@ describe('ScriptTransformer', () => {
transform: [['\\.js$', 'test_preprocessor', {}]],
};
let scriptTransformer = new ScriptTransformer(transformConfig);
scriptTransformer.transform('/fruits/banana.js', getCoverageOptions());
scriptTransformer.transform(
filename,
getCoverageOptions(),
mockFs[filename],
);

const cachePath = getCachePath(mockFs, config);
expect(writeFileAtomic.sync).toBeCalled();
Expand All @@ -699,10 +730,13 @@ describe('ScriptTransformer', () => {
// Restore the cached fs
mockFs = mockFsCopy;
scriptTransformer = new ScriptTransformer(transformConfig);
scriptTransformer.transform('/fruits/banana.js', getCoverageOptions());
scriptTransformer.transform(
filename,
getCoverageOptions(),
mockFs[filename],
);

expect(fs.readFileSync).toHaveBeenCalledTimes(2);
expect(fs.readFileSync).toBeCalledWith('/fruits/banana.js', 'utf8');
expect(fs.readFileSync).toHaveBeenCalledTimes(1);
expect(fs.readFileSync).toBeCalledWith(cachePath, 'utf8');
expect(writeFileAtomic.sync).not.toBeCalled();

Expand All @@ -712,11 +746,13 @@ describe('ScriptTransformer', () => {
mockFs = mockFsCopy;
transformConfig.cache = false;
scriptTransformer = new ScriptTransformer(transformConfig);
scriptTransformer.transform('/fruits/banana.js', getCoverageOptions());
scriptTransformer.transform(
filename,
getCoverageOptions(),
mockFs[filename],
);

expect(fs.readFileSync).toHaveBeenCalledTimes(1);
expect(fs.readFileSync).toBeCalledWith('/fruits/banana.js', 'utf8');
expect(fs.readFileSync).not.toBeCalledWith(cachePath, 'utf8');
expect(fs.readFileSync).not.toHaveBeenCalled();
expect(writeFileAtomic.sync).toBeCalled();
});

Expand All @@ -729,6 +765,7 @@ describe('ScriptTransformer', () => {
scriptTransformer.transform(
'/fruits/banana:colon.js',
getCoverageOptions(),
mockFs['/fruits/banana:colon.js'],
);

const cachePath = getCachePath(mockFs, config);
Expand All @@ -743,7 +780,11 @@ describe('ScriptTransformer', () => {
// Restore the cached fs
mockFs = mockFsCopy;
scriptTransformer = new ScriptTransformer(transformConfig);
scriptTransformer.transform('/fruits/banana:colon.js', {});
scriptTransformer.transform(
'/fruits/banana:colon.js',
getCoverageOptions(),
mockFs['/fruits/banana:colon.js'],
);

expect(fs.readFileSync).toHaveBeenCalledTimes(2);
expect(fs.readFileSync).toBeCalledWith('/fruits/banana:colon.js', 'utf8');
Expand All @@ -757,20 +798,25 @@ describe('ScriptTransformer', () => {
transform: [['\\.js$', 'test_preprocessor', {}]],
});

scriptTransformer.transform('/fruits/banana.js', getCoverageOptions());
scriptTransformer.transform(
filename,
getCoverageOptions(),
mockFs[filename],
);

const anotherScriptTransformer = new ScriptTransformer({
...config,
transform: [['\\.js$', 'css-preprocessor', {}]],
});

anotherScriptTransformer.transform(
'/fruits/banana.js',
filename,
getCoverageOptions(),
mockFs[filename],
);

expect(fs.readFileSync).toHaveBeenCalledTimes(2);
expect(fs.readFileSync).toBeCalledWith('/fruits/banana.js', 'utf8');
expect(fs.readFileSync).toBeCalledWith(filename, 'utf8');
});

it('preload transformer when using `preloadTransformer`', () => {
Expand All @@ -781,9 +827,7 @@ describe('ScriptTransformer', () => {

expect(Array.from(scriptTransformer._transformCache.entries())).toEqual([]);

expect(
scriptTransformer.preloadTransformer('/fruits/banana.js'),
).toBeUndefined();
expect(scriptTransformer.preloadTransformer(filename)).toBeUndefined();

expect(Array.from(scriptTransformer._transformCache.entries())).toEqual([
['test_preprocessor', expect.any(Object)],
Expand Down

0 comments on commit 31d67e2

Please sign in to comment.