Created
March 10, 2023 21:32
-
-
Save merlosy/67b73cf342aa9d29cd522d0b6cd7c662 to your computer and use it in GitHub Desktop.
Jest SVG transformer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path'); | |
const jest = require('jest'); | |
/** | |
* Object props is a more extensible solution when we have a lot of props | |
*/ | |
function buildModule({ pathname }) { | |
return ` | |
module.exports = { | |
__esModule: true, | |
default: '${pathname}' | |
};`; | |
}; | |
/** | |
* @see https://stackoverflow.com/questions/58603201/jest-cannot-load-svg-file | |
* @see https://jestjs.io/docs/28.x/upgrading-to-jest28#transformer | |
* @see https://jestjs.io/docs/28.x/code-transformation#writing-custom-transformers | |
*/ | |
module.exports = { | |
process(sourceText, sourcePath, options) { | |
const pathname = JSON.stringify(path.basename(sourcePath)); | |
let contents; | |
// todo: expose URL or SVG content? | |
// let baseContent = `module.exports = ${pathname};`; | |
let baseContent = sourceText; | |
if (sourcePath.match(/\.svg$/)) { | |
baseContent = buildModule({ pathname }); | |
} | |
// Adapt for > Jest 28 | |
const isJestGreaterThan28 = parseInt(jest.getVersion().split('.')[0]) >= 28; | |
if (isJestGreaterThan28) { | |
contents = { code: baseContent } | |
} else { | |
contents = baseContent; | |
} | |
return contents; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment