From e2a51b2873898dcfc0a1b03a49f48ce70ea70f0a Mon Sep 17 00:00:00 2001 From: Alexander von Weiss Date: Fri, 23 Apr 2021 12:42:07 +0200 Subject: [PATCH] feat(jest): support jest 27 transformerConfig --- packages/jest/index.ts | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/jest/index.ts b/packages/jest/index.ts index 05ba491d6..0861a0694 100644 --- a/packages/jest/index.ts +++ b/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() +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') @@ -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 }