Skip to content

Commit

Permalink
chore: migrate babel-jest to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 11, 2019
1 parent 780c838 commit 8aa7b61
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -25,6 +25,7 @@
- `[jest-watcher]`: Migrate to TypeScript ([#7843](https://github.com/facebook/jest/pull/7843))
- `[jest-mock]`: Migrate to TypeScript ([#7847](https://github.com/facebook/jest/pull/7847), [#7850](https://github.com/facebook/jest/pull/7850))
- `[jest-worker]`: Migrate to TypeScript ([#7853](https://github.com/facebook/jest/pull/7853))
- `[babel-jest]`: Migrate to TypeScript ([#7862](https://github.com/facebook/jest/pull/7862))

### Performance

Expand Down
2 changes: 2 additions & 0 deletions packages/babel-jest/package.json
Expand Up @@ -9,7 +9,9 @@
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@jest/types": "^24.1.0",
"babel-plugin-istanbul": "^5.1.0",
"babel-preset-jest": "^24.1.0",
"chalk": "^2.4.2",
Expand Down
Expand Up @@ -4,7 +4,9 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const babelJest = require('../index');

import {Config, Transform} from '@jest/types';
import babelJest from '../index';

//Mock data for all the tests
const sourceString = `
Expand All @@ -24,12 +26,16 @@ const mockConfig = {
};

test(`Returns source string with inline maps when no transformOptions is passed`, () => {
const result = babelJest.process(sourceString, 'dummy_path.js', mockConfig);
const result = babelJest.process(
sourceString,
'dummy_path.js',
(mockConfig as unknown) as Config.ProjectConfig,
) as Transform.TransformedSource;
expect(typeof result).toBe('object');
expect(result.code).toBeDefined();
expect(result.map).toBeDefined();
expect(result.code).toMatch('//# sourceMappingURL');
expect(result.code).toMatch('customMultiply');
expect(result.map.sources).toEqual(['dummy_path.js']);
expect(JSON.stringify(result.map.sourcesContent)).toMatch('customMultiply');
expect(result.map!.sources).toEqual(['dummy_path.js']);
expect(JSON.stringify(result.map!.sourcesContent)).toMatch('customMultiply');
});
Expand Up @@ -3,32 +3,31 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {Path, ProjectConfig} from 'types/Config';
import type {
CacheKeyOptions,
Transformer,
TransformOptions,
TransformedSource,
} from 'types/Transform';

import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
import {transformSync as babelTransform, loadPartialConfig} from '@babel/core';
import {Config, Transform} from '@jest/types';
import {
transformSync as babelTransform,
loadPartialConfig,
TransformOptions,
PartialConfig,
} from '@babel/core';
import chalk from 'chalk';
import slash from 'slash';

const THIS_FILE = fs.readFileSync(__filename);
const jestPresetPath = require.resolve('babel-preset-jest');
const babelIstanbulPlugin = require.resolve('babel-plugin-istanbul');

const createTransformer = (options: any): Transformer => {
const createTransformer = (
options: TransformOptions = {},
): Transform.Transformer => {
options = {
...options,
// @ts-ignore: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/32955
caller: {
name: 'babel-jest',
supportsStaticESM: false,
Expand All @@ -39,10 +38,13 @@ const createTransformer = (options: any): Transformer => {
sourceMaps: 'both',
};

// @ts-ignore: seems like this is removed. Is that true?
delete options.cacheDirectory;
delete options.filename;

function loadBabelConfig(cwd, filename) {
function loadBabelConfig(
cwd: Config.Path,
filename: Config.Path,
): PartialConfig {
// `cwd` first to allow incoming options to override it
const babelConfig = loadPartialConfig({cwd, ...options, filename});

Expand All @@ -63,9 +65,13 @@ const createTransformer = (options: any): Transformer => {
canInstrument: true,
getCacheKey(
fileData: string,
filename: Path,
filename: Config.Path,
configString: string,
{config, instrument, rootDir}: {config: ProjectConfig} & CacheKeyOptions,
{
config,
instrument,
rootDir,
}: {config: Config.ProjectConfig} & Transform.CacheKeyOptions,
): string {
const babelOptions = loadBabelConfig(config.cwd, filename);
const configPath = [
Expand Down Expand Up @@ -96,16 +102,16 @@ const createTransformer = (options: any): Transformer => {
},
process(
src: string,
filename: Path,
config: ProjectConfig,
transformOptions?: TransformOptions,
): string | TransformedSource {
filename: Config.Path,
config: Config.ProjectConfig,
transformOptions?: Transform.TransformOptions,
): string | Transform.TransformedSource {
const babelOptions = {...loadBabelConfig(config.cwd, filename).options};

if (transformOptions && transformOptions.instrument) {
babelOptions.auxiliaryCommentBefore = ' istanbul ignore next ';
// Copied from jest-runtime transform.js
babelOptions.plugins = babelOptions.plugins.concat([
babelOptions.plugins = (babelOptions.plugins || []).concat([
[
babelIstanbulPlugin,
{
Expand All @@ -119,10 +125,21 @@ const createTransformer = (options: any): Transformer => {

const transformResult = babelTransform(src, babelOptions);

return transformResult || src;
if (transformResult && typeof transformResult.code === 'string') {
// @ts-ignore: why doesn't TS understand this?
return transformResult;
}

return src;
},
};
};

module.exports = createTransformer();
(module.exports: any).createTransformer = createTransformer;
const transformer = createTransformer();

// FIXME: This is not part of the exported TS types. When fixed, remember to
// move @types/babel__core to `dependencies` instead of `devDependencies`
// (one fix is to use ESM, maybe for Jest 25?)
transformer.createTransformer = createTransformer;

export = transformer;
8 changes: 8 additions & 0 deletions packages/babel-jest/tsconfig.json
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
},
"references": [{"path": "../jest-types"}]
}
58 changes: 58 additions & 0 deletions packages/jest-types/src/Transform.ts
@@ -0,0 +1,58 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {Script} from 'vm';
import {Path, ProjectConfig} from './Config';

export type TransformedSource = {
code: string;
map?: // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
{
version: number;
sources: string[];
names: string[];
sourceRoot?: string;
sourcesContent?: string[];
mappings: string;
file: string;
} | null;
};

export type TransformResult = {
script: Script;
mapCoverage: boolean;
sourceMapPath?: string;
};

export type TransformOptions = {
instrument: boolean;
};

export type CacheKeyOptions = {
config: ProjectConfig;
instrument: boolean;
rootDir: string;
};

export type Transformer = {
canInstrument?: boolean;
createTransformer?: (options: any) => Transformer;

getCacheKey: (
fileData: string,
filePath: Path,
configStr: string,
options: CacheKeyOptions,
) => string;

process: (
sourceText: string,
sourcePath: Path,
config: ProjectConfig,
options?: TransformOptions,
) => string | TransformedSource;
};
3 changes: 2 additions & 1 deletion packages/jest-types/src/index.ts
Expand Up @@ -10,5 +10,6 @@ import * as Console from './Console';
import * as SourceMaps from './SourceMaps';
import * as TestResult from './TestResult';
import * as Mocks from './Mocks';
import * as Transform from './Transform';

export {Config, Console, SourceMaps, TestResult, Mocks};
export {Config, Console, SourceMaps, TestResult, Mocks, Transform};

0 comments on commit 8aa7b61

Please sign in to comment.