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 72% rename from e2e/__cases__/allow-js/with-outDir/tsconfig.json rename to e2e/__cases__/allow-js/tsconfig.json index c052135751..cf8c6a9c72 100644 --- a/e2e/__cases__/allow-js/with-outDir/tsconfig.json +++ b/e2e/__cases__/allow-js/tsconfig.json @@ -2,6 +2,6 @@ "compilerOptions": { "target": "es5", "allowJs": true, - "outDir": "$$ts-jest$$" + "outDir": "./build" } } 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 663df5ddd0..d600801cf6 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: { testMatch: null, 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', testMatch: null, @@ -28,25 +27,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', - testMatch: null, - 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/__snapshots__/language-service.spec.ts.snap b/src/compiler/__snapshots__/language-service.spec.ts.snap index 6a184d5200..980d92c288 100644 --- a/src/compiler/__snapshots__/language-service.spec.ts.snap +++ b/src/compiler/__snapshots__/language-service.spec.ts.snap @@ -1,24 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Language service allowJs option should compile js file for allowJs true with outDir 1`] = ` - ===[ FILE: test-allow-js.js ]=================================================== - "use strict"; - Object.defineProperty(exports, "__esModule", { value: true }); - exports.default = 42; - //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvdy1qcy5qcyIsIm1hcHBpbmdzIjoiOztBQUFBLGtCQUFlLEVBQUUsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWFsbG93LWpzLmpzIl0sInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBkZWZhdWx0IDQyIl0sInZlcnNpb24iOjN9 - ===[ INLINE SOURCE MAPS ]======================================================= - file: test-allow-js.js - mappings: ;;AAAA,kBAAe,EAAE,CAAA - names: [] - sources: - - test-allow-js.js - sourcesContent: - - export default 42 - version: 3 - ================================================================================ -`; - -exports[`Language service allowJs option should compile js file for allowJs true without outDir 1`] = ` +exports[`Language service allowJs option should compile js file for allowJs true 1`] = ` ===[ FILE: test-allow-js.js ]=================================================== "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/src/compiler/language-service.spec.ts b/src/compiler/language-service.spec.ts index 2d7d940687..a009b86e8d 100644 --- a/src/compiler/language-service.spec.ts +++ b/src/compiler/language-service.spec.ts @@ -20,20 +20,11 @@ describe('Language service', () => { const fileName = 'test-allow-js.js' const source = 'export default 42' - it('should compile js file for allowJs true with outDir', () => { - const compiler = makeCompiler({ - tsJestConfig: { tsconfig: { allowJs: true, outDir: '$$foo$$' } }, - }) - - const compiled = compiler.compile(source, fileName) - - expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot() - }) - - it('should compile js file for allowJs true without outDir', () => { + it('should compile js file for allowJs true', () => { const compiler = makeCompiler({ tsJestConfig: { tsconfig: { allowJs: true } }, }) + const compiled = compiler.compile(source, fileName) expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot() diff --git a/src/compiler/language-service.ts b/src/compiler/language-service.ts index a6d7d8998f..aaaaccc8cf 100644 --- a/src/compiler/language-service.ts +++ b/src/compiler/language-service.ts @@ -1,6 +1,6 @@ import { LogContexts, Logger, LogLevels } from 'bs-logger' import { existsSync, readFileSync, writeFile } from 'fs' -import { basename, normalize, relative, join } from 'path' +import { basename, normalize, join } from 'path' import memoize = require('lodash/memoize') import mkdirp = require('mkdirp') import type * as _ts from 'typescript' @@ -260,7 +260,7 @@ export const initializeLanguageServiceInstance = (configs: ConfigSet, logger: Lo } /* istanbul ignore next (this should never happen but is kept for security) */ if (output.emitSkipped) { - throw new TypeError(`${relative(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/compiler/transpiler.spec.ts b/src/compiler/transpiler.spec.ts index 8a8e822ae8..564cf5514e 100644 --- a/src/compiler/transpiler.spec.ts +++ b/src/compiler/transpiler.spec.ts @@ -1,6 +1,5 @@ import { makeCompiler } from '../__helpers__/fakers' import ProcessedSource from '../__helpers__/processed-source' -import { TS_JEST_OUT_DIR } from '../config/config-set' describe('Transpiler', () => { const baseTsJestConfig = { @@ -10,7 +9,7 @@ describe('Transpiler', () => { it('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' diff --git a/src/config/config-set.spec.ts b/src/config/config-set.spec.ts index 9e73850299..2f0e006ba8 100644 --- a/src/config/config-set.spec.ts +++ b/src/config/config-set.spec.ts @@ -69,14 +69,13 @@ describe('parsedTsConfig', () => { }) it('should override some options', () => { - expect(get({ tsconfig: { module: 'esnext' as any, inlineSources: false } }).options).toMatchObject({ - module: ts.ModuleKind.CommonJS, - inlineSources: true, - }) - }) - - 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) + expect(get({ tsconfig: { module: 'esnext' as any, inlineSources: false, outDir: 'build' } }).options).toMatchObject( + { + module: ts.ModuleKind.CommonJS, + inlineSources: true, + outDir: TS_JEST_OUT_DIR, + }, + ) }) it('should be able to read extends', () => { diff --git a/src/config/config-set.ts b/src/config/config-set.ts index 52a16386ff..b45e1f1556 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -164,6 +164,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, @@ -483,10 +485,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 9338b912af..501b0b51ff 100644 --- a/src/utils/messages.ts +++ b/src/utils/messages.ts @@ -16,8 +16,8 @@ export const enum Errors { GotUnknownFileTypeWithoutBabel = '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.', 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.', - UnableToFindProjectRoot = 'Unable to find the root of the project where ts-jest has been installed.', 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.", } /**