Skip to content

Commit

Permalink
fix(jest-config): Removing @swc/core from dependencies
Browse files Browse the repository at this point in the history
downgrade @swc/core requirements due to TypeStrong/ts-node#2070
update config generation
  • Loading branch information
aklesky committed Oct 1, 2023
1 parent 4cdc33a commit ce27beb
Show file tree
Hide file tree
Showing 11 changed files with 1,655 additions and 1,434 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -20,5 +20,5 @@ update:
npx ncu -ws --root -u
clean-build: clear build
clear:
npx -y rimraf node_modules
npx -y rimraf -rf node_modules
npx -y lerna run clean
9 changes: 6 additions & 3 deletions packages/jest-config/package.json
Expand Up @@ -42,12 +42,15 @@
"devDependencies": {
"@aklesky/eslint-config": "0.7.3",
"@aklesky/prettier-config": "0.7.3",
"@aklesky/ts-config": "0.7.3"
"@aklesky/ts-config": "0.7.3",
"@swc/core": "1.3.82",
"@types/jest": "29.5.5"
},
"peerDependencies": {
"@swc/core": ">=1.3.82"
},
"dependencies": {
"@swc/core": "1.3.90",
"@swc/jest": "0.2.29",
"@types/jest": "29.5.5",
"jest": "29.7.0"
}
}
16 changes: 16 additions & 0 deletions packages/jest-config/src/base.ts
@@ -0,0 +1,16 @@
import type { Config } from 'jest'
import { defaultEsmExtensions, defaultTestMatch, defaultTimeout } from './constants'

export const base: Partial<Config> = {
collectCoverage: true,
testEnvironment: 'node',
extensionsToTreatAsEsm: defaultEsmExtensions,
testMatch: defaultTestMatch,
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
testTimeout: defaultTimeout,
openHandlesTimeout: defaultTimeout,
}

export default base
34 changes: 8 additions & 26 deletions packages/jest-config/src/config.ts
@@ -1,29 +1,11 @@
import { Config } from 'jest'
import type { Config } from 'jest'
import base from './base'

const base: Partial<Config> = {
collectCoverage: true,
testEnvironment: 'node',
extensionsToTreatAsEsm: ['.ts', '.tsx'],
testMatch: ['**/__tests__/**/?(*.)+(spec|test).[jt]s?(x)'],
transform: {
'^.+\\.(t|j)sx?$': [
'@swc/jest',
{
jsc: {
transform: {
react: {
runtime: 'automatic',
},
},
},
},
],
},
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
testTimeout: 30000,
openHandlesTimeout: 3000,
export const defineConfig = (config: Partial<Config>): Partial<Config> => {
return {
...base,
...(config || {}),
}
}

export = base
export default defineConfig
3 changes: 3 additions & 0 deletions packages/jest-config/src/constants.ts
@@ -0,0 +1,3 @@
export const defaultTestMatch = ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)']
export const defaultEsmExtensions = ['.ts', '.tsx']
export const defaultTimeout = 30000
7 changes: 4 additions & 3 deletions packages/jest-config/src/index.ts
@@ -1,3 +1,4 @@
import * as base from './config'

export = base
export * from './config.js'
export * from './swc.js'
export * from './constants.js'
export * from './interfaces.js'
2 changes: 2 additions & 0 deletions packages/jest-config/src/interfaces.ts
@@ -0,0 +1,2 @@
export type * as SWC from '@swc/core'
export type * as Jest from 'jest'
Empty file.
32 changes: 32 additions & 0 deletions packages/jest-config/src/swc.ts
@@ -0,0 +1,32 @@
import { Config as JestConfig } from 'jest'
import type { Config as SWCConfig } from '@swc/core'
import { getRootSwc, defineJestTransform } from './utils'

const swc = getRootSwc()

export const withSWC = (config: Partial<SWCConfig>, jest: Partial<JestConfig>): JestConfig => {
return {
...jest,
...defineJestTransform('@swc/jest', defineSwcConfiguration(config)),
}
}

export const defineSwcConfiguration = (config: Partial<SWCConfig>): Partial<SWCConfig> => {
const options = {
...(swc || {}),
...(config || {}),
}
return {
...(options || {}),
jsc: {
...(options.jsc || {}),
transform: {
...(options.jsc?.transform || {}),
react: {
runtime: 'automatic',
...(options.jsc?.transform?.react || {}),
},
},
},
}
}
34 changes: 34 additions & 0 deletions packages/jest-config/src/utils.ts
@@ -0,0 +1,34 @@
import type { Config as SWCConfig } from '@swc/core'
import type { Config as JestConfig } from 'jest'
import { resolve } from 'path'
import fs from 'node:fs'

export const getRootSwc = () => {
try {
const root = process.cwd()
const path = resolve(root, '.swcrc')
if (!fs.existsSync(path)) {
return {}
}
const content = fs.readFileSync(path, 'utf-8')
return JSON.parse(content) as Partial<SWCConfig>
} catch (e) {
return {}
}
}

export const defineJestTransform = (
name: string = 'babel-jest',
options?: Record<string, unknown>,
): Partial<JestConfig> => {
return {
transform: {
'^.+\\.(t|j)sx?$': [
name,
{
...(options || {}),
},
],
},
}
}

0 comments on commit ce27beb

Please sign in to comment.