Skip to content

Commit

Permalink
fix(config): improve emit skipped error message (#2358)
Browse files Browse the repository at this point in the history
Closes #2350
  • Loading branch information
ahnpnl committed Feb 14, 2021
1 parent 4726b1f commit 2ae8df4
Show file tree
Hide file tree
Showing 19 changed files with 11 additions and 223 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 0 additions & 10 deletions e2e/__cases__/allow-js/without-outDir/bar.spec.ts

This file was deleted.

1 change: 0 additions & 1 deletion e2e/__cases__/allow-js/without-outDir/bar.ts

This file was deleted.

5 changes: 0 additions & 5 deletions e2e/__cases__/allow-js/without-outDir/esm.spec.js

This file was deleted.

1 change: 0 additions & 1 deletion e2e/__cases__/allow-js/without-outDir/foo.js

This file was deleted.

10 changes: 0 additions & 10 deletions e2e/__cases__/allow-js/without-outDir/foo.spec.js

This file was deleted.

6 changes: 0 additions & 6 deletions e2e/__cases__/allow-js/without-outDir/tsconfig.json

This file was deleted.

154 changes: 0 additions & 154 deletions e2e/__tests__/__snapshots__/allow-js.test.ts.snap

This file was deleted.

25 changes: 3 additions & 22 deletions e2e/__tests__/allow-js.test.ts
Expand Up @@ -2,21 +2,20 @@ 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$' },
})

testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { testLabel }) => {
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$',
Expand All @@ -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()
})
})
})
4 changes: 1 addition & 3 deletions src/compiler/ts-compiler.spec.ts
Expand Up @@ -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'

Expand All @@ -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'

Expand Down Expand Up @@ -189,7 +188,6 @@ const t: string = f(5)
...baseTsJestConfig,
tsconfig: {
allowJs: true,
outDir: TS_JEST_OUT_DIR,
},
astTransformers: {
before: ['dummy-transformer'],
Expand Down
4 changes: 2 additions & 2 deletions 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'
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 2 additions & 5 deletions src/config/config-set.spec.ts
Expand Up @@ -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' },
Expand Down
6 changes: 2 additions & 4 deletions src/config/config-set.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions src/utils/messages.ts
Expand Up @@ -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.",
}

/**
Expand Down

0 comments on commit 2ae8df4

Please sign in to comment.