Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding aliases and root in resolve will not work in tests. #620

Closed
AlexFrazer opened this issue Dec 14, 2016 · 1 comment
Closed

Adding aliases and root in resolve will not work in tests. #620

AlexFrazer opened this issue Dec 14, 2016 · 1 comment
Labels

Comments

@AlexFrazer
Copy link
Contributor

The errors:

If you add any aliases or root to the webpack.config.base.js, it will not function the same in a test environment, and will not resolve appropriately. The error will be something like:

Error: Cannot find module 'app/utils/api'

How to reproduce:

Simply add a single alias or root to the webpack.config.base.js

export default {
  // ... whatever
  resolve: {
    extensions: ['', '.js', '.jsx', '.json', '.css', '.scss'],
    packageMains: ['webpack', 'browser', 'web', 'browserify', ['jam', 'main'], 'main'],
    alias: { 'app': `${__dirname}/app` },
  }
}

Then add this to the webpack.config.test.js

module.exports = {
  // ...
  resolve: devConfig.resolve
}

Use an aliased import anywhere in the application and import it in a test.

import 'app/utils/api';  // <- just an example.

Run tests.

$ npm run test
@jhen0409
Copy link
Member

jhen0409 commented Dec 15, 2016

Write resolve.alias to test config and add babel-plugin-webpack-aliases to TEST env of babelrc may be a simple workaround.

(See istarkov/babel-plugin-webpack-loaders#68)

.babelrc

{
  ...
  "env": {
    ...
    "test": {
      "plugins": [
+       ["webpack-aliases", { "config": "webpack.config.test.js" }],
        ["webpack-loaders", { "config": "webpack.config.test.js", "verbose": false }]
      ]
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants