Skip to content

Commit f2808bb

Browse files
authored
fix(config): improve emit skipped error message and ensure outDir always TS_JEST_OUT_DIR (#2357)
1 parent 1965e20 commit f2808bb

21 files changed

+20
-258
lines changed

e2e/__cases__/allow-js/with-outDir/tsconfig.json renamed to e2e/__cases__/allow-js/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"compilerOptions": {
33
"target": "es5",
44
"allowJs": true,
5-
"outDir": "$$ts-jest$$"
5+
"outDir": "./build"
66
}
77
}

e2e/__cases__/allow-js/without-outDir/bar.spec.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

e2e/__cases__/allow-js/without-outDir/bar.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

e2e/__cases__/allow-js/without-outDir/esm.spec.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

e2e/__cases__/allow-js/without-outDir/foo.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

e2e/__cases__/allow-js/without-outDir/foo.spec.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

e2e/__cases__/allow-js/without-outDir/tsconfig.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

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

Lines changed: 0 additions & 154 deletions
This file was deleted.

e2e/__tests__/allow-js.test.ts

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ 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/with-outDir', {
5+
const testCase = configureTestCase('allow-js', {
66
jestConfig: { testMatch: null, testRegex: '(foo|bar)\\.spec\\.[jt]s$' },
77
})
88

99
testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { testLabel }) => {
1010
it(testLabel, () => {
1111
const result = runTest()
1212
expect(result.status).toBe(0)
13-
expect(result).toMatchSnapshot()
1413
})
1514
})
1615
})
1716

