diff --git a/e2e/__tests__/__snapshots__/hoisting.test.ts.snap b/e2e/__tests__/__snapshots__/hoisting.test.ts.snap index 0c30a8c019..68918db94f 100644 --- a/e2e/__tests__/__snapshots__/hoisting.test.ts.snap +++ b/e2e/__tests__/__snapshots__/hoisting.test.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Hoisting test with template "default" should pass 1`] = ` +exports[`Hoisting test should pass using template "default" 1`] = ` jest exit code: 0 ===[ STDOUT ]=================================================================== @@ -17,7 +17,7 @@ jest exit code: 0 ================================================================================ `; -exports[`Hoisting test with template "with-babel-6" should pass 1`] = ` +exports[`Hoisting test should pass using template "with-babel-6" 1`] = ` jest exit code: 0 ===[ STDOUT ]=================================================================== @@ -34,7 +34,7 @@ jest exit code: 0 ================================================================================ `; -exports[`Hoisting test with template "with-babel-7" should pass 1`] = ` +exports[`Hoisting test should pass using template "with-babel-7" 1`] = ` jest exit code: 0 ===[ STDOUT ]=================================================================== @@ -51,7 +51,7 @@ jest exit code: 0 ================================================================================ `; -exports[`Hoisting test with template "with-jest-22" should pass 1`] = ` +exports[`Hoisting test should pass using template "with-jest-22" 1`] = ` jest exit code: 0 ===[ STDOUT ]=================================================================== diff --git a/e2e/__tests__/__snapshots__/simple.test.ts.snap b/e2e/__tests__/__snapshots__/simple.test.ts.snap index 3cdc10a52d..d819ab3dfb 100644 --- a/e2e/__tests__/__snapshots__/simple.test.ts.snap +++ b/e2e/__tests__/__snapshots__/simple.test.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Simple test with template "default" should pass 1`] = ` +exports[`Simple test should pass using template "default" 1`] = ` jest exit code: 0 ===[ STDOUT ]=================================================================== @@ -17,7 +17,7 @@ jest exit code: 0 ================================================================================ `; -exports[`Simple test with template "with-babel-6" should pass 1`] = ` +exports[`Simple test should pass using template "with-babel-6" 1`] = ` jest exit code: 0 ===[ STDOUT ]=================================================================== @@ -34,7 +34,7 @@ jest exit code: 0 ================================================================================ `; -exports[`Simple test with template "with-babel-7" should pass 1`] = ` +exports[`Simple test should pass using template "with-babel-7" 1`] = ` jest exit code: 0 ===[ STDOUT ]=================================================================== @@ -51,7 +51,7 @@ jest exit code: 0 ================================================================================ `; -exports[`Simple test with template "with-jest-22" should pass 1`] = ` +exports[`Simple test should pass using template "with-jest-22" 1`] = ` jest exit code: 0 ===[ STDOUT ]=================================================================== diff --git a/e2e/__tests__/__snapshots__/source-map.test.ts.snap b/e2e/__tests__/__snapshots__/source-map.test.ts.snap index 8dc2e939d8..6491b8a679 100644 --- a/e2e/__tests__/__snapshots__/source-map.test.ts.snap +++ b/e2e/__tests__/__snapshots__/source-map.test.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Source maps console.log() with template "default" should pass reporting correct line number 1`] = ` +exports[`Source maps console.log() should pass reporting correct line number using template "default" 1`] = ` jest exit code: 0 ===[ STDOUT ]=================================================================== console.log echo.spec.ts:5 @@ -21,7 +21,7 @@ jest exit code: 0 ================================================================================ `; -exports[`Source maps console.log() with template "with-babel-6" should pass reporting correct line number 1`] = ` +exports[`Source maps console.log() should pass reporting correct line number using template "with-babel-6" 1`] = ` jest exit code: 0 ===[ STDOUT ]=================================================================== console.log echo.spec.ts:5 @@ -42,7 +42,7 @@ jest exit code: 0 ================================================================================ `; -exports[`Source maps console.log() with template "with-babel-7" should pass reporting correct line number 1`] = ` +exports[`Source maps console.log() should pass reporting correct line number using template "with-babel-7" 1`] = ` jest exit code: 0 ===[ STDOUT ]=================================================================== console.log echo.spec.ts:5 @@ -63,7 +63,7 @@ jest exit code: 0 ================================================================================ `; -exports[`Source maps console.log() with template "with-jest-22" should pass reporting correct line number 1`] = ` +exports[`Source maps console.log() should pass reporting correct line number using template "with-jest-22" 1`] = ` jest exit code: 0 ===[ STDOUT ]=================================================================== console.log echo.spec.ts:5 @@ -84,7 +84,7 @@ jest exit code: 0 ================================================================================ `; -exports[`Source maps throw new Error() with template "default" should fail reporting correct line number 1`] = ` +exports[`Source maps throw new Error() should fail reporting correct line number using template "default" 1`] = ` jest exit code: 1 ===[ STDOUT ]=================================================================== console.log echo.spec.ts:5 @@ -120,7 +120,7 @@ jest exit code: 1 ================================================================================ `; -exports[`Source maps throw new Error() with template "with-babel-6" should fail reporting correct line number 1`] = ` +exports[`Source maps throw new Error() should fail reporting correct line number using template "with-babel-6" 1`] = ` jest exit code: 1 ===[ STDOUT ]=================================================================== console.log echo.spec.ts:5 @@ -156,7 +156,7 @@ jest exit code: 1 ================================================================================ `; -exports[`Source maps throw new Error() with template "with-babel-7" should fail reporting correct line number 1`] = ` +exports[`Source maps throw new Error() should fail reporting correct line number using template "with-babel-7" 1`] = ` jest exit code: 1 ===[ STDOUT ]=================================================================== console.log echo.spec.ts:5 @@ -192,7 +192,7 @@ jest exit code: 1 ================================================================================ `; -exports[`Source maps throw new Error() with template "with-jest-22" should fail reporting correct line number 1`] = ` +exports[`Source maps throw new Error() should fail reporting correct line number using template "with-jest-22" 1`] = ` jest exit code: 1 ===[ STDOUT ]=================================================================== console.log echo.spec.ts:5 diff --git a/e2e/__tests__/hoisting.test.ts b/e2e/__tests__/hoisting.test.ts index c5c63631e4..df6fa0393b 100644 --- a/e2e/__tests__/hoisting.test.ts +++ b/e2e/__tests__/hoisting.test.ts @@ -4,17 +4,11 @@ import { allPackageSets } from '../__helpers__/templates'; describe('Hoisting test', () => { const testCase = configureTestCase('hoisting', { args: ['--no-cache'] }); - testCase.runWithTemplates( - allPackageSets, - 0, - (runTest, { describeLabel, itLabel }) => { - describe(describeLabel, () => { - it(itLabel, () => { - const result = runTest(); - expect(result.status).toBe(0); - expect(result).toMatchSnapshot(); - }); - }); - }, - ); + testCase.runWithTemplates(allPackageSets, 0, (runTest, { testLabel }) => { + it(testLabel, () => { + const result = runTest(); + expect(result.status).toBe(0); + expect(result).toMatchSnapshot(); + }); + }); }); diff --git a/e2e/__tests__/simple.test.ts b/e2e/__tests__/simple.test.ts index 9045f74550..58c4949560 100644 --- a/e2e/__tests__/simple.test.ts +++ b/e2e/__tests__/simple.test.ts @@ -4,17 +4,11 @@ import { allPackageSets } from '../__helpers__/templates'; describe('Simple test', () => { const testCase = configureTestCase('simple', { args: ['--no-cache'] }); - testCase.runWithTemplates( - allPackageSets, - 0, - (runTest, { describeLabel, itLabel }) => { - describe(describeLabel, () => { - it(itLabel, () => { - const result = runTest(); - expect(result.status).toBe(0); - expect(result).toMatchSnapshot(); - }); - }); - }, - ); + testCase.runWithTemplates(allPackageSets, 0, (runTest, { testLabel }) => { + it(testLabel, () => { + const result = runTest(); + expect(result.status).toBe(0); + expect(result).toMatchSnapshot(); + }); + }); }); diff --git a/e2e/__tests__/source-map.test.ts b/e2e/__tests__/source-map.test.ts index 8180dad8b7..9b55283631 100644 --- a/e2e/__tests__/source-map.test.ts +++ b/e2e/__tests__/source-map.test.ts @@ -8,13 +8,11 @@ describe('Source maps', () => { testCase.runWithTemplates( allPackageSets, 0, - (runTest, { describeLabel }) => { - describe(describeLabel, () => { - it('should pass reporting correct line number', () => { - const result = runTest(); - expect(result.status).toBe(0); - expect(result).toMatchSnapshot(); - }); + (runTest, { templateName }) => { + it(`should pass reporting correct line number using template "${templateName}"`, () => { + const result = runTest(); + expect(result.status).toBe(0); + expect(result).toMatchSnapshot(); }); }, ); @@ -29,13 +27,11 @@ describe('Source maps', () => { testCase.runWithTemplates( allPackageSets, 1, - (runTest, { describeLabel }) => { - describe(describeLabel, () => { - it('should fail reporting correct line number', () => { - const result = runTest(); - expect(result.status).toBe(1); - expect(result).toMatchSnapshot(); - }); + (runTest, { templateName }) => { + it(`should fail reporting correct line number using template "${templateName}"`, () => { + const result = runTest(); + expect(result.status).toBe(1); + expect(result).toMatchSnapshot(); }); }, ); diff --git a/src/utils/diagnostics.ts b/src/diagnostics.ts similarity index 98% rename from src/utils/diagnostics.ts rename to src/diagnostics.ts index cf1db3462b..0ee9fd18ae 100644 --- a/src/utils/diagnostics.ts +++ b/src/diagnostics.ts @@ -1,4 +1,4 @@ -import { DiagnosticTypes, DiagnosticSets, diagnosticSets } from '../types'; +import { DiagnosticTypes, DiagnosticSets, diagnosticSets } from './types'; export const isDiagnosticType = (val: any): val is DiagnosticTypes => { return val && DiagnosticTypes[val] === val; diff --git a/src/index.ts b/src/index.ts index 18f4dceb7e..5051e378a3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import { TsJestGlobalOptions, TsJestConfig } from './types'; import TsProgram from './ts-program'; import Memoize from './memoize'; -import { normalizeDiagnosticTypes } from './utils/diagnostics'; +import { normalizeDiagnosticTypes } from './diagnostics'; const rootDirFor = (jestConfig: jest.ProjectConfig): string => { return jestConfig.rootDir || process.cwd(); diff --git a/src/transformers/manager.ts b/src/transformers/manager.ts deleted file mode 100644 index e62383b35f..0000000000 --- a/src/transformers/manager.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { SourceFile, Node } from 'typescript'; -import Replacement from './replacement'; - -// see tslint: https://github.com/palantir/tslint/blob/master/src/language/rule/rule.ts - -export default class TransformationManager { - protected _replacements: Replacement[]; - - constructor(protected _sourceFile: SourceFile) { - this._replacements = []; - } - - get sourceFile(): SourceFile { - return this._sourceFile; - } - - applyAndFlush(): SourceFile { - if (this._replacements.length === 0) { - return this._sourceFile; - } - - const replacements = this._replacements; - // sort - replacements.sort( - (a, b) => (b.end !== a.end ? b.end - a.end : b.start - a.start), - ); - // compute new text - const oldText = this.sourceFile.text; - const newText = replacements.reduce((text, r) => r.apply(text), oldText); - // apply - this._sourceFile = this._sourceFile.update(newText, { - newLength: newText.length, - span: { start: 0, length: oldText.length }, - }); - // flush - this._replacements = []; - - return this._sourceFile; - } - - replaceNode(node: Node, text: string) { - return this._push( - this._replaceFromTo(node.getStart(this.sourceFile), node.getEnd(), text), - ); - } - - replaceFromTo(start: number, end: number, text: string) { - return this._push(new Replacement(start, end - start, text)); - } - - deleteText(start: number, length: number) { - return this._push(new Replacement(start, length, '')); - } - - deleteFromTo(start: number, end: number) { - return this._push(new Replacement(start, end - start, '')); - } - - appendText(start: number, text: string) { - return this._push(new Replacement(start, 0, text)); - } - - protected _push(...replacements: Replacement[]): this { - this._replacements.push(...replacements); - return this; - } - - protected _replaceFromTo(start: number, end: number, text: string) { - return new Replacement(start, end - start, text); - } -} diff --git a/src/types.ts b/src/types.ts index c9af6f5123..a82566d471 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,6 +1,5 @@ import * as _babelJest from 'babel-jest'; -import { CompilerOptions, Node } from 'typescript'; -import TransformationManager from './transformers/manager'; +import { CompilerOptions } from 'typescript'; export type TBabelJest = typeof _babelJest; diff --git a/src/utils/file-extension.ts b/src/utils/file-extension.ts deleted file mode 100644 index 65c22e0a59..0000000000 --- a/src/utils/file-extension.ts +++ /dev/null @@ -1,4 +0,0 @@ -export default function(path: string): string { - const dotIndex = path.lastIndexOf('.'); - return path.substr(dotIndex + 1); -} diff --git a/src/utils/ts-internals.ts b/src/utils/ts-internals.ts deleted file mode 100644 index f75c2384a1..0000000000 --- a/src/utils/ts-internals.ts +++ /dev/null @@ -1,20 +0,0 @@ -import ts, { - SourceFile, - Bundle, - Node, - Diagnostic, - CompilerOptions, -} from 'typescript'; - -// typescript internals -// TODO: should we copy/paste their source since it's internal? -export const getNodeId: (node: Node) => any = (ts as any).getNodeId; -export const fixupCompilerOptions: ( - options: CompilerOptions, - diagnostics: Diagnostic[], -) => CompilerOptions = (ts as any).fixupCompilerOptions; -export const chainBundle: ( - transformSourceFile: (x: SourceFile) => SourceFile, -) => (x: SourceFile | Bundle) => SourceFile | Bundle = (ts as any).chainBundle; -export const getOriginalNodeId: (node: Node) => number = (ts as any) - .getOriginalNodeId;