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 all commits
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 __mocks__/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
}
}
9 changes: 9 additions & 0 deletions __mocks__/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"]
}
}
44 changes: 44 additions & 0 deletions __tests__/utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { getTSConfig } from '../lib/utils'
import { resolve } from 'path'
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 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: resolve(__dirname, '../__mocks__/config/base-tsconfig.json')
}
}
})
expect(compilerOptions.module).toBe(ts.ModuleKind.ES2015)
expect(compilerOptions.moduleResolution).toBeUndefined()
})
})
8 changes: 0 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const crypto = require('crypto')
const babelJest = require('babel-jest')
const tsJest = require('ts-jest')

module.exports = {
process: require('./process'),
Expand All @@ -20,13 +19,6 @@ module.exports = {
}),
'hex'
)
.update(
tsJest.getCacheKey(fileData, filename, configString, {
instrument,
rootDir
}),
'hex'
)
.digest('hex')
}
}
9 changes: 4 additions & 5 deletions lib/typescript-transformer.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
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
const getTSCInstance = require('./utils').getTSCInstance

module.exports = {
process(scriptContent, filePath, config) {
ensureRequire('typescript', ['typescript'])
const typescript = require('typescript')
const typescript = getTSCInstance()
const vueJestConfig = getVueJestConfig(config)
const tsconfig = getTsJestConfig(config)
const tsconfig = getTSConfig(config)
const babelOptions = getBabelOptions(filePath)

const res = typescript.transpileModule(scriptContent, tsconfig)
Expand Down
52 changes: 45 additions & 7 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const loadPartialConfig = require('@babel/core').loadPartialConfig
const createTransformer = require('ts-jest').createTransformer
const chalk = require('chalk')
const fs = require('fs')
const ensureRequire = require('./ensure-require')
const path = require('path')

const fetchTransformer = function fetchTransformer(key, obj) {
Expand Down Expand Up @@ -62,10 +63,41 @@ 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 {
// auto pick tsconfig from the root folder
return parseTSConfigFile()
}
}

const parseTSConfigFile = function parseTSConfigFile(
pathToConfig = path.resolve(process.cwd(), './tsconfig.json')
) {
const ts = getTSCInstance()
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 @@ -114,17 +146,23 @@ const logResultErrors = result => {
}
}

const getTSCInstance = function getTSCInstance() {
ensureRequire('typescript', ['typescript'])
return require('typescript')
}

module.exports = {
stripInlineSourceMap,
throwError,
logResultErrors,
getCustomTransformer,
getTsJestConfig,
getTSConfig,
getBabelOptions,
getVueJestConfig,
transformContent,
info,
warn,
resolvePath,
fetchTransformer
fetchTransformer,
getTSCInstance
}
30 changes: 26 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
"lint": "eslint --ignore-path .gitignore '{,!(node_modules)/**/}*.js'",
"lint:fix": "npm run lint --fix",
"release": "semantic-release",
"test": "npm run lint && npm run format:check && npm run test:e2e",
"test:e2e": "node e2e/test-runner"
"test": "npm run lint && npm run format:check && npm run test:e2e && npm run test:unit",
"test:e2e": "node e2e/test-runner",
"test:unit": "jest --no-cache --coverage"
},
"author": "Edd Yerburgh",
"license": "MIT",
Expand Down Expand Up @@ -71,8 +72,7 @@
"@vue/component-compiler-utils": "^2.4.0",
"chalk": "^2.1.0",
"extract-from-css": "^0.4.4",
"source-map": "^0.5.6",
"ts-jest": "^23.10.5"
"source-map": "^0.5.6"
},
"repository": {
"type": "git",
Expand All @@ -88,5 +88,27 @@
"npm run format",
"git add"
]
},
"jest": {
"testPathIgnorePatterns": [
"<rootDir>/e2e"
],
"coveragePathIgnorePatterns": [
"<rootDir>/e2e",
"<rootDir>/__mocks__"
],
"moduleFileExtensions": [
"js",
"json",
"vue"
],
"transform": {
"^.+\\.js$": "babel-jest"
}
},
"babel": {
"presets": [
"@babel/env"
]
}
}
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./__mocks__/tsconfig.json"
}
46 changes: 6 additions & 40 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1428,13 +1428,6 @@ browserslist@^4.3.4:
electron-to-chromium "^1.3.92"
node-releases "^1.1.1"

[email protected]:
version "0.2.6"
resolved "https://registry.yarnpkg.com/bs-logger/-/bs-logger-0.2.6.tgz#eb7d365307a72cf974cc6cda76b68354ad336bd8"
integrity sha512-pd8DCoxmbgc7hyPKOvxtqNcjYoOsABPQdcCUjGp3d42VR2CX1ORhk2A87oqqu5R1kk+76nsxZupkmyd+MVtCog==
dependencies:
fast-json-stable-stringify "2.x"

bser@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/bser/-/bser-2.0.0.tgz#9ac78d3ed5d915804fd87acb158bc797147a1719"
Expand All @@ -1447,7 +1440,7 @@ btoa-lite@^1.0.0:
resolved "https://registry.yarnpkg.com/btoa-lite/-/btoa-lite-1.0.0.tgz#337766da15801210fdd956c22e9c6891ab9d0337"
integrity sha1-M3dm2hWAEhD92VbCLpxokaudAzc=

buffer-from@1.x, buffer-from@^1.0.0:
buffer-from@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==
Expand Down Expand Up @@ -3125,7 +3118,7 @@ fast-glob@^2.0.2:
merge2 "^1.2.3"
micromatch "^3.1.10"

