Skip to content

Commit

Permalink
feat: use ts-jest for TypeScript config resolution (#139)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: removes tsConfig options
  • Loading branch information
eddyerburgh authored Jan 5, 2019
1 parent 9888245 commit 842467b
Show file tree
Hide file tree
Showing 30 changed files with 1,980 additions and 1,296 deletions.
6 changes: 1 addition & 5 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
{
"env": {
"test": {
"presets": ["env"]
}
}
"presets": ["@babel/env"]
}
5 changes: 0 additions & 5 deletions lib/cache.js

This file was deleted.

12 changes: 7 additions & 5 deletions lib/compilers/coffee-compiler.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
const ensureRequire = require('../ensure-require.js')
const throwError = require('../throw-error')
const loadBabelConfig = require('../load-babel-config.js')
const throwError = require('../utils').throwError
const getBabelOptions = require('../utils').getBabelOptions

module.exports = function(raw, config, filePath) {
module.exports = function(src, filename, config) {
ensureRequire('coffee', ['coffeescript'])
const coffee = require('coffeescript')
const babelOptions = getBabelOptions(filename)
let compiled
try {
compiled = coffee.compile(raw, {
compiled = coffee.compile(src, {
filename,
bare: true,
sourceMap: true,
transpile: loadBabelConfig(config, filePath)
transpile: babelOptions
})
} catch (err) {
throwError(err)
Expand Down
15 changes: 0 additions & 15 deletions lib/compilers/helpers/local-resolve-helper.js

This file was deleted.

16 changes: 15 additions & 1 deletion lib/compilers/helpers/module-name-mapper-helper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
const localResolve = require('./local-resolve-helper')
const path = require('path')

/**
* Resolves the path to the file locally.
*
* @param {String} to - the name of the file to resolve to
* @param {String} localPath - the local path
* @returns {String} path - path to the file to import
*/
function localResolve(to, localPath) {
if (localPath.startsWith('/')) {
return localPath
}
return path.join(path.dirname(to), localPath)
}

/**
* Applies the moduleNameMapper substitution from the jest config
Expand Down
8 changes: 4 additions & 4 deletions lib/compilers/sass-compiler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ensureRequire = require('../ensure-require')
const getVueJestConfig = require('../get-vue-jest-config')
const logger = require('../logger')
const getVueJestConfig = require('../utils').getVueJestConfig
const warn = require('../utils').warn

const applyModuleNameMapper = require('./helpers/module-name-mapper-helper')

Expand Down Expand Up @@ -35,10 +35,10 @@ module.exports = (content, filePath, jestConfig = {}) => {
.css.toString()
} catch (err) {
if (!vueJestConfig.hideStyleWarn) {
logger.warn(
warn(
`There was an error rendering the SASS in ${filePath}. SASS is fully supported by vue-jest. Still some features might throw errors. Webpack aliases are a common cause of errors. If you use Webpack aliases, please use jest's suggested way via moduleNameMapper which is supported.`
)
logger.warn(`Error while compiling styles: ${err}`)
warn(`Error while compiling styles: ${err}`)
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/compilers/scss-compiler.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const path = require('path')
const fs = require('fs')
const ensureRequire = require('../ensure-require')
const getVueJestConfig = require('../get-vue-jest-config')
const logger = require('../logger')
const getVueJestConfig = require('../utils').getVueJestConfig
const warn = require('../utils').warn

const applyModuleNameMapper = require('./helpers/module-name-mapper-helper')

Expand Down Expand Up @@ -45,10 +45,10 @@ module.exports = (content, filePath, jestConfig = {}) => {
.css.toString()
} catch (err) {
if (!vueJestConfig.hideStyleWarn) {
logger.warn(
warn(
`There was an error rendering the SCSS in ${filePath}. SCSS is fully supported by vue-jest. Still some features might throw errors. Webpack aliases are a common cause of errors. If you use Webpack aliases, please use jest's suggested way via moduleNameMapper which is supported.`
)
logger.warn(`Error while compiling styles: ${err}`)
warn(`Error while compiling styles: ${err}`)
}
}

Expand Down
33 changes: 17 additions & 16 deletions lib/compilers/typescript-compiler.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
const ensureRequire = require('../ensure-require')
const loadBabelConfig = require('../load-babel-config.js')
const { loadTypescriptConfig } = require('../load-typescript-config')
const babelJest = require('babel-jest')
const getVueJestConfig = require('../get-vue-jest-config')
const getBabelOptions = require('../utils').getBabelOptions
const getTsJestConfig = require('../utils').getTsJestConfig
const stripInlineSourceMap = require('../utils').stripInlineSourceMap

module.exports = function compileTypescript(scriptContent, filePath, config) {
ensureRequire('typescript', ['typescript'])
const vueJestConfig = getVueJestConfig(config)
const typescript = require('typescript')
const tsConfig = loadTypescriptConfig(vueJestConfig)

const res = typescript.transpileModule(scriptContent, tsConfig)
const { tsconfig } = getTsJestConfig(config)
const babelOptions = getBabelOptions(filePath)

const res = typescript.transpileModule(scriptContent, tsconfig)

res.outputText = stripInlineSourceMap(res.outputText)

const inputSourceMap =
res.sourceMapText !== undefined ? JSON.parse(res.sourceMapText) : ''

// handle ES modules in TS source code in case user uses non commonjs module
// output and there is no .babelrc.
let inlineBabelConfig = {}
if (
tsConfig.compilerOptions.module !== 'commonjs' &&
!loadBabelConfig(vueJestConfig)
) {
inlineBabelConfig = {
plugins: [require('babel-plugin-transform-es2015-modules-commonjs')]
let inlineBabelOptions = {}
if (tsconfig.compilerOptions.module !== 'commonjs' && !babelOptions) {
inlineBabelOptions = {
plugins: [require('@babel/plugin-transform-modules-commonjs')]
}
}

const transformer = babelJest.createTransformer(
Object.assign(inlineBabelConfig, {
inputSourceMap
})
Object.assign(inlineBabelOptions, { inputSourceMap })
)

return transformer.process(res.outputText, filePath, config)
}
24 changes: 0 additions & 24 deletions lib/deprecate.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/ensure-require.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const throwError = require('./throw-error')
const throwError = require('./utils').throwError

module.exports = function(name, deps) {
let i, len
Expand Down
20 changes: 0 additions & 20 deletions lib/get-cache-key.js

This file was deleted.

11 changes: 0 additions & 11 deletions lib/get-vue-jest-config.js

This file was deleted.

64 changes: 0 additions & 64 deletions lib/load-babel-config.js

This file was deleted.

77 changes: 0 additions & 77 deletions lib/load-typescript-config.js

This file was deleted.

9 changes: 0 additions & 9 deletions lib/logger.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/process-style.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const getVueJestConfig = require('./get-vue-jest-config')
const getVueJestConfig = require('./utils').getVueJestConfig
const cssExtract = require('extract-from-css')

module.exports = function processStyle(stylePart, filePath, jestConfig = {}) {
Expand Down
Loading

0 comments on commit 842467b

Please sign in to comment.