From 1a287adc317cbad508112a6ddca50efb422e5060 Mon Sep 17 00:00:00 2001 From: Ahn Date: Sat, 4 Apr 2020 13:23:53 +0200 Subject: [PATCH] fix(config): set default `outDir` when `allowJs` is true and no `outDir` in `tsconfig` (#1502) --- .../allow-js/{ => with-outDir}/bar.spec.ts | 0 .../allow-js/{ => with-outDir}/bar.ts | 0 .../allow-js/{ => with-outDir}/esm.spec.js | 0 .../allow-js/{ => with-outDir}/foo.js | 0 .../allow-js/{ => with-outDir}/foo.spec.js | 0 .../allow-js/{ => with-outDir}/tsconfig.json | 0 .../allow-js/without-outDir/bar.spec.ts | 10 +++ e2e/__cases__/allow-js/without-outDir/bar.ts | 1 + .../allow-js/without-outDir/esm.spec.js | 5 ++ e2e/__cases__/allow-js/without-outDir/foo.js | 1 + .../allow-js/without-outDir/foo.spec.js | 10 +++ .../allow-js/without-outDir/tsconfig.json | 6 ++ .../__snapshots__/allow-js.test.ts.snap | 76 ++++++++++++++++- e2e/__tests__/allow-js.test.ts | 24 +++++- .../language-service.spec.ts.snap | 28 +++++-- .../__snapshots__/program.spec.ts.snap | 56 ++++++++++--- src/compiler/language-service.spec.ts | 21 ++++- src/compiler/program.spec.ts | 83 +++++++++++++++---- src/compiler/transpile-module.spec.ts | 3 +- src/config/config-set.spec.ts | 6 +- src/config/config-set.ts | 10 ++- 21 files changed, 294 insertions(+), 46 deletions(-) rename e2e/__cases__/allow-js/{ => with-outDir}/bar.spec.ts (100%) rename e2e/__cases__/allow-js/{ => with-outDir}/bar.ts (100%) rename e2e/__cases__/allow-js/{ => with-outDir}/esm.spec.js (100%) rename e2e/__cases__/allow-js/{ => with-outDir}/foo.js (100%) rename e2e/__cases__/allow-js/{ => with-outDir}/foo.spec.js (100%) rename e2e/__cases__/allow-js/{ => with-outDir}/tsconfig.json (100%) create mode 100644 e2e/__cases__/allow-js/without-outDir/bar.spec.ts create mode 100644 e2e/__cases__/allow-js/without-outDir/bar.ts create mode 100644 e2e/__cases__/allow-js/without-outDir/esm.spec.js create mode 100644 e2e/__cases__/allow-js/without-outDir/foo.js create mode 100644 e2e/__cases__/allow-js/without-outDir/foo.spec.js create mode 100644 e2e/__cases__/allow-js/without-outDir/tsconfig.json diff --git a/e2e/__cases__/allow-js/bar.spec.ts b/e2e/__cases__/allow-js/with-outDir/bar.spec.ts similarity index 100% rename from e2e/__cases__/allow-js/bar.spec.ts rename to e2e/__cases__/allow-js/with-outDir/bar.spec.ts diff --git a/e2e/__cases__/allow-js/bar.ts b/e2e/__cases__/allow-js/with-outDir/bar.ts similarity index 100% rename from e2e/__cases__/allow-js/bar.ts rename to e2e/__cases__/allow-js/with-outDir/bar.ts diff --git a/e2e/__cases__/allow-js/esm.spec.js b/e2e/__cases__/allow-js/with-outDir/esm.spec.js similarity index 100% rename from e2e/__cases__/allow-js/esm.spec.js rename to e2e/__cases__/allow-js/with-outDir/esm.spec.js diff --git a/e2e/__cases__/allow-js/foo.js b/e2e/__cases__/allow-js/with-outDir/foo.js similarity index 100% rename from e2e/__cases__/allow-js/foo.js rename to e2e/__cases__/allow-js/with-outDir/foo.js diff --git a/e2e/__cases__/allow-js/foo.spec.js b/e2e/__cases__/allow-js/with-outDir/foo.spec.js similarity index 100% rename from e2e/__cases__/allow-js/foo.spec.js rename to e2e/__cases__/allow-js/with-outDir/foo.spec.js diff --git a/e2e/__cases__/allow-js/tsconfig.json b/e2e/__cases__/allow-js/with-outDir/tsconfig.json similarity index 100% rename from e2e/__cases__/allow-js/tsconfig.json rename to e2e/__cases__/allow-js/with-outDir/tsconfig.json diff --git a/e2e/__cases__/allow-js/without-outDir/bar.spec.ts b/e2e/__cases__/allow-js/without-outDir/bar.spec.ts new file mode 100644 index 0000000000..b2be7ec5e8 --- /dev/null +++ b/e2e/__cases__/allow-js/without-outDir/bar.spec.ts @@ -0,0 +1,10 @@ +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 new file mode 100644 index 0000000000..03df82b30b --- /dev/null +++ b/e2e/__cases__/allow-js/without-outDir/bar.ts @@ -0,0 +1 @@ +export = 'BAR!' diff --git a/e2e/__cases__/allow-js/without-outDir/esm.spec.js b/e2e/__cases__/allow-js/without-outDir/esm.spec.js new file mode 100644 index 0000000000..4672def549 --- /dev/null +++ b/e2e/__cases__/allow-js/without-outDir/esm.spec.js @@ -0,0 +1,5 @@ +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 new file mode 100644 index 0000000000..73cfb2112e --- /dev/null +++ b/e2e/__cases__/allow-js/without-outDir/foo.js @@ -0,0 +1 @@ +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 new file mode 100644 index 0000000000..d6eb95ee1a --- /dev/null +++ b/e2e/__cases__/allow-js/without-outDir/foo.spec.js @@ -0,0 +1,10 @@ +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 new file mode 100644 index 0000000000..26f31aa261 --- /dev/null +++ b/e2e/__cases__/allow-js/without-outDir/tsconfig.json @@ -0,0 +1,6 @@ +{ + "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 index 400e658f16..ceb45fcdf5 100644 --- a/e2e/__tests__/__snapshots__/allow-js.test.ts.snap +++ b/e2e/__tests__/__snapshots__/allow-js.test.ts.snap @@ -68,7 +68,7 @@ exports[`using babel-jest for js files should pass using template "with-typescri ================================================================================ `; -exports[`using ts-jest for js files should pass using template "default" 1`] = ` +exports[`using ts-jest for js files with outDir should pass using template "default" 1`] = ` √ jest ↳ exit code: 0 ===[ STDOUT ]=================================================================== @@ -85,7 +85,7 @@ exports[`using ts-jest for js files should pass using template "default" 1`] = ` ================================================================================ `; -exports[`using ts-jest for js files should pass using template "with-babel-7" 1`] = ` +exports[`using ts-jest for js files with outDir should pass using template "with-babel-7" 1`] = ` √ jest ↳ exit code: 0 ===[ STDOUT ]=================================================================== @@ -102,7 +102,7 @@ exports[`using ts-jest for js files should pass using template "with-babel-7" 1` ================================================================================ `; -exports[`using ts-jest for js files should pass using template "with-babel-7-string-config" 1`] = ` +exports[`using ts-jest for js files with outDir should pass using template "with-babel-7-string-config" 1`] = ` √ jest ↳ exit code: 0 ===[ STDOUT ]=================================================================== @@ -119,7 +119,75 @@ exports[`using ts-jest for js files should pass using template "with-babel-7-str ================================================================================ `; -exports[`using ts-jest for js files should pass using template "with-typescript-2-7" 1`] = ` +exports[`using ts-jest for js files with outDir should pass using template "with-typescript-2-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 "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. + ================================================================================ +`; + +exports[`using ts-jest for js files without outDir should pass using template "with-typescript-2-7" 1`] = ` √ jest ↳ exit code: 0 ===[ STDOUT ]=================================================================== diff --git a/e2e/__tests__/allow-js.test.ts b/e2e/__tests__/allow-js.test.ts index 069aa58552..663df5ddd0 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', { + const testCase = configureTestCase('allow-js/with-outDir', { jestConfig: { testMatch: null, testRegex: '(foo|bar)\\.spec\\.[jt]s$' }, }) @@ -15,8 +15,26 @@ describe('using babel-jest for js files', () => { }) }) -describe('using ts-jest for js files', () => { - const testCase = configureTestCase('allow-js', { +describe('using ts-jest for js files with outDir', () => { + const testCase = configureTestCase('allow-js/with-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() + }) + }) +}) + +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, diff --git a/src/compiler/__snapshots__/language-service.spec.ts.snap b/src/compiler/__snapshots__/language-service.spec.ts.snap index 0d1f547a12..f6ae9ebfe7 100644 --- a/src/compiler/__snapshots__/language-service.spec.ts.snap +++ b/src/compiler/__snapshots__/language-service.spec.ts.snap @@ -1,17 +1,35 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`language service should compile js file for allowJs true 1`] = ` - ===[ FILE: test-allow-js.js ]=================================================== +exports[`language service should compile js file for allowJs true with outDir 1`] = ` + ===[ FILE: test-allow-js-with-outDir.js ]======================================= "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = 42; - //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvdy1qcy5qcyIsIm1hcHBpbmdzIjoiOztBQUFBLGtCQUFlLEVBQUUsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWFsbG93LWpzLmpzIl0sInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBkZWZhdWx0IDQyIl0sInZlcnNpb24iOjN9 + //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvdy1qcy13aXRoLW91dERpci5qcyIsIm1hcHBpbmdzIjoiOztBQUFBLGtCQUFlLEVBQUUsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWFsbG93LWpzLXdpdGgtb3V0RGlyLmpzIl0sInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBkZWZhdWx0IDQyIl0sInZlcnNpb24iOjN9 ===[ INLINE SOURCE MAPS ]======================================================= - file: test-allow-js.js + file: test-allow-js-with-outDir.js mappings: ';;AAAA,kBAAe,EAAE,CAAA' names: [] sources: - - test-allow-js.js + - test-allow-js-with-outDir.js + sourcesContent: + - export default 42 + version: 3 + ================================================================================ +`; + +exports[`language service should compile js file for allowJs true without outDir 1`] = ` + ===[ FILE: test-allow-js-no-outDir.js ]========================================= + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.default = 42; + //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvdy1qcy1uby1vdXREaXIuanMiLCJtYXBwaW5ncyI6Ijs7QUFBQSxrQkFBZSxFQUFFLENBQUEiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsidGVzdC1hbGxvdy1qcy1uby1vdXREaXIuanMiXSwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IGRlZmF1bHQgNDIiXSwidmVyc2lvbiI6M30= + ===[ INLINE SOURCE MAPS ]======================================================= + file: test-allow-js-no-outDir.js + mappings: ';;AAAA,kBAAe,EAAE,CAAA' + names: [] + sources: + - test-allow-js-no-outDir.js sourcesContent: - export default 42 version: 3 diff --git a/src/compiler/__snapshots__/program.spec.ts.snap b/src/compiler/__snapshots__/program.spec.ts.snap index b566e22448..906c4af532 100644 --- a/src/compiler/__snapshots__/program.spec.ts.snap +++ b/src/compiler/__snapshots__/program.spec.ts.snap @@ -1,35 +1,71 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`allowJs should compile js file for allowJs true with incremental program 1`] = ` - ===[ FILE: test-allowJs.test.js ]=============================================== +exports[`allowJs with incremental program should compile js file for allowJs true with outDir 1`] = ` + ===[ FILE: test-allowJs-incremental-outDir.test.js ]============================ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = 42; - //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvd0pzLnRlc3QuanMiLCJtYXBwaW5ncyI6Ijs7QUFBQSxrQkFBZSxFQUFFLENBQUEiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsidGVzdC1hbGxvd0pzLnRlc3QuanMiXSwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IGRlZmF1bHQgNDIiXSwidmVyc2lvbiI6M30= + //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvd0pzLWluY3JlbWVudGFsLW91dERpci50ZXN0LmpzIiwibWFwcGluZ3MiOiI7O0FBQUEsa0JBQWUsRUFBRSxDQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbInRlc3QtYWxsb3dKcy1pbmNyZW1lbnRhbC1vdXREaXIudGVzdC5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgZGVmYXVsdCA0MiJdLCJ2ZXJzaW9uIjozfQ== ===[ INLINE SOURCE MAPS ]======================================================= - file: test-allowJs.test.js + file: test-allowJs-incremental-outDir.test.js mappings: ';;AAAA,kBAAe,EAAE,CAAA' names: [] sources: - - test-allowJs.test.js + - test-allowJs-incremental-outDir.test.js sourcesContent: - export default 42 version: 3 ================================================================================ `; -exports[`allowJs should compile js file for allowJs true with normal program 1`] = ` - ===[ FILE: test-allowJs.test.js ]=============================================== +exports[`allowJs with incremental program should compile js file for allowJs true without outDir 1`] = ` + ===[ FILE: test-allowJs-incremental-no-outDir.test.js ]========================= "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = 42; - //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvd0pzLnRlc3QuanMiLCJtYXBwaW5ncyI6Ijs7QUFBQSxrQkFBZSxFQUFFLENBQUEiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsidGVzdC1hbGxvd0pzLnRlc3QuanMiXSwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IGRlZmF1bHQgNDIiXSwidmVyc2lvbiI6M30= + //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvd0pzLWluY3JlbWVudGFsLW5vLW91dERpci50ZXN0LmpzIiwibWFwcGluZ3MiOiI7O0FBQUEsa0JBQWUsRUFBRSxDQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbInRlc3QtYWxsb3dKcy1pbmNyZW1lbnRhbC1uby1vdXREaXIudGVzdC5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgZGVmYXVsdCA0MiJdLCJ2ZXJzaW9uIjozfQ== ===[ INLINE SOURCE MAPS ]======================================================= - file: test-allowJs.test.js + file: test-allowJs-incremental-no-outDir.test.js mappings: ';;AAAA,kBAAe,EAAE,CAAA' names: [] sources: - - test-allowJs.test.js + - test-allowJs-incremental-no-outDir.test.js + sourcesContent: + - export default 42 + version: 3 + ================================================================================ +`; + +exports[`allowJs with program should compile js file for allowJs true with outDir 1`] = ` + ===[ FILE: test-allowJs-program-outDir.test.js ]================================ + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.default = 42; + //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvd0pzLXByb2dyYW0tb3V0RGlyLnRlc3QuanMiLCJtYXBwaW5ncyI6Ijs7QUFBQSxrQkFBZSxFQUFFLENBQUEiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsidGVzdC1hbGxvd0pzLXByb2dyYW0tb3V0RGlyLnRlc3QuanMiXSwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IGRlZmF1bHQgNDIiXSwidmVyc2lvbiI6M30= + ===[ INLINE SOURCE MAPS ]======================================================= + file: test-allowJs-program-outDir.test.js + mappings: ';;AAAA,kBAAe,EAAE,CAAA' + names: [] + sources: + - test-allowJs-program-outDir.test.js + sourcesContent: + - export default 42 + version: 3 + ================================================================================ +`; + +exports[`allowJs with program should compile js file for allowJs true without outDir 1`] = ` + ===[ FILE: test-allowJs-program-no-outDir.test.js ]============================= + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.default = 42; + //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvd0pzLXByb2dyYW0tbm8tb3V0RGlyLnRlc3QuanMiLCJtYXBwaW5ncyI6Ijs7QUFBQSxrQkFBZSxFQUFFLENBQUEiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsidGVzdC1hbGxvd0pzLXByb2dyYW0tbm8tb3V0RGlyLnRlc3QuanMiXSwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IGRlZmF1bHQgNDIiXSwidmVyc2lvbiI6M30= + ===[ INLINE SOURCE MAPS ]======================================================= + file: test-allowJs-program-no-outDir.test.js + mappings: ';;AAAA,kBAAe,EAAE,CAAA' + names: [] + sources: + - test-allowJs-program-no-outDir.test.js sourcesContent: - export default 42 version: 3 diff --git a/src/compiler/language-service.spec.ts b/src/compiler/language-service.spec.ts index 7fba8ab490..7088afc67b 100644 --- a/src/compiler/language-service.spec.ts +++ b/src/compiler/language-service.spec.ts @@ -60,10 +60,25 @@ describe('language service', () => { removeSync(fileName) }) - it('should compile js file for allowJs true', () => { - const fileName = `test-allow-js.js` + it('should compile js file for allowJs true with outDir', () => { + const fileName = `test-allow-js-with-outDir.js` const compiler = makeCompiler({ - tsJestConfig: { tsConfig: { allowJs: true, outDir: '$$ts-jest$$' } }, + tsJestConfig: { tsConfig: { allowJs: true, outDir: '$$foo$$' } }, + }) + const source = 'export default 42' + + writeFileSync(fileName, source, 'utf8') + const compiled = compiler.compile(source, fileName) + + expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot() + + removeSync(fileName) + }) + + it('should compile js file for allowJs true without outDir', () => { + const fileName = `test-allow-js-no-outDir.js` + const compiler = makeCompiler({ + tsJestConfig: { tsConfig: { allowJs: true } }, }) const source = 'export default 42' diff --git a/src/compiler/program.spec.ts b/src/compiler/program.spec.ts index 7703ede6f1..fff2b56153 100644 --- a/src/compiler/program.spec.ts +++ b/src/compiler/program.spec.ts @@ -108,36 +108,83 @@ describe('cache', () => { }) describe('allowJs', () => { - const fileName = 'test-allowJs.test.js' + const baseFileName = 'test-allowJs' + const baseFileExt = 'test.js' const source = 'export default 42' - const tsConfig = { allowJs: true, outDir: '$$ts-jest$$' } + const tsConfig = { allowJs: true } - beforeAll(() => { - writeFileSync(fileName, source, 'utf8') - }) + describe(`with program`, () => { + it('should compile js file for allowJs true with outDir', () => { + const fileName = `${baseFileName}-program-outDir.${baseFileExt}` + writeFileSync(fileName, source, 'utf8') + const compiler = makeCompiler({ + tsJestConfig: { + ...baseTsJestConfig, + incremental: false, + tsConfig: { + ...tsConfig, + outDir: '$$foo$$', + }, + }, + }) - afterAll(() => { - removeSync(fileName) - }) + const compiled = compiler.compile(source, fileName) - it('should compile js file for allowJs true with normal program', () => { - const compiler = makeCompiler({ - tsJestConfig: { ...baseTsJestConfig, incremental: false, tsConfig }, + expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot() + + removeSync(fileName) }) - const compiled = compiler.compile(source, fileName) + it('should compile js file for allowJs true without outDir', () => { + const fileName = `${baseFileName}-program-no-outDir.${baseFileExt}` + writeFileSync(fileName, source, 'utf8') + const compiler = makeCompiler({ + tsJestConfig: { ...baseTsJestConfig, incremental: false, tsConfig }, + }) - expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot() + const compiled = compiler.compile(source, fileName) + + expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot() + + removeSync(fileName) + }) }) - it('should compile js file for allowJs true with incremental program', () => { - const compiler = makeCompiler({ - tsJestConfig: { ...baseTsJestConfig, incremental: true, tsConfig }, + describe(`with incremental program`, () => { + it('should compile js file for allowJs true with outDir', () => { + const fileName = `${baseFileName}-incremental-outDir.${baseFileExt}` + writeFileSync(fileName, source, 'utf8') + const compiler = makeCompiler({ + tsJestConfig: { + ...baseTsJestConfig, + incremental: true, + tsConfig: { + ...tsConfig, + outDir: '$$foo$$', + }, + }, + }) + + const compiled = compiler.compile(source, fileName) + + expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot() + + removeSync(fileName) }) - const compiled = compiler.compile(source, fileName) + it('should compile js file for allowJs true without outDir', () => { + const fileName = `${baseFileName}-incremental-no-outDir.${baseFileExt}` + writeFileSync(fileName, source, 'utf8') + const compiler = makeCompiler({ + tsJestConfig: { ...baseTsJestConfig, incremental: true, tsConfig }, + }) - expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot() + const compiled = compiler.compile(source, fileName) + + expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot() + + removeSync(fileName) + }) }) }) diff --git a/src/compiler/transpile-module.spec.ts b/src/compiler/transpile-module.spec.ts index 67e5d0cb71..98fb4b4048 100644 --- a/src/compiler/transpile-module.spec.ts +++ b/src/compiler/transpile-module.spec.ts @@ -4,6 +4,7 @@ import { removeSync, writeFileSync } from 'fs-extra' import { makeCompiler } from '../__helpers__/fakers' import { logTargetMock } from '../__helpers__/mocks' import ProcessedSource from '../__helpers__/processed-source' +import { TS_JEST_OUT_DIR } from '../config/config-set' const logTarget = logTargetMock() @@ -42,7 +43,7 @@ describe('transpile module with isolatedModule: true', () => { it('should compile js file for allowJs true', () => { const fileName = `${__filename}.test.js` const compiler = makeCompiler({ - tsJestConfig: { ...baseTsJestConfig, tsConfig: { allowJs: true, outDir: '$$ts-jest$$' } }, + tsJestConfig: { ...baseTsJestConfig, tsConfig: { allowJs: true, outDir: TS_JEST_OUT_DIR } }, }) const source = 'export default 42' diff --git a/src/config/config-set.spec.ts b/src/config/config-set.spec.ts index 5f99046a1c..8835b0a2cf 100644 --- a/src/config/config-set.spec.ts +++ b/src/config/config-set.spec.ts @@ -15,7 +15,7 @@ import { getPackageVersion } from '../util/get-package-version' import { normalizeSlashes } from '../util/normalize-slashes' import { mocked } from '../util/testing' -import { ConfigSet, IGNORE_DIAGNOSTIC_CODES, MATCH_NOTHING } from './config-set' +import { ConfigSet, IGNORE_DIAGNOSTIC_CODES, MATCH_NOTHING, TS_JEST_OUT_DIR } from './config-set' jest.mock('../util/backports') jest.mock('../index') @@ -462,6 +462,10 @@ describe('typescript', () => { }) }) + it('should include default outDir $$ts-jest$$ when allowJs is enabled and no outDir from config', () => { + expect(get(void 0, { 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 276bdeb9df..3f79c61152 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -70,6 +70,10 @@ export const IGNORE_DIAGNOSTIC_CODES = [ 18002, // "The 'files' list in config file is empty." 18003, // "No inputs were found in config file." ] +/** + * @internal + */ +export const TS_JEST_OUT_DIR = '$$ts-jest$$' /** * @internal @@ -584,7 +588,7 @@ export class ConfigSet { inlineSources: true, // we don't want to create declaration files declaration: false, - noEmit: false, + noEmit: false, // set to true will make compiler API not emit any compiled results. // else istanbul related will be dropped removeComments: false, // to clear out else it's buggy @@ -778,6 +782,10 @@ 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)) {