Jest transformer for Embedded Ruby (`.erb`) files in Ruby projects
npm install jest-erb-transformer   
Custom transformer for compiling Embedded Ruby template files (``.erb`) for use in the Jest JavaScript testing framework.
Add to project using your package manager, e.g. yarn:
`sh`
yarn add jest-erb-transformer --dev
Ensure the `"erb"` file extension and the jest-erb-transformer extension matcher and configuration is included in the project's `package.json` jest key:
`json`
"moduleFileExtensions": [
"js",
"erb"
],
`json`
"transform": {
"^.+\\.erb$": "jest-erb-transformer"
}
See the Jest docs for more information on the configuration of transformers.
To add custom configuration, such as using the Ruby on Rails runner for ERB compilation, add a configuration object to the transformer entry in the project `package.json` using the Jest transformer configuration syntax. For example, to compile in the rails environment:
`json`
"transform": {
"^.+\\.erb$": [ "jest-erb-transformer", { "application": "rails" } ]
}
#### Babel
By default the transformer does not process its output with Babel.
To perform post-processing with Babel, either configure the transformer `babelConfig` option as outlined in the options table below, or I have found including the `.erb` files in the babel-jest transformer directly has worked in some scenarios. For example:
`json`
"transform": {
"^.+\\.js(?:\\.erb)?$": "babel-jest",
"^.+\\.erb$": [ "jest-erb-transformer", { "application": "rails" } ]
}
#### All Options
| Key | Default Value | Description |
| :--- | :--- | :--- |
| `"application"` | `"ruby"` | Transformer is run using `ruby` by default, set value to `"rails"` to use `bin/rails runner`. The `"rails"` option can be useful if the .erb files include Ruby on Rails specific environment variables such as `Rails.application.credentials`. |`
| "engine"` | `"erb"` | Transformer uses the ruby 'ERB' engine by default, to use the Erubi engine set the value to `"erubi"`. |`
| "timeout"` | `"5000"` | Set the timeout duration in milliseconds for the compilation of individual files. |`
| "babelConfig"` | `false` | Process the output of the ERB transformer using babel-jest. `
Options: false` - Disables the use of Babel.`true` - Babel processing using project or file-relative Babel configuration file.`./path/to/.babelrc.json` - Relative path to Babel configuration file to be used for processing.`{ "comments": false }`` - Object containing Babel options to be used for processing. |
Feel free to ask questions using issues or contribute if you have any improvements.
MIT