question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Recipe for using AVA with Webpack aliases

See original GitHub issue

I use Webpack in my app and I defined aliases in config: webpack.config.dev.js:

....
resolve: {
    // We can now require('file') instead of require('file.jsx')
    extensions: ['', '.js', '.jsx', '.scss'],
    alias: {
      actions: PATHS.app + '/actions',
      components: PATHS.app + '/components',
      store: PATHS.app + '/store',
      reducers: PATHS.app + '/reducers',
      helpers: PATHS.app + '/helpers',
      sagas:  PATHS.app + '/sagas',
      services:  PATHS.app + '/services',
    },
  }
....

And I import modules like so: componts/LoginPage.js

import {login} from 'actions/Login'

instead of

import {login} from '../actions/Login'

But AVA doesn’t allow to define aliases, so I’m unable to test componts/LoginPage.js with AVA since it throws that error:

Error: Cannot find module 'actions/Login'

Is there a solution?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

18reactions
apedyashevcommented, Aug 20, 2016

Thank you, guys for your help. It seems to work with following configs:

config/webpack.config.ava.js

const path = require('path');

const PATHS = {
  app: path.resolve(__dirname, '../src'),
};

module.exports = {
   resolve: {
     modulesDirectories: [
       __dirname,
      'node_modules',
       PATHS.app,
     ]
   },
   module: {
     loaders: [
       {
         test: /\.css$/,
         loader: 'style-loader!css-loader?modules&importLoaders=1!postcss-loader',
       },
     ]
   }
};

package.json

{
  ....
  "scripts": {
    "test": "CONFIG=$(pwd)/config/webpack.config.ava.js BABEL_DISABLE_CACHE=1 NODE_ENV=AVA ava" 
    },
  ....
  "ava": {
    "files": [
      "src/**/*.spec.js"
    ],
    "require": [
      "babel-register"
    ],
    "babel": "inherit"
  }
}

.babelrc

{
  "presets": ["es2015", "react", "stage-1", "stage-0"],
  "env": {
    "AVA": {
      "plugins": [
        [
          "babel-plugin-webpack-loaders",
          {
            "config": "${CONFIG}",
            "verbose": false
          }
        ]
      ]
    }
  }
}
10reactions
apedyashevcommented, Oct 17, 2016

Btw, I have another solution of my problem (without using webpack):

env NODE_PATH=src ava
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using webpack aliases in AVA tests
I need to include the aliases from webpack into AVA when it runs. I used webpack's resolve.alias to access all the files under...
Read more >
Using Webpack Aliases In Ava Tests
I'm working on some unit tests of React components.The tests are written with AVA and use Enzyme to shallow render components.When the component...
Read more >
avajs/ava
We make use of aliases in webpack to alias some of our requires so that we can switch between a local and a...
Read more >
npm:@ava/babel
Adds Babel 7 support to AVA so you can use the latest JavaScript syntax in your ... Webpack aliases can be used to...
Read more >
ts-node
... by running `npm i ts-node`. There are 7243 other projects in the npm registry using ts-node. ... Recipes. Watching and restarting; AVA....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found