Skip to content

Commit

Permalink
fix(config): improve emit skipped error message and ensure outDir a…
Browse files Browse the repository at this point in the history
…lways `TS_JEST_OUT_DIR` (#2357)
  • Loading branch information
ahnpnl authored Feb 13, 2021
1 parent 1965e20 commit f2808bb
Show file tree
Hide file tree
Showing 21 changed files with 20 additions and 258 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"compilerOptions": {
"target": "es5",
"allowJs": true,
"outDir": "$$ts-jest$$"
"outDir": "./build"
}
}
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.

26 changes: 3 additions & 23 deletions e2e/__tests__/allow-js.test.ts
Original file line number Diff line number Diff line change
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: { testMatch: null, 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',
testMatch: null,
Expand All @@ -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()
})
})
})
20 changes: 1 addition & 19 deletions src/compiler/__snapshots__/language-service.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -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 });
Expand Down
13 changes: 2 additions & 11 deletions src/compiler/language-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/language-service.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/compiler/transpiler.spec.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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'

Expand Down
15 changes: 7 additions & 8 deletions src/config/config-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 2 additions & 4 deletions src/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
}

/**
Expand Down

0 comments on commit f2808bb

Please sign in to comment.