Skip to content

Commit

Permalink
feat(jest): replace babel-jest with @swc/jest for significantly i…
Browse files Browse the repository at this point in the history
…mproved performance
  • Loading branch information
wintercounter committed Dec 3, 2021
1 parent 5f42089 commit 656bee6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 deletions.
12 changes: 10 additions & 2 deletions src/configs/jest/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import { loadConfig } from '@/utils'
import mhyConfig from '@/configs/mhy'

const srcPath = path.join(process.cwd(), mhyConfig.srcFolder)
const aliases = Object.entries(mhyConfig.defaultAliases).reduce((acc, [key, value]) => {
value = `${value.replace(/\\/g, '/')}`
acc[`${key}$`] = value
acc[`${key}\/(.*)$`] = `${value}/$1`

return acc
}, {})

const jestConfig = loadConfig('jest', {
setupFilesAfterEnv: [
Expand All @@ -20,7 +27,7 @@ const jestConfig = loadConfig('jest', {
path.resolve(__dirname, '../../../node_modules')
],
transform: {
'^.+\\.[jt]sx?$': require.resolve('./preprocess')
'^.+\\.(t|j)sx?$': require.resolve('./preprocess')
},
transformIgnorePatterns: [],
bail: true,
Expand All @@ -30,7 +37,8 @@ const jestConfig = loadConfig('jest', {
moduleNameMapper: {
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': 'identity-obj-proxy',
'\\.(s?css|less)$': 'identity-obj-proxy',
'\\.(svgx?)$': path.resolve(__dirname, 'mocks/react-null.js')
'\\.(svgx?)$': path.resolve(__dirname, 'mocks/react-null.js'),
...aliases
},
collectCoverageFrom: ['**/*.js'],
watchPlugins: []
Expand Down
24 changes: 19 additions & 5 deletions src/configs/jest/preprocess.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import path from 'path'
import fs from 'fs'

const babelrc = JSON.parse(fs.readFileSync(path.resolve(__dirname, '.babelrc'), 'utf8'))
babelrc.presets.find(([name]) => name.includes('preset-env'))[1].modules = 'commonjs'
babelrc.plugins.push(require.resolve('babel-plugin-dynamic-import-node'))
babelrc.plugins.push(require.resolve('babel-plugin-add-module-exports'))
const transformer = require('babel-jest').default.createTransformer(babelrc)
const first100Characters = str => {
if (str.length > 100) {
return str.substring(0, 100) + '...'
}
return str
}

const swcrc = JSON.parse(fs.readFileSync(path.resolve(__dirname, '.swcrc'), 'utf8'))
swcrc.module = {
type: 'commonjs'
}
const transformer = require('@swc/jest').createTransformer(swcrc)
const proc = transformer.process.bind(transformer)
transformer.process = function (...args) {
const [code, path] = args
console.log(path)
if (!path.includes('node_modules')) {
console.log(path)
console.log(first100Characters(code).replace('\n', ''))
console.log(!path.includes('node_modules') || code.match(/^(import |export )|import\(/gm))
}

if (!path.includes('node_modules') || code.match(/^(import |export )|import\(/gm)) {
return proc(...args)
}
Expand Down
5 changes: 3 additions & 2 deletions src/configs/swc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ const swcConfig = loadConfig('swc', {
paths: tsConfig.paths,
baseUrl: tsConfig.baseUrl,
transform: null,
target: 'es2016',
target: 'es2021',
loose: false,
externalHelpers: false,
// Requires v1.2.50 or upper and requires target to be es2016 or upper.
keepClassNames: false
}
},
test: ['es6', 'es', 'jsx', 'js', 'mjs', 'ts', 'tsx', 'esm'].map(ext => `.*.${ext}$`)
})

export default swcConfig
3 changes: 2 additions & 1 deletion src/processes/ecosystem/root/jest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path'
import Process from '@/processes'
import FileTypes from '@/utils/fileTypes'

const getJestCLICmd = flags => [
'node',
Expand All @@ -13,7 +14,7 @@ const getJestCLICmd = flags => [
class Jest extends Process {
constructor(args) {
const jestDir = path.dirname(require.resolve('@/configs/jest'))
require('@/configs/babel/write')(jestDir)
require('@/configs/swc/write')(jestDir, FileTypes.JSON_NO_EXT, true)

const { props: { defaultAction = 'start' } = {}, ...rest } = args
super(args)
Expand Down
3 changes: 0 additions & 3 deletions src/processes/ecosystem/root/swc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ const getCmdSWCCLI = (flags = []) => [
['node_modules', 'test', 'tests', 'temp', 'tmp', '**/*.d.ts', mhyConfig.distFolder, mhyConfig.buildFolder].join(
','
),
'--delete-dir-on-start',
'--extensions',
'.js,.jsx,.ts,.tsx',
...flags
]

Expand Down
1 change: 0 additions & 1 deletion src/resources/nodeProcessSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ global.mhy = mhyConfig
// Guard against poorly mocked module constructors
const Module = module.constructor.length > 1 ? module.constructor : BuiltinModule

swcConfig.extensions = ['.es6', '.es', '.jsx', '.js', '.mjs', '.ts', '.tsx']
swcConfig.module = {
type: 'commonjs'
}
Expand Down

0 comments on commit 656bee6

Please sign in to comment.