Skip to content
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

Added tsconfig option to configuration #851

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions packages/jest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = {

// configuration
{
tsconfig: '<rootDir>/tsconfig-test.json',
dynamicImport: true,
react: {
pragma: 'h',
Expand Down
30 changes: 19 additions & 11 deletions packages/jest/index.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,46 @@
import { xxh64 } from '@node-rs/xxhash'
import type { Output } from '@swc/core'
import type { TransformOptions } from '@jest/transform'
import { Options, transformJest } from '@swc-node/core'
import { readDefaultTsConfig, tsCompilerOptionsToSwcConfig } from '@swc-node/register/read-default-tsconfig'

interface JestConfig26 {
transform: [match: string, transformerPath: string, options: Options][]
interface TransformerConfig extends Options {
tsconfig?: string
}

interface JestConfig27 {
transformerConfig: Options
interface JestConfig26 extends TransformOptions {
transform: [match: string, transformerPath: string, options: TransformerConfig][]
}

function getJestTransformConfig(jestConfig: JestConfig26 | JestConfig27): Options {
type JestConfig27 = TransformOptions<TransformerConfig>

function getJestTransformConfig(jestConfig: JestConfig26 | JestConfig27): TransformerConfig {
if ('transformerConfig' in jestConfig) {
// jest 27
return jestConfig.transformerConfig
return (jestConfig as JestConfig27).transformerConfig
}

if ('transform' in jestConfig) {
// jest 26
return jestConfig.transform.find(([, transformerPath]) => transformerPath === __filename)?.[2] ?? {}
return (
(jestConfig as JestConfig26).transform.find(([, transformerPath]) => transformerPath === __filename)?.[2] ?? {}
)
}

return {}
}

const defaultTsConfig = readDefaultTsConfig()

export = {
process(src: string, path: string, jestConfig: JestConfig26 | JestConfig27): Output | string {
if (/\.(tsx?|jsx?|mjs)$/.test(path)) {
const transformerConfig = getJestTransformConfig(jestConfig)
const tsconfig = transformerConfig.tsconfig
? transformerConfig.tsconfig.replace('<rootDir>', jestConfig.config.rootDir)
: undefined
const tsConfigObject = readDefaultTsConfig(tsconfig)
return transformJest(src, path, {
...tsCompilerOptionsToSwcConfig(defaultTsConfig, path),
...getJestTransformConfig(jestConfig),
...tsCompilerOptionsToSwcConfig(tsConfigObject, path),
...transformerConfig,
})
}
return src
Expand Down
3 changes: 2 additions & 1 deletion packages/jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
},
"devDependencies": {
"@swc/core": "^1.6.6",
"@swc/types": "^0.1.9"
"@swc/types": "^0.1.9",
"@jest/transform": "^29.7.0"
},
"repository": {
"type": "git",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading