Preprocessors
Note that you don’t need a preprocessor to use Babel or Webpack with wallaby.js. You may read how to use the listed technologies in the corresponding docs sections.
Wallaby.js supports Karma-like preprocessors to transform the contents of your files before feeding them to the test runner.
Wallaby preprocessors are extremely simple – you specify a function or an array of functions to run for each file which satisfy a given pattern:
or
The preprocessor function may take one or two arguments (making it sync or async):
In case of one argument the preprocessor result is expected immediately. In case of two arguments the preprocessor is async and the second argument is the callback that is invoked when the preprocessor result is ready.
The first argument is a file
object.
The file.content
property contains the current content of the file.
The file.path
property contains the current path to the file.
The file.rename(newPath)
function allows you to rename/move the file.
The file.changeExt(newExt)
function is just a shortcut for the rename
function allowing you to change the file extension. For example, file.changeExt('js')
will change the file
extension from whatever it is to .js
.
For example, the following preprocessor runs a react tools transformation to all jsx
files and renames them to *.jsx.js
files.
The result of a preprocessor can be:
- a string: a modified content of the file,
- an object: with
code
andmap
(orsourceMap
) properties, wherecode
is the file modified content andmap
is the source map of the transformation. - an error: error object of
Error
type (passed todone
callback or thrown from sync preprocessor).
Please note that if the transformation produces a JavaScript file that you are interested to see the coverage for, then returning the source map is essential. Otherwise, the reported coverage and error stacks may not be correct.
Writing a custom preprocessor
If your preprocessor is more complex than a simple inline function, you may want to consider creating a separate module and also publishing it. Have a look at wallaby-ng-html2js-preprocessor for an example.