Use EJS templates in your Jest tests.
yarn add jst-ejs-transformer
In your jest.config.js:
const { defaults: { moduleFileExtensions } } = require("jest-config")
module.exports = {
moduleFileExtensions: [ ...moduleFileExtensions, "ejs"],
transform: {
"\\.ejs$": "jest-ejs-transformer",
//"\\.js$": "babel-jest" (optional)
}
}
You can now import
or require
an EJS template as a module:
const Template = require("./template/ejs")
module.exports = {
render(props) {
return Template(props)
}
}
Special thanks to Matt Rose and Roman Loktev for the code behind this package. I just copied their code from StackOverflow and put it in a reusable module.