From 63fa8c2772b155e2379c3bafa135ff7427e0d0da Mon Sep 17 00:00:00 2001 From: Igor Chulinda Date: Mon, 5 Dec 2016 14:37:59 +0100 Subject: [PATCH 1/2] Simple implementation and tests for #2191 --- .../src/__tests__/normalize-test.js | 96 +++++++++++++++++++ packages/jest-config/src/constants.js | 1 + packages/jest-config/src/normalize.js | 40 +++++++- 3 files changed, 135 insertions(+), 2 deletions(-) diff --git a/packages/jest-config/src/__tests__/normalize-test.js b/packages/jest-config/src/__tests__/normalize-test.js index f629caa8ac60..5127417a6a81 100644 --- a/packages/jest-config/src/__tests__/normalize-test.js +++ b/packages/jest-config/src/__tests__/normalize-test.js @@ -15,6 +15,7 @@ const utils = require('jest-util'); const normalize = require('../normalize'); const DEFAULT_JS_PATTERN = require('../constants').DEFAULT_JS_PATTERN; +const DEFAULT_TS_PATTERN = require('../constants').DEFAULT_TS_PATTERN; const DEFAULT_CSS_PATTERN = '^.+\\.(css)$'; describe('normalize', () => { @@ -614,6 +615,101 @@ describe('normalize', () => { }); }); + describe('ts-jest', () => { + let Resolver; + beforeEach(() => { + Resolver = require('jest-resolve'); + Resolver.findNodeModule = jest.fn( + name => 'node_modules' + path.sep + name, + ); + }); + + it('correctly identifies and uses ts-jest', () => { + const config = normalize({ + rootDir: '/root', + }); + const tsTransformerPath = uniformPath(config.transform[1][1]); + const testResultsProcessorPath = uniformPath(config.testResultsProcessor); + expect(config.transform[1][0]).toBe(DEFAULT_TS_PATTERN); + expect(tsTransformerPath) + .toEqual('/root/node_modules/ts-jest/preprocessor.js'); + expect(testResultsProcessorPath) + .toEqual('/root/node_modules/ts-jest/coverageprocessor.js'); + expect(config.moduleFileExtensions) + .toEqual(['js', 'jsx', 'json', 'ts', 'tsx']); + }); + + it('uses ts-jest if ts-jest is explicitly specified in a custom transform config', () => { + const customTSPattern = '^.+\\.ts$'; + const ROOT_DIR = '' + path.sep; + const config = normalize({ + rootDir: '/root', + transform: { + [customTSPattern]: (ROOT_DIR + Resolver.findNodeModule( + 'ts-jest', + ) + path.sep + 'preprocessor.js'), + }, + }); + const tsTransformerPath = uniformPath(config.transform[0][1]); + const testResultsProcessorPath = uniformPath(config.testResultsProcessor); + expect(config.transform[0][0]).toBe(customTSPattern); + expect(tsTransformerPath) + .toEqual('/root/node_modules/ts-jest/preprocessor.js'); + expect(testResultsProcessorPath) + .toEqual('/root/node_modules/ts-jest/coverageprocessor.js'); + expect(config.moduleFileExtensions) + .toEqual(['js', 'jsx', 'json', 'ts', 'tsx']); + }); + + it(`doesn't use ts-jest if its not available`, () => { + Resolver.findNodeModule.mockImplementation(() => null); + + const config = normalize({ + rootDir: '/root', + }); + + expect(config.transform).toEqual(undefined); + expect(config.testResultsProcessor).toEqual(undefined); + expect(config.moduleFileExtensions) + .toEqual(['js', 'json', 'jsx', 'node']); + }); + + it(`doesn't use ts-jest coverage proccessor if another is defined`, () => { + const ROOT_DIR = '' + path.sep; + const config = normalize({ + rootDir: '/root', + testResultsProcessor: ROOT_DIR + 'anotherProcessor.js', + }); + const tsTransformerPath = uniformPath(config.transform[1][1]); + const testResultsProcessorPath = uniformPath(config.testResultsProcessor); + expect(config.transform[1][0]).toBe(DEFAULT_TS_PATTERN); + expect(tsTransformerPath) + .toEqual('/root/node_modules/ts-jest/preprocessor.js'); + expect(testResultsProcessorPath).not + .toEqual('/root/node_modules/ts-jest/coverageprocessor.js'); + expect(config.moduleFileExtensions) + .toEqual(['js', 'jsx', 'json', 'ts', 'tsx']); + }); + + it(`doesn't use ts module extensions nor defalut if another is defined`, () => { + const config = normalize({ + moduleFileExtensions: ['js', 'ts'], + rootDir: '/root', + }); + const tsTransformerPath = uniformPath(config.transform[1][1]); + const testResultsProcessorPath = uniformPath(config.testResultsProcessor); + expect(config.transform[1][0]).toBe(DEFAULT_TS_PATTERN); + expect(tsTransformerPath) + .toEqual('/root/node_modules/ts-jest/preprocessor.js'); + expect(testResultsProcessorPath) + .toEqual('/root/node_modules/ts-jest/coverageprocessor.js'); + expect(config.moduleFileExtensions).not + .toEqual(['js', 'json', 'jsx', 'node']); + expect(config.moduleFileExtensions).not + .toEqual(['js', 'jsx', 'json', 'ts', 'tsx']); + }); + }); + describe('Upgrade help', () => { let consoleWarn; diff --git a/packages/jest-config/src/constants.js b/packages/jest-config/src/constants.js index ff4cfa3efd2d..380b00b2a735 100644 --- a/packages/jest-config/src/constants.js +++ b/packages/jest-config/src/constants.js @@ -12,3 +12,4 @@ const path = require('path'); exports.NODE_MODULES = path.sep + 'node_modules' + path.sep; exports.DEFAULT_JS_PATTERN = '^.+\\.jsx?$'; +exports.DEFAULT_TS_PATTERN = '^.+\\.tsx?$'; diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index cd6556a4110b..4e28668bbaa9 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -244,6 +244,7 @@ function normalize(config, argv) { } let babelJest; + let tsJest; if (config.transform) { const customJSPattern = Object.keys(config.transform).find(regex => { const pattern = new RegExp(regex); @@ -260,14 +261,40 @@ function normalize(config, argv) { babelJest = jsTransformer; } } + + const customTSPattern = Object.keys(config.transform).find(regex => { + const pattern = new RegExp(regex); + return pattern.test('foobar.ts') || pattern.test('foobar.tsx'); + }); + + if (customTSPattern) { + const tsTransformer = config.transform[customTSPattern]; + if ( + tsTransformer.includes( + constants.NODE_MODULES + 'ts-jest' + ) + ) { + tsJest = Resolver.findNodeModule('ts-jest', { + basedir: config.rootDir, + }); + } + } } else { babelJest = Resolver.findNodeModule('babel-jest', { basedir: config.rootDir, }); + tsJest = Resolver.findNodeModule('ts-jest', { + basedir: config.rootDir, + }); if (babelJest) { - config.transform = { + config.transform = Object.assign({}, config.transform, { [constants.DEFAULT_JS_PATTERN]: babelJest, - }; + }); + } + if (tsJest) { + config.transform = Object.assign({}, config.transform, { + [constants.DEFAULT_TS_PATTERN]: tsJest + '/preprocessor.js', + }); } } @@ -282,6 +309,15 @@ function normalize(config, argv) { config.usesBabelJest = true; } + if (tsJest) { + if (!config.testResultsProcessor) { + config.testResultsProcessor = tsJest + '/coverageprocessor.js'; + } + if (!config.moduleFileExtensions) { + config.moduleFileExtensions = ['js', 'jsx', 'json', 'ts', 'tsx']; + } + } + Object.keys(config).reduce((newConfig, key) => { let value; switch (key) { From 97bdaed4439071ae6d297320c6a30c002b67ff28 Mon Sep 17 00:00:00 2001 From: Igor Chulinda Date: Mon, 5 Dec 2016 14:56:57 +0100 Subject: [PATCH 2/2] Changing default testRegex is ts-jest is enabled. --- .../src/__tests__/normalize-test.js | 30 +++++++++++++++++++ packages/jest-config/src/normalize.js | 3 ++ 2 files changed, 33 insertions(+) diff --git a/packages/jest-config/src/__tests__/normalize-test.js b/packages/jest-config/src/__tests__/normalize-test.js index 5127417a6a81..5a43faa86294 100644 --- a/packages/jest-config/src/__tests__/normalize-test.js +++ b/packages/jest-config/src/__tests__/normalize-test.js @@ -637,6 +637,8 @@ describe('normalize', () => { .toEqual('/root/node_modules/ts-jest/coverageprocessor.js'); expect(config.moduleFileExtensions) .toEqual(['js', 'jsx', 'json', 'ts', 'tsx']); + expect(config.testRegex) + .toEqual('(/__tests__/.*|\\.(test|spec))\\.(j|t)sx?$'); }); it('uses ts-jest if ts-jest is explicitly specified in a custom transform config', () => { @@ -659,6 +661,8 @@ describe('normalize', () => { .toEqual('/root/node_modules/ts-jest/coverageprocessor.js'); expect(config.moduleFileExtensions) .toEqual(['js', 'jsx', 'json', 'ts', 'tsx']); + expect(config.testRegex) + .toEqual('(/__tests__/.*|\\.(test|spec))\\.(j|t)sx?$'); }); it(`doesn't use ts-jest if its not available`, () => { @@ -672,6 +676,8 @@ describe('normalize', () => { expect(config.testResultsProcessor).toEqual(undefined); expect(config.moduleFileExtensions) .toEqual(['js', 'json', 'jsx', 'node']); + expect(config.testRegex) + .toEqual('(/__tests__/.*|\\.(test|spec))\\.jsx?$'); }); it(`doesn't use ts-jest coverage proccessor if another is defined`, () => { @@ -689,6 +695,8 @@ describe('normalize', () => { .toEqual('/root/node_modules/ts-jest/coverageprocessor.js'); expect(config.moduleFileExtensions) .toEqual(['js', 'jsx', 'json', 'ts', 'tsx']); + expect(config.testRegex) + .toEqual('(/__tests__/.*|\\.(test|spec))\\.(j|t)sx?$'); }); it(`doesn't use ts module extensions nor defalut if another is defined`, () => { @@ -707,6 +715,28 @@ describe('normalize', () => { .toEqual(['js', 'json', 'jsx', 'node']); expect(config.moduleFileExtensions).not .toEqual(['js', 'jsx', 'json', 'ts', 'tsx']); + expect(config.testRegex) + .toEqual('(/__tests__/.*|\\.(test|spec))\\.(j|t)sx?$'); + }); + + it(`doesn't use ts test Regex nor defalut if another is defined`, () => { + const config = normalize({ + rootDir: '/root', + testRegex: '\\.spec\\.tsx?$', + }); + const tsTransformerPath = uniformPath(config.transform[1][1]); + const testResultsProcessorPath = uniformPath(config.testResultsProcessor); + expect(config.transform[1][0]).toBe(DEFAULT_TS_PATTERN); + expect(tsTransformerPath) + .toEqual('/root/node_modules/ts-jest/preprocessor.js'); + expect(testResultsProcessorPath) + .toEqual('/root/node_modules/ts-jest/coverageprocessor.js'); + expect(config.moduleFileExtensions) + .toEqual(['js', 'jsx', 'json', 'ts', 'tsx']); + expect(config.testRegex).not + .toEqual('(/__tests__/.*|\\.(test|spec))\\.(j|t)sx?$'); + expect(config.testRegex).not + .toEqual('(/__tests__/.*|\\.(test|spec))\\.jsx?$'); }); }); diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 4e28668bbaa9..78fe801ec62a 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -316,6 +316,9 @@ function normalize(config, argv) { if (!config.moduleFileExtensions) { config.moduleFileExtensions = ['js', 'jsx', 'json', 'ts', 'tsx']; } + if (!config.testRegex) { + config.testRegex = '(/__tests__/.*|\\.(test|spec))\\.(j|t)sx?$'; + } } Object.keys(config).reduce((newConfig, key) => {