From c9a2a53ccda555793defeda4fe1856ec93de37be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Wed, 24 May 2023 10:18:15 +0800 Subject: [PATCH] fix: jest 28 compatable --- src/transformers/fileTransformer.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/transformers/fileTransformer.ts b/src/transformers/fileTransformer.ts index 29ead99..6c7216a 100644 --- a/src/transformers/fileTransformer.ts +++ b/src/transformers/fileTransformer.ts @@ -1,11 +1,15 @@ import path from "path"; +// Jest 28 has breaking change of this. +const jestPkg = require('jest/package.json'); +const aboveJest28 = parseFloat(jestPkg.version) >= 28; + export default { process(src: string, filename: string) { const assetFilename = JSON.stringify(path.basename(filename)); if (filename.match(/\.svg$/)) { - return `module.exports = { + const code = `module.exports = { __esModule: true, default: ${assetFilename}, ReactComponent: ({ svgRef, ...props }) => ({ @@ -18,8 +22,11 @@ export default { }) }), };`; + + return aboveJest28 ? { code } : code; } - return `module.exports = ${assetFilename};`; + const rawCode = `module.exports = ${assetFilename};`; + return aboveJest28 ? { code: rawCode } : rawCode; }, };