diff --git a/e2e/__cases__/allow-js/with-outDir/bar.spec.ts b/e2e/__cases__/allow-js/bar.spec.ts similarity index 100% rename from e2e/__cases__/allow-js/with-outDir/bar.spec.ts rename to e2e/__cases__/allow-js/bar.spec.ts diff --git a/e2e/__cases__/allow-js/with-outDir/bar.ts b/e2e/__cases__/allow-js/bar.ts similarity index 100% rename from e2e/__cases__/allow-js/with-outDir/bar.ts rename to e2e/__cases__/allow-js/bar.ts diff --git a/e2e/__cases__/allow-js/with-outDir/esm.spec.js b/e2e/__cases__/allow-js/esm.spec.js similarity index 100% rename from e2e/__cases__/allow-js/with-outDir/esm.spec.js rename to e2e/__cases__/allow-js/esm.spec.js diff --git a/e2e/__cases__/allow-js/with-outDir/foo.js b/e2e/__cases__/allow-js/foo.js similarity index 100% rename from e2e/__cases__/allow-js/with-outDir/foo.js rename to e2e/__cases__/allow-js/foo.js diff --git a/e2e/__cases__/allow-js/with-outDir/foo.spec.js b/e2e/__cases__/allow-js/foo.spec.js similarity index 100% rename from e2e/__cases__/allow-js/with-outDir/foo.spec.js rename to e2e/__cases__/allow-js/foo.spec.js diff --git a/e2e/__cases__/allow-js/with-outDir/tsconfig.json b/e2e/__cases__/allow-js/tsconfig.json similarity index 100% rename from e2e/__cases__/allow-js/with-outDir/tsconfig.json rename to e2e/__cases__/allow-js/tsconfig.json diff --git a/e2e/__cases__/allow-js/without-outDir/bar.spec.ts b/e2e/__cases__/allow-js/without-outDir/bar.spec.ts deleted file mode 100644 index b2be7ec5e8..0000000000 --- a/e2e/__cases__/allow-js/without-outDir/bar.spec.ts +++ /dev/null @@ -1,10 +0,0 @@ -import * as foo from './foo' -import bar = require('./bar') - -test('foo', () => { - expect(foo).toBe('FOO!') -}) - -test('bar', () => { - expect(bar).toBe('BAR!') -}) diff --git a/e2e/__cases__/allow-js/without-outDir/bar.ts b/e2e/__cases__/allow-js/without-outDir/bar.ts deleted file mode 100644 index 03df82b30b..0000000000 --- a/e2e/__cases__/allow-js/without-outDir/bar.ts +++ /dev/null @@ -1 +0,0 @@ -export = 'BAR!' diff --git a/e2e/__cases__/allow-js/without-outDir/esm.spec.js b/e2e/__cases__/allow-js/without-outDir/esm.spec.js deleted file mode 100644 index 4672def549..0000000000 --- a/e2e/__cases__/allow-js/without-outDir/esm.spec.js +++ /dev/null @@ -1,5 +0,0 @@ -import * as bar from './bar' - -test('esm', () => { - expect(bar).toBe('BAR!') -}) diff --git a/e2e/__cases__/allow-js/without-outDir/foo.js b/e2e/__cases__/allow-js/without-outDir/foo.js deleted file mode 100644 index 73cfb2112e..0000000000 --- a/e2e/__cases__/allow-js/without-outDir/foo.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = 'FOO!' diff --git a/e2e/__cases__/allow-js/without-outDir/foo.spec.js b/e2e/__cases__/allow-js/without-outDir/foo.spec.js deleted file mode 100644 index d6eb95ee1a..0000000000 --- a/e2e/__cases__/allow-js/without-outDir/foo.spec.js +++ /dev/null @@ -1,10 +0,0 @@ -const foo = require('./foo') -const bar = require('./bar') - -test('foo', () => { - expect(foo).toBe('FOO!') -}) - -test('bar', () => { - expect(bar).toBe('BAR!') -}) diff --git a/e2e/__cases__/allow-js/without-outDir/tsconfig.json b/e2e/__cases__/allow-js/without-outDir/tsconfig.json deleted file mode 100644 index 26f31aa261..0000000000 --- a/e2e/__cases__/allow-js/without-outDir/tsconfig.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "compilerOptions": { - "target": "es5", - "allowJs": true - } -} diff --git a/e2e/__tests__/__snapshots__/allow-js.test.ts.snap b/e2e/__tests__/__snapshots__/allow-js.test.ts.snap deleted file mode 100644 index 0227d56a0f..0000000000 --- a/e2e/__tests__/__snapshots__/allow-js.test.ts.snap +++ /dev/null @@ -1,154 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`using babel-jest for js files should pass using template "default" 1`] = ` - √ jest - ↳ exit code: 0 - ===[ STDOUT ]=================================================================== - - ===[ STDERR ]=================================================================== - PASS ./bar.spec.ts - PASS ./foo.spec.js - - Test Suites: 2 passed, 2 total - Tests: 4 passed, 4 total - Snapshots: 0 total - Time: XXs - Ran all test suites. - ================================================================================ -`; - -exports[`using babel-jest for js files should pass using template "with-babel-7" 1`] = ` - √ jest - ↳ exit code: 0 - ===[ STDOUT ]=================================================================== - - ===[ STDERR ]=================================================================== - PASS ./bar.spec.ts - PASS ./foo.spec.js - - Test Suites: 2 passed, 2 total - Tests: 4 passed, 4 total - Snapshots: 0 total - Time: XXs - Ran all test suites. - ================================================================================ -`; - -exports[`using babel-jest for js files should pass using template "with-babel-7-string-config" 1`] = ` - √ jest - ↳ exit code: 0 - ===[ STDOUT ]=================================================================== - - ===[ STDERR ]=================================================================== - PASS ./bar.spec.ts - PASS ./foo.spec.js - - Test Suites: 2 passed, 2 total - Tests: 4 passed, 4 total - Snapshots: 0 total - Time: XXs - Ran all test suites. - ================================================================================ -`; - -exports[`using ts-jest for js files with outDir should pass using template "default" 1`] = ` - √ jest - ↳ exit code: 0 - ===[ STDOUT ]=================================================================== - - ===[ STDERR ]=================================================================== - PASS ./esm.spec.js - √ esm - - Test Suites: 1 passed, 1 total - Tests: 1 passed, 1 total - Snapshots: 0 total - Time: XXs - Ran all test suites. - ================================================================================ -`; - -exports[`using ts-jest for js files with outDir should pass using template "with-babel-7" 1`] = ` - √ jest - ↳ exit code: 0 - ===[ STDOUT ]=================================================================== - - ===[ STDERR ]=================================================================== - PASS ./esm.spec.js - √ esm - - Test Suites: 1 passed, 1 total - Tests: 1 passed, 1 total - Snapshots: 0 total - Time: XXs - Ran all test suites. - ================================================================================ -`; - -exports[`using ts-jest for js files with outDir should pass using template "with-babel-7-string-config" 1`] = ` - √ jest - ↳ exit code: 0 - ===[ STDOUT ]=================================================================== - - ===[ STDERR ]=================================================================== - PASS ./esm.spec.js - √ esm - - Test Suites: 1 passed, 1 total - Tests: 1 passed, 1 total - Snapshots: 0 total - Time: XXs - Ran all test suites. - ================================================================================ -`; - -exports[`using ts-jest for js files without outDir should pass using template "default" 1`] = ` - √ jest - ↳ exit code: 0 - ===[ STDOUT ]=================================================================== - - ===[ STDERR ]=================================================================== - PASS ./esm.spec.js - √ esm - - Test Suites: 1 passed, 1 total - Tests: 1 passed, 1 total - Snapshots: 0 total - Time: XXs - Ran all test suites. - ================================================================================ -`; - -exports[`using ts-jest for js files without outDir should pass using template "with-babel-7" 1`] = ` - √ jest - ↳ exit code: 0 - ===[ STDOUT ]=================================================================== - - ===[ STDERR ]=================================================================== - PASS ./esm.spec.js - √ esm - - Test Suites: 1 passed, 1 total - Tests: 1 passed, 1 total - Snapshots: 0 total - Time: XXs - Ran all test suites. - ================================================================================ -`; - -exports[`using ts-jest for js files without outDir should pass using template "with-babel-7-string-config" 1`] = ` - √ jest - ↳ exit code: 0 - ===[ STDOUT ]=================================================================== - - ===[ STDERR ]=================================================================== - PASS ./esm.spec.js - √ esm - - Test Suites: 1 passed, 1 total - Tests: 1 passed, 1 total - Snapshots: 0 total - Time: XXs - Ran all test suites. - ================================================================================ -`; diff --git a/e2e/__tests__/allow-js.test.ts b/e2e/__tests__/allow-js.test.ts index 8548c9bc8b..ef9aff0258 100644 --- a/e2e/__tests__/allow-js.test.ts +++ b/e2e/__tests__/allow-js.test.ts @@ -2,7 +2,7 @@ import { allPackageSetsWithPreset, allValidPackageSets } from '../__helpers__/te import { configureTestCase } from '../__helpers__/test-case' describe('using babel-jest for js files', () => { - const testCase = configureTestCase('allow-js/with-outDir', { + const testCase = configureTestCase('allow-js', { jestConfig: { testRegex: '(foo|bar)\\.spec\\.[jt]s$' }, }) @@ -10,13 +10,12 @@ describe('using babel-jest for js files', () => { it(testLabel, () => { const result = runTest() expect(result.status).toBe(0) - expect(result).toMatchSnapshot() }) }) }) -describe('using ts-jest for js files with outDir', () => { - const testCase = configureTestCase('allow-js/with-outDir', { +describe('using ts-jest for js files', () => { + const testCase = configureTestCase('allow-js', { jestConfig: { preset: 'ts-jest/presets/js-with-ts', testRegex: 'esm\\.spec\\.[jt]s$', @@ -27,24 +26,6 @@ describe('using ts-jest for js files with outDir', () => { it(testLabel, () => { const result = runTest() expect(result.status).toBe(0) - expect(result).toMatchSnapshot() - }) - }) -}) - -describe('using ts-jest for js files without outDir', () => { - const testCase = configureTestCase('allow-js/without-outDir', { - jestConfig: { - preset: 'ts-jest/presets/js-with-ts', - testRegex: 'esm\\.spec\\.[jt]s$', - }, - }) - - testCase.runWithTemplates(allPackageSetsWithPreset, 0, (runTest, { testLabel }) => { - it(testLabel, () => { - const result = runTest() - expect(result.status).toBe(0) - expect(result).toMatchSnapshot() }) }) }) diff --git a/src/compiler/ts-compiler.spec.ts b/src/compiler/ts-compiler.spec.ts index f474213ac1..40ff99c7df 100644 --- a/src/compiler/ts-compiler.spec.ts +++ b/src/compiler/ts-compiler.spec.ts @@ -7,7 +7,6 @@ import { createConfigSet, makeCompiler } from '../__helpers__/fakers' import { logTargetMock } from '../__helpers__/mocks' import { mockFolder } from '../__helpers__/path' import ProcessedSource from '../__helpers__/processed-source' -import { TS_JEST_OUT_DIR } from '../config/config-set' import { TsCompiler } from './ts-compiler' @@ -33,7 +32,7 @@ describe('TsCompiler', () => { test('should compile js file for allowJs true', () => { const fileName = 'foo.js' const compiler = makeCompiler({ - tsJestConfig: { ...baseTsJestConfig, tsconfig: { allowJs: true, outDir: TS_JEST_OUT_DIR } }, + tsJestConfig: { ...baseTsJestConfig, tsconfig: { allowJs: true } }, }) const source = 'export default 42' @@ -189,7 +188,6 @@ const t: string = f(5) ...baseTsJestConfig, tsconfig: { allowJs: true, - outDir: TS_JEST_OUT_DIR, }, astTransformers: { before: ['dummy-transformer'], diff --git a/src/compiler/ts-compiler.ts b/src/compiler/ts-compiler.ts index bcc803a888..6b4f6227f0 100644 --- a/src/compiler/ts-compiler.ts +++ b/src/compiler/ts-compiler.ts @@ -1,4 +1,4 @@ -import { basename, normalize, relative } from 'path' +import { basename, normalize } from 'path' import { LogContexts, Logger, LogLevels } from 'bs-logger' import memoize from 'lodash/memoize' @@ -201,7 +201,7 @@ export class TsCompiler implements TsCompilerInstance { this._doTypeChecking(fileName) /* istanbul ignore next (this should never happen but is kept for security) */ if (output.emitSkipped) { - throw new TypeError(`${relative(this.configSet.cwd, fileName)}: Emit skipped for language service`) + throw new Error(interpolate(Errors.CannotCompile, { file: fileName })) } // Throw an error when requiring `.d.ts` files. if (!output.outputFiles.length) { diff --git a/src/config/config-set.spec.ts b/src/config/config-set.spec.ts index fe90079b1a..ee151465e4 100644 --- a/src/config/config-set.spec.ts +++ b/src/config/config-set.spec.ts @@ -51,15 +51,12 @@ describe('parsedTsConfig', () => { }) it('should override some options', () => { - expect(get({ tsconfig: { inlineSources: false } }).options).toMatchObject({ + expect(get({ tsconfig: { inlineSources: false, outDir: 'build' } }).options).toMatchObject({ inlineSources: true, + outDir: TS_JEST_OUT_DIR, }) }) - it('should include default outDir $$ts-jest$$ when allowJs is enabled and no outDir from config', () => { - expect(get({ tsconfig: { allowJs: true } }).options.outDir).toBe(TS_JEST_OUT_DIR) - }) - it('should be able to read extends', () => { const cs = createConfigSet({ tsJestConfig: { tsconfig: 'tsconfig.build.json' }, diff --git a/src/config/config-set.ts b/src/config/config-set.ts index 2753035a04..ba217026d6 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -158,6 +158,8 @@ export class ConfigSet { // to clear out else it's buggy out: undefined, outFile: undefined, + // ensure that `LanguageService` won't pick up things from `build` folder which can lead to emit skipped error + outDir: TS_JEST_OUT_DIR, composite: undefined, // see https://github.com/TypeStrong/ts-node/pull/657/files declarationDir: undefined, declarationMap: undefined, @@ -419,10 +421,6 @@ export class ConfigSet { finalOptions.allowSyntheticDefaultImports = true } } - // Make sure when allowJs is enabled, outDir is required to have when using allowJs: true - if (finalOptions.allowJs && !finalOptions.outDir) { - finalOptions.outDir = TS_JEST_OUT_DIR - } // ensure undefined are removed and other values are overridden for (const key of Object.keys(forcedOptions)) { diff --git a/src/utils/messages.ts b/src/utils/messages.ts index d740530d45..e02de4c4bc 100644 --- a/src/utils/messages.ts +++ b/src/utils/messages.ts @@ -17,6 +17,7 @@ export const enum Errors { GotUnknownFileTypeWithBabel = 'Got a unknown file type to compile (file: {{path}}). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore. If you still want Babel to process it, add another entry to the `transform` option with value `babel-jest` which key matches this type of files.', ConfigNoModuleInterop = 'If you have issues related to imports, you should consider setting `esModuleInterop` to `true` in your TypeScript configuration file (usually `tsconfig.json`). See https://blogs.msdn.microsoft.com/typescript/2018/01/31/announcing-typescript-2-7/#easier-ecmascript-module-interoperability for more information.', MismatchNodeTargetMapping = 'There is a mismatch between your NodeJs version {{nodeJsVer}} and your TypeScript target {{compilationTarget}}. This might lead to some unexpected errors when running tests with `ts-jest`. To fix this, you can check https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping', + CannotCompile = "Unable to process '{{file}}'. Please check your tsconfig.", } /**