Skip to content

Commit 1a287ad

Browse files
authored
fix(config): set default outDir when allowJs is true and no outDir in tsconfig (#1502)
1 parent bb8d84c commit 1a287ad

21 files changed

+294
-46
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as foo from './foo'
2+
import bar = require('./bar')
3+
4+
test('foo', () => {
5+
expect(foo).toBe('FOO!')
6+
})
7+
8+
test('bar', () => {
9+
expect(bar).toBe('BAR!')
10+
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export = 'BAR!'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as bar from './bar'
2+
3+
test('esm', () => {
4+
expect(bar).toBe('BAR!')
5+
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'FOO!'
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const foo = require('./foo')
2+
const bar = require('./bar')
3+
4+
test('foo', () => {
5+
expect(foo).toBe('FOO!')
6+
})
7+
8+
test('bar', () => {
9+
expect(bar).toBe('BAR!')
10+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"allowJs": true
5+
}
6+
}

e2e/__tests__/__snapshots__/allow-js.test.ts.snap

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ exports[`using babel-jest for js files should pass using template "with-typescri
6868
================================================================================
6969
`;
7070

71-
exports[`using ts-jest for js files should pass using template "default" 1`] = `
71+
exports[`using ts-jest for js files with outDir should pass using template "default" 1`] = `
7272
√ jest
7373
↳ exit code: 0
7474
===[ STDOUT ]===================================================================
@@ -85,7 +85,7 @@ exports[`using ts-jest for js files should pass using template "default" 1`] = `
8585
================================================================================
8686
`;
8787

88-
exports[`using ts-jest for js files should pass using template "with-babel-7" 1`] = `
88+
exports[`using ts-jest for js files with outDir should pass using template "with-babel-7" 1`] = `
8989
√ jest
9090
↳ exit code: 0
9191
===[ STDOUT ]===================================================================
@@ -102,7 +102,7 @@ exports[`using ts-jest for js files should pass using template "with-babel-7" 1`
102102
================================================================================
103103
`;
104104

105-
exports[`using ts-jest for js files should pass using template "with-babel-7-string-config" 1`] = `
105+
exports[`using ts-jest for js files with outDir should pass using template "with-babel-7-string-config" 1`] = `
106106
√ jest
107107
↳ exit code: 0
108108
===[ STDOUT ]===================================================================
@@ -119,7 +119,75 @@ exports[`using ts-jest for js files should pass using template "with-babel-7-str
119119
================================================================================
120120
`;
121121

122-
exports[`using ts-jest for js files should pass using template "with-typescript-2-7" 1`] = `
122+
exports[`using ts-jest for js files with outDir should pass using template "with-typescript-2-7" 1`] = `
123+
√ jest
124+
↳ exit code: 0
125+
===[ STDOUT ]===================================================================
126+
127+
===[ STDERR ]===================================================================
128+
PASS ./esm.spec.js
129+
√ esm
130+
131+
Test Suites: 1 passed, 1 total
132+
Tests: 1 passed, 1 total
133+
Snapshots: 0 total
134+
Time: XXs
135+
Ran all test suites.
136+
================================================================================
137+
`;
138+
139+
exports[`using ts-jest for js files without outDir should pass using template "default" 1`] = `
140+
√ jest
141+
↳ exit code: 0
142+
===[ STDOUT ]===================================================================
143+
144+
===[ STDERR ]===================================================================
145+
PASS ./esm.spec.js
146+
√ esm
147+
148+
Test Suites: 1 passed, 1 total
149+
Tests: 1 passed, 1 total
150+
Snapshots: 0 total
151+
Time: XXs
152+
Ran all test suites.
153+
================================================================================
154+
`;
155+
156+
exports[`using ts-jest for js files without outDir should pass using template "with-babel-7" 1`] = `
157+
√ jest
158+
↳ exit code: 0
159+
===[ STDOUT ]===================================================================
160+
161+
===[ STDERR ]===================================================================
162+
PASS ./esm.spec.js
163+
√ esm
164+
165+
Test Suites: 1 passed, 1 total
166+
Tests: 1 passed, 1 total
167+
Snapshots: 0 total
168+
Time: XXs
169+
Ran all test suites.
170+
================================================================================
171+
`;
172+
173+
exports[`using ts-jest for js files without outDir should pass using template "with-babel-7-string-config" 1`] = `
174+
√ jest
175+
↳ exit code: 0
176+
===[ STDOUT ]===================================================================
177+
178+
===[ STDERR ]===================================================================
179+
PASS ./esm.spec.js
180+
√ esm
181+
182+
Test Suites: 1 passed, 1 total
183+
Tests: 1 passed, 1 total
184+
Snapshots: 0 total
185+
Time: XXs
186+
Ran all test suites.
187+
================================================================================
188+
`;
189+
190+
exports[`using ts-jest for js files without outDir should pass using template "with-typescript-2-7" 1`] = `
123191
√ jest
124192
↳ exit code: 0
125193
===[ STDOUT ]===================================================================

e2e/__tests__/allow-js.test.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { allPackageSetsWithPreset, allValidPackageSets } from '../__helpers__/te
22
import { configureTestCase } from '../__helpers__/test-case'
33

44
describe('using babel-jest for js files', () => {
5-
const testCase = configureTestCase('allow-js', {
5+
const testCase = configureTestCase('allow-js/with-outDir', {
66
jestConfig: { testMatch: null, testRegex: '(foo|bar)\\.spec\\.[jt]s$' },
77
})
88

@@ -15,8 +15,26 @@ describe('using babel-jest for js files', () => {
1515
})
1616
})
1717

18-
describe('using ts-jest for js files', () => {
19-
const testCase = configureTestCase('allow-js', {
18+
describe('using ts-jest for js files with outDir', () => {
19+
const testCase = configureTestCase('allow-js/with-outDir', {
20+
jestConfig: {
21+
preset: 'ts-jest/presets/js-with-ts',
22+
testMatch: null,
23+
testRegex: 'esm\\.spec\\.[jt]s$',
24+
},
25+
})
26+
27+
testCase.runWithTemplates(allPackageSetsWithPreset, 0, (runTest, { testLabel }) => {
28+
it(testLabel, () => {
29+
const result = runTest()
30+
expect(result.status).toBe(0)
31+
expect(result).toMatchSnapshot()
32+
})
33+
})
34+
})
35+
36+
describe('using ts-jest for js files without outDir', () => {
37+
const testCase = configureTestCase('allow-js/without-outDir', {
2038
jestConfig: {
2139
preset: 'ts-jest/presets/js-with-ts',
2240
testMatch: null,

src/compiler/__snapshots__/language-service.spec.ts.snap

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`language service should compile js file for allowJs true 1`] = `
4-
===[ FILE: test-allow-js.js ]===================================================
3+
exports[`language service should compile js file for allowJs true with outDir 1`] = `
4+
===[ FILE: test-allow-js-with-outDir.js ]=======================================
55
"use strict";
66
Object.defineProperty(exports, "__esModule", { value: true });
77
exports.default = 42;
8-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvdy1qcy5qcyIsIm1hcHBpbmdzIjoiOztBQUFBLGtCQUFlLEVBQUUsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWFsbG93LWpzLmpzIl0sInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBkZWZhdWx0IDQyIl0sInZlcnNpb24iOjN9
8+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvdy1qcy13aXRoLW91dERpci5qcyIsIm1hcHBpbmdzIjoiOztBQUFBLGtCQUFlLEVBQUUsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWFsbG93LWpzLXdpdGgtb3V0RGlyLmpzIl0sInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBkZWZhdWx0IDQyIl0sInZlcnNpb24iOjN9
99
===[ INLINE SOURCE MAPS ]=======================================================
10-
file: test-allow-js.js
10+
file: test-allow-js-with-outDir.js
1111
mappings: ';;AAAA,kBAAe,EAAE,CAAA'
1212
names: []
1313
sources:
14-
- test-allow-js.js
14+
- test-allow-js-with-outDir.js
15+
sourcesContent:
16+
- export default 42
17+
version: 3
18+
================================================================================
19+
`;
20+
21+
exports[`language service should compile js file for allowJs true without outDir 1`] = `
22+
===[ FILE: test-allow-js-no-outDir.js ]=========================================
23+
"use strict";
24+
Object.defineProperty(exports, "__esModule", { value: true });
25+
exports.default = 42;
26+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvdy1qcy1uby1vdXREaXIuanMiLCJtYXBwaW5ncyI6Ijs7QUFBQSxrQkFBZSxFQUFFLENBQUEiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsidGVzdC1hbGxvdy1qcy1uby1vdXREaXIuanMiXSwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IGRlZmF1bHQgNDIiXSwidmVyc2lvbiI6M30=
27+
===[ INLINE SOURCE MAPS ]=======================================================
28+
file: test-allow-js-no-outDir.js
29+
mappings: ';;AAAA,kBAAe,EAAE,CAAA'
30+
names: []
31+
sources:
32+
- test-allow-js-no-outDir.js
1533
sourcesContent:
1634
- export default 42
1735
version: 3

src/compiler/__snapshots__/program.spec.ts.snap

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,71 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`allowJs should compile js file for allowJs true with incremental program 1`] = `
4-
===[ FILE: test-allowJs.test.js ]===============================================
3+
exports[`allowJs with incremental program should compile js file for allowJs true with outDir 1`] = `
4+
===[ FILE: test-allowJs-incremental-outDir.test.js ]============================
55
"use strict";
66
Object.defineProperty(exports, "__esModule", { value: true });
77
exports.default = 42;
8-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvd0pzLnRlc3QuanMiLCJtYXBwaW5ncyI6Ijs7QUFBQSxrQkFBZSxFQUFFLENBQUEiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsidGVzdC1hbGxvd0pzLnRlc3QuanMiXSwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IGRlZmF1bHQgNDIiXSwidmVyc2lvbiI6M30=
8+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvd0pzLWluY3JlbWVudGFsLW91dERpci50ZXN0LmpzIiwibWFwcGluZ3MiOiI7O0FBQUEsa0JBQWUsRUFBRSxDQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbInRlc3QtYWxsb3dKcy1pbmNyZW1lbnRhbC1vdXREaXIudGVzdC5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgZGVmYXVsdCA0MiJdLCJ2ZXJzaW9uIjozfQ==
99
===[ INLINE SOURCE MAPS ]=======================================================
10-
file: test-allowJs.test.js
10+
file: test-allowJs-incremental-outDir.test.js
1111
mappings: ';;AAAA,kBAAe,EAAE,CAAA'
1212
names: []
1313
sources:
14-
- test-allowJs.test.js
14+
- test-allowJs-incremental-outDir.test.js
1515
sourcesContent:
1616
- export default 42
1717
version: 3
1818
================================================================================
1919
`;
2020

21-
exports[`allowJs should compile js file for allowJs true with normal program 1`] = `
22-
===[ FILE: test-allowJs.test.js ]===============================================
21+
exports[`allowJs with incremental program should compile js file for allowJs true without outDir 1`] = `
22+
===[ FILE: test-allowJs-incremental-no-outDir.test.js ]=========================
2323
"use strict";
2424
Object.defineProperty(exports, "__esModule", { value: true });
2525
exports.default = 42;
26-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvd0pzLnRlc3QuanMiLCJtYXBwaW5ncyI6Ijs7QUFBQSxrQkFBZSxFQUFFLENBQUEiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsidGVzdC1hbGxvd0pzLnRlc3QuanMiXSwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IGRlZmF1bHQgNDIiXSwidmVyc2lvbiI6M30=
26+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvd0pzLWluY3JlbWVudGFsLW5vLW91dERpci50ZXN0LmpzIiwibWFwcGluZ3MiOiI7O0FBQUEsa0JBQWUsRUFBRSxDQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbInRlc3QtYWxsb3dKcy1pbmNyZW1lbnRhbC1uby1vdXREaXIudGVzdC5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgZGVmYXVsdCA0MiJdLCJ2ZXJzaW9uIjozfQ==
2727
===[ INLINE SOURCE MAPS ]=======================================================
28-
file: test-allowJs.test.js
28+
file: test-allowJs-incremental-no-outDir.test.js
2929
mappings: ';;AAAA,kBAAe,EAAE,CAAA'
3030
names: []
3131
sources:
32-
- test-allowJs.test.js
32+
- test-allowJs-incremental-no-outDir.test.js
33+
sourcesContent:
34+
- export default 42
35+
version: 3
36+
================================================================================
37+
`;
38+
39+
exports[`allowJs with program should compile js file for allowJs true with outDir 1`] = `
40+
===[ FILE: test-allowJs-program-outDir.test.js ]================================
41+
"use strict";
42+
Object.defineProperty(exports, "__esModule", { value: true });
43+
exports.default = 42;
44+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvd0pzLXByb2dyYW0tb3V0RGlyLnRlc3QuanMiLCJtYXBwaW5ncyI6Ijs7QUFBQSxrQkFBZSxFQUFFLENBQUEiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsidGVzdC1hbGxvd0pzLXByb2dyYW0tb3V0RGlyLnRlc3QuanMiXSwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IGRlZmF1bHQgNDIiXSwidmVyc2lvbiI6M30=
45+
===[ INLINE SOURCE MAPS ]=======================================================
46+
file: test-allowJs-program-outDir.test.js
47+
mappings: ';;AAAA,kBAAe,EAAE,CAAA'
48+
names: []
49+
sources:
50+
- test-allowJs-program-outDir.test.js
51+
sourcesContent:
52+
- export default 42
53+
version: 3
54+
================================================================================
55+
`;
56+
57+
exports[`allowJs with program should compile js file for allowJs true without outDir 1`] = `
58+
===[ FILE: test-allowJs-program-no-outDir.test.js ]=============================
59+
"use strict";
60+
Object.defineProperty(exports, "__esModule", { value: true });
61+
exports.default = 42;
62+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvd0pzLXByb2dyYW0tbm8tb3V0RGlyLnRlc3QuanMiLCJtYXBwaW5ncyI6Ijs7QUFBQSxrQkFBZSxFQUFFLENBQUEiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsidGVzdC1hbGxvd0pzLXByb2dyYW0tbm8tb3V0RGlyLnRlc3QuanMiXSwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IGRlZmF1bHQgNDIiXSwidmVyc2lvbiI6M30=
63+
===[ INLINE SOURCE MAPS ]=======================================================
64+
file: test-allowJs-program-no-outDir.test.js
65+
mappings: ';;AAAA,kBAAe,EAAE,CAAA'
66+
names: []
67+
sources:
68+
- test-allowJs-program-no-outDir.test.js
3369
sourcesContent:
3470
- export default 42
3571
version: 3

src/compiler/language-service.spec.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,25 @@ describe('language service', () => {
6060
removeSync(fileName)
6161
})
6262

63-
it('should compile js file for allowJs true', () => {
64-
const fileName = `test-allow-js.js`
63+
it('should compile js file for allowJs true with outDir', () => {
64+
const fileName = `test-allow-js-with-outDir.js`
6565
const compiler = makeCompiler({
66-
tsJestConfig: { tsConfig: { allowJs: true, outDir: '$$ts-jest$$' } },
66+
tsJestConfig: { tsConfig: { allowJs: true, outDir: '$$foo$$' } },
67+
})
68+
const source = 'export default 42'
69+
70+
writeFileSync(fileName, source, 'utf8')
71+
const compiled = compiler.compile(source, fileName)
72+
73+
expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot()
74+
75+
removeSync(fileName)
76+
})
77+
78+
it('should compile js file for allowJs true without outDir', () => {
79+
const fileName = `test-allow-js-no-outDir.js`
80+
const compiler = makeCompiler({
81+
tsJestConfig: { tsConfig: { allowJs: true } },
6782
})
6883
const source = 'export default 42'
6984

0 commit comments

Comments
 (0)