Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/papermana/jest-alias-preprocessor
A preprocessor for Jest with support for webpack aliases
https://github.com/papermana/jest-alias-preprocessor
Last synced: 2 months ago
JSON representation
A preprocessor for Jest with support for webpack aliases
- Host: GitHub
- URL: https://github.com/papermana/jest-alias-preprocessor
- Owner: papermana
- License: mit
- Created: 2016-07-15T21:09:12.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-06-16T11:18:27.000Z (over 7 years ago)
- Last Synced: 2024-10-08T20:07:21.634Z (3 months ago)
- Language: JavaScript
- Size: 22.5 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE.md
Awesome Lists containing this project
README
# jest-alias-preprocessor
This package allows you to use aliases when testing with [Jest](https://github.com/facebook/jest). It's very handy when you want to use your existing [webpack](https://github.com/webpack/webpack) configuration, but you can also use it on its own.Really, it's just a very simple wrapper on top of [transform-jest-deps](https://github.com/Ticketmaster/transform-jest-deps).
## Usage
`jest-alias-preprocessor` exports a higher-order function; to make it work, you have to provide a configuration object for it to use. Simply create a new file, import this module, and either import `webpack.config.js`, or create a new object from scratch. Then, invoke the function, passing the config as an argument. Finally, link to the preprocessor from your Jest configuration using the `scriptPreprocessor` option.Note that your config has to look like a webpack config object even if you don't use webpack. It needs a `resolve` property, and that property needs to have a `root` and `alias` property. You can see how it looks below.
### Example 1 ― everything in root directory
```
const config = require('./webpack.config.js');module.exports = require('jest-alias-preprocessor')(config);
```### Example 2 ― no Webpack config
```
module.exports = require('jest-alias-preprocessor')({
resolve: {
root: process.cwd(),
alias: {
_js: './app/js',
_utils: './app/js/utils',
},
},
});
```### Example 3 ― with babel-jest
```
var config = require('./webpack.config.js');
var aliasPreprocessor = require('jest-alias-preprocessor')(config);
var babel = require('babel-jest');module.exports = {
process: function(src, path) {
src = aliasPreprocessor.process(src, path);
src = babel.process(src, path);return src;
},
};
```