18-
describe('using ts-jest for js files with outDir', () => {
19-
const testCase = configureTestCase('allow-js/with-outDir', {
17+
describe('using ts-jest for js files', () => {
18+
const testCase = configureTestCase('allow-js', {
2019
jestConfig: {
2120
preset: 'ts-jest/presets/js-with-ts',
2221
testMatch: null,
@@ -28,25 +27,6 @@ describe('using ts-jest for js files with outDir', () => {
2827
it(testLabel, () => {
2928
const result = runTest()
3029
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', {
38-
jestConfig: {
39-
preset: 'ts-jest/presets/js-with-ts',
40-
testMatch: null,
41-
testRegex: 'esm\\.spec\\.[jt]s$',
42-
},
43-
})
44-
45-
testCase.runWithTemplates(allPackageSetsWithPreset, 0, (runTest, { testLabel }) => {
46-
it(testLabel, () => {
47-
const result = runTest()
48-
expect(result.status).toBe(0)
49-
expect(result).toMatchSnapshot()
5030
})
5131
})
5232
})

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Language service allowJs option should compile js file for allowJs true with outDir 1`] = `
4-
===[ FILE: test-allow-js.js ]===================================================
5-
"use strict";
6-
Object.defineProperty(exports, "__esModule", { value: true });
7-
exports.default = 42;
8-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvdy1qcy5qcyIsIm1hcHBpbmdzIjoiOztBQUFBLGtCQUFlLEVBQUUsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWFsbG93LWpzLmpzIl0sInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBkZWZhdWx0IDQyIl0sInZlcnNpb24iOjN9
9-
===[ INLINE SOURCE MAPS ]=======================================================
10-
file: test-allow-js.js
11-
mappings: ;;AAAA,kBAAe,EAAE,CAAA
12-
names: []
13-
sources:
14-
- test-allow-js.js
15-
sourcesContent:
16-
- export default 42
17-
version: 3
18-
================================================================================
19-
`;
20-
21-
exports[`Language service allowJs option should compile js file for allowJs true without outDir 1`] = `
3+
exports[`Language service allowJs option should compile js file for allowJs true 1`] = `
224
===[ FILE: test-allow-js.js ]===================================================
235
"use strict";
246
Object.defineProperty(exports, "__esModule", { value: true });

src/compiler/language-service.spec.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,11 @@ describe('Language service', () => {
2020
const fileName = 'test-allow-js.js'
2121
const source = 'export default 42'
2222

23-
it('should compile js file for allowJs true with outDir', () => {
24-
const compiler = makeCompiler({
25-
tsJestConfig: { tsconfig: { allowJs: true, outDir: '$$foo$$' } },
26-
})
27-
28-
const compiled = compiler.compile(source, fileName)
29-
30-
expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot()
31-
})
32-
33-
it('should compile js file for allowJs true without outDir', () => {
23+
it('should compile js file for allowJs true', () => {
3424
const compiler = makeCompiler({
3525
tsJestConfig: { tsconfig: { allowJs: true } },
3626
})
27+
3728
const compiled = compiler.compile(source, fileName)
3829

3930
expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot()

src/compiler/language-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { LogContexts, Logger, LogLevels } from 'bs-logger'
22
import { existsSync, readFileSync, writeFile } from 'fs'
3-
import { basename, normalize, relative, join } from 'path'
3+
import { basename, normalize, join } from 'path'
44
import memoize = require('lodash/memoize')
55
import mkdirp = require('mkdirp')
66
import type * as _ts from 'typescript'
@@ -260,7 +260,7 @@ export const initializeLanguageServiceInstance = (configs: ConfigSet, logger: Lo
260260
}
261261
/* istanbul ignore next (this should never happen but is kept for security) */
262262
if (output.emitSkipped) {
263-
throw new TypeError(`${relative(cwd, fileName)}: Emit skipped for language service`)
263+
throw new Error(interpolate(Errors.CannotCompile, { file: fileName }))
264264
}
265265
// Throw an error when requiring `.d.ts` files.
266266
if (!output.outputFiles.length) {

src/compiler/transpiler.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { makeCompiler } from '../__helpers__/fakers'
22
import ProcessedSource from '../__helpers__/processed-source'
3-
import { TS_JEST_OUT_DIR } from '../config/config-set'
43

54
describe('Transpiler', () => {
65
const baseTsJestConfig = {
@@ -10,7 +9,7 @@ describe('Transpiler', () => {
109
it('should compile js file for allowJs true', () => {
1110
const fileName = 'foo.js'
1211
const compiler = makeCompiler({
13-
tsJestConfig: { ...baseTsJestConfig, tsconfig: { allowJs: true, outDir: TS_JEST_OUT_DIR } },
12+
tsJestConfig: { ...baseTsJestConfig, tsconfig: { allowJs: true } },
1413
})
1514
const source = 'export default 42'
1615

src/config/config-set.spec.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,13 @@ describe('parsedTsConfig', () => {
6969
})
7070

7171
it('should override some options', () => {
72-
expect(get({ tsconfig: { module: 'esnext' as any, inlineSources: false } }).options).toMatchObject({
73-
module: ts.ModuleKind.CommonJS,
74-
inlineSources: true,
75-
})
76-
})
77-
78-
it('should include default outDir $$ts-jest$$ when allowJs is enabled and no outDir from config', () => {
79-
expect(get({ tsconfig: { allowJs: true } }).options.outDir).toBe(TS_JEST_OUT_DIR)
72+
expect(get({ tsconfig: { module: 'esnext' as any, inlineSources: false, outDir: 'build' } }).options).toMatchObject(
73+
{
74+
module: ts.ModuleKind.CommonJS,
75+
inlineSources: true,
76+
outDir: TS_JEST_OUT_DIR,
77+
},
78+
)
8079
})
8180

8281
it('should be able to read extends', () => {

src/config/config-set.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ export class ConfigSet {
164164
// to clear out else it's buggy
165165
out: undefined,
166166
outFile: undefined,
167+
// ensure that `LanguageService` won't pick up things from `build` folder which can lead to emit skipped error
168+
outDir: TS_JEST_OUT_DIR,
167169
composite: undefined, // see https://github.com/TypeStrong/ts-node/pull/657/files
168170
declarationDir: undefined,
169171
declarationMap: undefined,
@@ -483,10 +485,6 @@ export class ConfigSet {
483485
finalOptions.allowSyntheticDefaultImports = true
484486
}
485487
}
486-
// Make sure when allowJs is enabled, outDir is required to have when using allowJs: true
487-
if (finalOptions.allowJs && !finalOptions.outDir) {
488-
finalOptions.outDir = TS_JEST_OUT_DIR
489-
}
490488

491489
// ensure undefined are removed and other values are overridden
492490
for (const key of Object.keys(forcedOptions)) {

src/utils/messages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export const enum Errors {
1616
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.',
1717
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.',
1818
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.',
19-
UnableToFindProjectRoot = 'Unable to find the root of the project where ts-jest has been installed.',
2019
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',
20+
CannotCompile = "Unable to process '{{file}}'. Please check your tsconfig.",
2121
}
2222

2323
/**

0 commit comments

Comments
 (0)