Skip to content

fix(ts-config): remove dependency on ts-jest and configure tsconfif w… #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions e2e/__projects__/tsconfig/config/base-tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"module": "ES2015",
"isolatedModules": false,
"experimentalDecorators": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"suppressImplicitAnyIndexErrors": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true
}
}
37 changes: 37 additions & 0 deletions e2e/__projects__/tsconfig/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "babel-in-package",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"private": true,
"scripts": {
"test": "jest --no-cache test.js"
},
"dependencies": {
"vue": "^2.5.21",
"vue-template-compiler": "^2.5.21"
},
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.3",
"@vue/test-utils": "^1.0.0-beta.28",
"jest": "^24.0.0",
"vue-jest": "file:../../../"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"vue"
],
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.vue$": "vue-jest"
}
},
"babel": {
"presets": [
"@babel/env"
]
}
}
54 changes: 54 additions & 0 deletions e2e/__projects__/tsconfig/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { getTSConfig } from 'vue-jest/lib/utils'
Copy link
Member

@eddyerburgh eddyerburgh Feb 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This directory is intended for e2e tests

import ts from 'typescript'

describe('tsconfig options', () => {
test('should extend the tsconfig when extends options is used', () => {
const { compilerOptions } = getTSConfig()
expect(compilerOptions.strictNullChecks).toBeTruthy()
expect(compilerOptions.module).toBe(ts.ModuleKind.ES2015)
})

test('should read tsconfig from root when not assigned', () => {
const { compilerOptions } = getTSConfig()
expect(compilerOptions.strictNullChecks).toBeTruthy()
expect(compilerOptions.module).toBe(ts.ModuleKind.ES2015)
})

test('should read tsconfig from ts-jest when used in jest config', () => {
const { compilerOptions } = getTSConfig({
globals: {
'ts-jest': {
tsConfig: './tsconfig.json'
}
}
})
expect(compilerOptions.outDir).toBe('$$ts-jest$$')
})

test('should read from tsConfig object when specified in vue-jest config', () => {
const { compilerOptions } = getTSConfig({
globals: {
'vue-jest': {
tsConfig: {
compilerOptions: {
target: 'ES2016'
}
}
}
}
})
expect(compilerOptions.target).toBe(ts.ScriptTarget[ts.ScriptTarget.ES2016])
})

test('should read user configured file path when specified in vue-jest config', () => {
const { compilerOptions } = getTSConfig({
globals: {
'vue-jest': {
tsConfig: './config/base-tsconfig.json'
}
}
})
expect(compilerOptions.module).toBe(ts.ModuleKind.ES2015)
expect(compilerOptions.moduleResolution).toBeUndefined()
})
})
9 changes: 9 additions & 0 deletions e2e/__projects__/tsconfig/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./config/base-tsconfig.json",
"compilerOptions": {
"target": "ES2017",
"lib": ["dom", "es6"],
"moduleResolution": "node",
"types": ["vue-typescript-import-dts", "node"]
}
}
4 changes: 2 additions & 2 deletions lib/typescript-transformer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const ensureRequire = require('./ensure-require')
const babelJest = require('babel-jest')
const getBabelOptions = require('./utils').getBabelOptions
const getTsJestConfig = require('./utils').getTsJestConfig
const getTSConfig = require('./utils').getTSConfig
const stripInlineSourceMap = require('./utils').stripInlineSourceMap
const getCustomTransformer = require('./utils').getCustomTransformer
const getVueJestConfig = require('./utils').getVueJestConfig
Expand All @@ -11,7 +11,7 @@ module.exports = {
ensureRequire('typescript', ['typescript'])
const typescript = require('typescript')
const vueJestConfig = getVueJestConfig(config)
const tsconfig = getTsJestConfig(config)
const tsconfig = getTSConfig(config)
const babelOptions = getBabelOptions(filePath)

const res = typescript.transpileModule(scriptContent, tsconfig)
Expand Down
48 changes: 43 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const loadPartialConfig = require('@babel/core').loadPartialConfig
const createTransformer = require('ts-jest').createTransformer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will still error if users don't have TypeScript installed, can you move the require to inside the getTSConfig function

const chalk = require('chalk')
const fs = require('fs')
const ts = require('typescript')
const path = require('path')

const fetchTransformer = function fetchTransformer(key, obj) {
Expand Down Expand Up @@ -62,10 +64,46 @@ const getBabelOptions = function loadBabelOptions(filename, options = {}) {
return loadPartialConfig(opts).options
}

const getTsJestConfig = function getTsJestConfig(config) {
const tr = createTransformer()
const { typescript } = tr.configsFor(config)
return { compilerOptions: typescript.options }
const getTSConfig = function getTSConfig(config = {}) {
const vueTsConfig = getVueJestConfig(config)
if (typeof vueTsConfig.tsConfig === 'string') {
return parseTSConfigFile(vueTsConfig.tsConfig)
} else if (typeof vueTsConfig.tsConfig === 'object') {
return vueTsConfig.tsConfig
} else if (config.globals && config.globals['ts-jest']) {
const tr = createTransformer()
const {
typescript: { options: compilerOptions }
} = tr.configsFor(config)
return { compilerOptions }
} else {
// auto pick tsconfig from the root folder
return parseTSConfigFile()
}
}

const parseTSConfigFile = function parseTSConfigFile(
pathToConfig = path.resolve(process.cwd(), './tsconfig.json')
) {
if (!fs.existsSync(pathToConfig)) {
return {
compilerOptions: {}
}
}
const configJson = ts.parseConfigFileTextToJson(
pathToConfig,
ts.sys.readFile(pathToConfig)
)
const { options: compilerOptions } = ts.parseJsonConfigFileContent(
configJson.config,
ts.sys,
ts.getDirectoryPath(pathToConfig),
{},
pathToConfig
)
return {
compilerOptions
}
}

function isValidTransformer(transformer) {
Expand Down Expand Up @@ -119,7 +157,7 @@ module.exports = {
throwError,
logResultErrors,
getCustomTransformer,
getTsJestConfig,
getTSConfig,
getBabelOptions,
getVueJestConfig,
transformContent,
Expand Down