fast-json-stable-stringify@2.x, fast-json-stable-stringify@^2.0.0:
fast-json-stable-stringify@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz#d5142c0caee6b1189f87d3a76111064f86c8bbf2"
integrity sha1-1RQsDK7msRifh9OnYREGT4bIu/I=
Expand Down Expand Up @@ -4991,7 +4984,7 @@ json-stringify-safe@^5.0.1, json-stringify-safe@~5.0.1:
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
integrity sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=

json5@2.x, json5@^2.1.0:
json5@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/json5/-/json5-2.1.0.tgz#e7a0c62c48285c628d20a10b85c89bb807c32850"
integrity sha512-8Mh9h6xViijj36g7Dxi+Y4S6hNGV96vcJZr/SrlHh1LR/pEn/8j/+qIBbs44YKl69Lrfctp4QD+AdWLTMqEZAQ==
Expand Down Expand Up @@ -5507,11 +5500,6 @@ make-dir@^1.0.0, make-dir@^1.3.0:
dependencies:
pify "^3.0.0"

[email protected]:
version "1.3.5"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.5.tgz#efe4e81f6db28cadd605c70f29c831b58ef776c8"
integrity sha512-c3sIjNUow0+8swNwVpqoH4YCShKNFkMaw6oH1mNS2haDZQqkeZFlHS3dhoeEbKKmJB4vXpJucU6oH75aDYeE9g==

"make-fetch-happen@^2.5.0 || 3 || 4", make-fetch-happen@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/make-fetch-happen/-/make-fetch-happen-4.0.1.tgz#141497cb878f243ba93136c83d8aba12c216c083"
Expand Down Expand Up @@ -5836,7 +5824,7 @@ mixin-deep@^1.2.0:
for-in "^1.0.2"
is-extendable "^1.0.1"

[email protected], [email protected], "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkdirp@~0.5.1:
[email protected], "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkdirp@~0.5.1:
version "0.5.1"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903"
integrity sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=
Expand Down Expand Up @@ -7561,7 +7549,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b"
integrity sha1-IDEU2CrSxe2ejgQRs5ModeiJ6Xs=

resolve@1.x, resolve@^1.1.6, resolve@^1.3.2, resolve@^1.5.0, resolve@^1.6.0, resolve@^1.8.1:
resolve@^1.1.6, resolve@^1.3.2, resolve@^1.5.0, resolve@^1.6.0, resolve@^1.8.1:
version "1.9.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.9.0.tgz#a14c6fdfa8f92a7df1d996cb7105fa744658ea06"
integrity sha512-TZNye00tI67lwYvzxCxHGjwTNlUV70io54/Ed4j6PscB8xVfuBJpRenI/o6dVk0cY0PYTY27AgCoGGxRnYuItQ==
Expand Down Expand Up @@ -7754,7 +7742,7 @@ semver-regex@^2.0.0:
resolved "https://registry.yarnpkg.com/semver-regex/-/semver-regex-2.0.0.tgz#a93c2c5844539a770233379107b38c7b4ac9d338"
integrity sha512-mUdIBBvdn0PLOeP3TEkMH7HHeUP3GjsXCwKarjv/kGmUFOYg1VqEemKhoQpWMu6X2I8kHeuVdGibLGkVK+/5Qw==

"semver@2 >=2.2.1 || 3.x || 4 || 5", "semver@2 || 3 || 4 || 5", "[email protected] || 3.x || 4 || 5", "semver@^2.3.0 || 3.x || 4 || 5", semver@^5.0.3, semver@^5.1.0, semver@^5.3.0, semver@^5.4.1, semver@^5.5, semver@^5.5.0, semver@^5.5.1:
"semver@2 >=2.2.1 || 3.x || 4 || 5", "semver@2 || 3 || 4 || 5", "[email protected] || 3.x || 4 || 5", "semver@^2.3.0 || 3.x || 4 || 5", semver@^5.0.3, semver@^5.1.0, semver@^5.3.0, semver@^5.4.1, semver@^5.5.0, semver@^5.5.1:
version "5.6.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.6.0.tgz#7e74256fbaa49c75aa7c7a205cc22799cac80004"
integrity sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg==
Expand Down Expand Up @@ -8530,21 +8518,6 @@ trim-right@^1.0.1:
dependencies:
glob "^7.1.2"

ts-jest@^23.10.5:
version "23.10.5"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-23.10.5.tgz#cdb550df4466a30489bf70ba867615799f388dd5"
integrity sha512-MRCs9qnGoyKgFc8adDEntAOP64fWK1vZKnOYU1o2HxaqjdJvGqmkLCPCnVq1/If4zkUmEjKPnCiUisTrlX2p2A==
dependencies:
bs-logger "0.x"
buffer-from "1.x"
fast-json-stable-stringify "2.x"
json5 "2.x"
make-error "1.x"
mkdirp "0.x"
resolve "1.x"
semver "^5.5"
yargs-parser "10.x"

tslib@^1.9.0:
version "1.9.3"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.3.tgz#d7e4dd79245d85428c4d7e4822a79917954ca286"
Expand Down Expand Up @@ -9074,13 +9047,6 @@ yallist@^3.0.0, yallist@^3.0.2:
resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.3.tgz#b4b049e314be545e3ce802236d6cd22cd91c3de9"
integrity sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A==

[email protected]:
version "10.1.0"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8"
integrity sha512-VCIyR1wJoEBZUqk5PA+oOBF6ypbwh5aNB3I50guxAL/quggdfs4TtNHQrSazFA3fYZ+tEqfs0zIGlv0c/rgjbQ==
dependencies:
camelcase "^4.1.0"

yargs-parser@^11.1.1:
version "11.1.1"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-11.1.1.tgz#879a0865973bca9f6bab5cbdf3b1c67ec7d3bcf4"
Expand Down