mwolson / jest-webpack-alias Goto Github PK
View Code? Open in Web Editor NEWPreprocessor for Jest that is able to resolve require() statements using webpack aliases
License: MIT License
Preprocessor for Jest that is able to resolve require() statements using webpack aliases
License: MIT License
Hi all,
As I using this lib, found it doesn't support ES6 syntax.
It's good if we can support that.
It looks like Jest does not use jest-webpack-alias when resolving jest.unmock()
module paths. e.g.
// webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'build'),
filename: 'bundle.js',
},
resolve: {
root: path.resolve(__dirname, 'src'),
},
module: {
loaders: [
{
test: /\.js$/,
include: path.resolve(__dirname, 'src'),
loader: 'babel-loader',
},
],
},
};
// src/actions/index.js
export function foo() {
return { type: 'FOO' };
}
Notice jest.unmock('..')
vs jest.unmock('actions')
:
// src/actions/__tests__/index-test.js
// This succeeds:
// jest.unmock('..');
// But this fails:
jest.unmock('actions');
import { foo } from 'actions';
describe('foo', () => {
it('creates a FOO action', () => {
expect(foo()).toEqual({ type: 'FOO' });
});
});
Would this be an issue with jest-webpack-alias or with jest itself?
When webpack.config.js
exports a function
, jest-webpack-alias throws the following error:
● Test suite failed to run
Missing setting "resolve.modules" (webpack v2) or "resolve.root" (webpack v1) in /Users/davidcalhoun/Sites/web/gn-web/webpack.config.js
at Object.read (node_modules/jest-webpack-alias/lib/webpackInfo.js:38:11)
at ensureWebpackInfo (node_modules/jest-webpack-alias/lib/preprocessor.js:26:33)
at Object.process (node_modules/jest-webpack-alias/lib/preprocessor.js:165:3)
at Object.process (test/preprocessor.js:9:45)
The reason I am attempting to export a function
is to setup environment-specific configs utilizing webpack-merge. Is it possible to support exporting a function
?
My issues appear to be similar to #15.
It is common to write webpack config on ES6 (webpack makes it possible if config name simply has *.babel.js
in the tail).
jest-webpack-alias doesn't support ES6 syntax within given webpack config.
Here is my configuration:
// package.json
...
jest-webpack-alias": {
"configFile": "webpack.config.babel.js"
}
...
// webpack.config.babel.js
import webpack from 'webpack';
...
The resolver should be able to handle aliases that reference absolute paths. For instance, given the following resolver config:
resolve: {
root: '/srv/app',
alias: {
helpers: '/srv/helpers'
}
}
and given the file, /srv/helpers/logger.js
exists,
The statement, require('helpers/logger');
, in a file at /srv/app/index.js
should be transformed to require('../helpers/logger.js');
.
Hey
I use a webpack alias to load my custom config for my reactjs app.
alias: {
config: path.join(__dirname, 'config', `${process.env.NODE_ENV || 'development'}`)
}
When running my tests with jest-webpack-alias enabled, I get this error :
> npm run test
> jest
Error: Unknown config option "jest-webpack-alias" with value "[object Object]". This is either a typing error or another user mistake and fixing it will remove this message.
Using Jest CLI v12.1.1, jasmine2
I tried to rename my alias because I thought this might be a reserved name. But even after renaming or deleting the alias the error still gets thrown.
Hope you can help
If you have a path containing "node_modules" in resolve.root, dependencies should still get resolved rather than ignored.
The resolver code should be moved into a separate npm module, so that it's easier to use in a potential Babel plugin.
The alias config below works ok with webpack
but jest-webpack-alias
doesn't work properly.
resolve: {
modules: [path.resolve(__dirname, '..'), 'node_modules'],
alias: {
'@app/core': '../apps/core',
'@app/views': '../apps/components/views'
}
}
jest-webpack-alias
works after I change it to @app-core
instead of @app/core
, It looks a little tricky.
Could you please help to support slash symbol /
in the alias.
Thanks
Not totally sure if this is a problem with this lib, but maybe someone can point me in the right direction.
I have the preprocessor/config setup like the readme and a .babelrc that works, but I still get this error when trying to run tests:
- SyntaxError: Unexpected token export
I am pretty sure it fails because of this statement in a file that I am importing:
export const VERSION = __VERSION__;
I had to get https://www.npmjs.com/package/babel-plugin-add-module-exports to get my imports/exports working properly with babel 6 and am wondering if I have to add anything to the preprocessor to get it working too?
Hi. when I try run test with 'jest-webpack-alias', he has error
● Runtime Error
SyntaxError: Unexpected reserved word
This error in file webpackInfo.js on line 17 var webpackSettings = require(webpackFile);
I don't understand why this happening. Help please.
Hi, I am not sure if this is a proper place for this question, but I have a component which imports Stylus file. I use Css Modules approach. When I try to test this component with Jest I get an error: SyntaxError: Unexpected character '@ (3:0)'
or if I remove 'imports' I get: Unexpected token (6:0)
etc so it looks like Jest will not support style imports at all.
So I ended with something like:
var babelJest = require('babel-jest');
var webpackAlias = require('jest-webpack-alias');
module.exports = {
process: function(src, filename) {
// ignore styles and images imports
if ((/\.(gif|jpg|jpeg|tiff|png|css|styl|less|scss)$/i).test(path.basename(filename))) {
return '';
}
if (filename.indexOf('node_modules') === -1) {
src = babelJest.process(src, filename);
src = webpackAlias.process(src, filename);
}
return src;
}
};
Isn't it a little bit hacky? :/ Can we make it work in some other way?
I have webpack set up to use coffeescript in my project. Should coffeescript then work for jest testing?
I created an example repo to show what happens. https://github.com/tylercollier/jest-with-webpack
$ npm test __tests__/MyCoffeeComponent.coffee
> [email protected] test /home/tylercollier/repos/jest-with-webpack
> jest "__tests__/MyCoffeeComponent.coffee"
Using Jest CLI v0.8.2, jasmine1
FAIL __tests__/MyCoffeeComponent.coffee
● Runtime Error
SyntaxError: Unexpected token (1:22)
npm ERR! Test failed. See above for more details.
Maybe I've misunderstood what jest-webpack-alias is for? I read #5 and figured that "coffeescript is javascript", so it should work, right?
I also wrote this issue up at http://stackoverflow.com/questions/35327165/how-do-i-use-jest-with-coffeescript-and-es6-es2015-e-g-via-babel. If someone writes an answer there, I'll give them credit on that site, otherwise, I'll transfer stuff from here to there.
Add .travis.yml, coveralls npm task, travis and coveralls badges to README.md
I can provide you with the necessary PR for that but you gotta take few setup steps.
Sorry if I'm ignorant, but I can't work out how to use this.
Is webpackProfile
an actual webpack thing or is it something you guys use internally? It looks like your webpack.config.js
exports an object with profile names as keys. Is that something you made up?
When jest-webpack-alias imports the webpack config:
var webpackSettings = require(webpackFile);
_.get(webpackSettings, 'resolve.root')
fails because webpackSettings
looks like this:
> webpackSettings
{ __esModule: true,
default:
{ name: 'main',
entry: './src/app/main.jsx',
output:
{ pathinfo: true,
filename: 'main.bundle.js',
path: '/Users/spencerelliott/some/project/build/public',
publicPath: '/assets/' },
devtool: 'eval',
module: { preLoaders: [Object], loaders: [Object], noParse: [Object] },
resolve:
{ extensions: [Object],
root: '/Users/spencerelliott/some/project/src' },
plugins: [ [Object], [Object], [Object], [Object] ] } }
Causing this error:
● Runtime Error
Error: Missing setting "resolve.root" in /Users/spencerelliott/some/project/webpack.config.js
at Object.read (/Users/spencerelliott/some/project/node_modules/jest-webpack-alias/lib/webpackInfo.js:34:11)
at ensureWebpackInfo (/Users/spencerelliott/some/project/node_modules/jest-webpack-alias/lib/preprocessor.js:25:33)
at Object.process (/Users/spencerelliott/some/project/node_modules/jest-webpack-alias/lib/preprocessor.js:138:3)
at Object.module.exports.process (/Users/spencerelliott/some/project/jestPreprocessor.js:20:31)
My current workaround is:
// webpack.config.js
export default module.exports = { /* the config */ };
Cannot find module '******/webpack.config.js'
I found a line where you parse package.json ...
{
"jest-webpack-alias":{
"configFile": "tasks/webpack/production.js"
}
}
Ok. But, it's strange to see that when you probably can place configFile
into jest config. Why not?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.