Skip to content

Commit

Permalink
Merge pull request #396 from sod/jest-27
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Apr 23, 2021
2 parents 531dade + e2a51b2 commit a827408
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions packages/jest/index.ts
@@ -1,14 +1,34 @@
import { createHash } from 'crypto'

import { transformJest } from '@swc-node/core'
import { Options, transformJest } from '@swc-node/core'
import { Output } from '@swc/core'

const Cache = new Map<string, Output>()

interface JestConfig26 {
transform: [match: string, transformerPath: string, options: Options][]
}

interface JestConfig27 {
transformerConfig: Options
}

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

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

return {}
}

export = {
process(src: string, path: string, jestConfig: any) {
const [, , transformOptions = {}] =
(jestConfig.transform || []).find(([, transformerPath]: [string, string]) => transformerPath === __filename) || []
process(src: string, path: string, jestConfig: JestConfig26 | JestConfig27) {
if (/\.(t|j)sx?$/.test(path)) {
// sha1 is fast, and we don't care about security here
const cacheHash = createHash('sha1')
Expand All @@ -18,7 +38,7 @@ export = {
if (Cache.has(cacheKey)) {
return Cache.get(cacheKey)
}
const output = transformJest(src, path, transformOptions)
const output = transformJest(src, path, getJestTransformConfig(jestConfig))
Cache.set(cacheKey, output)
return output
}
Expand Down

0 comments on commit a827408

Please sign in to comment.