Search code examples
javascriptwebpackaliaspackage.jsonava

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 src folder:

webpack.config.js

resolve: {
  alias: {
    '@': path.resolve(__dirname, 'src/'),
  },
},

and then that @ special prefix for my own modules like this:

my-module.js

import main from '@/view/main'

This is my AVA configuration:

package.json

"scripts": {
  "test-unit": "ava test/unit"
},
"ava": {
  "require": ["esm"]
},

Is possible to add something to package.json like in this mocha solution? https://stackoverflow.com/a/42989394/12361834

Thank you so much for your time and help!


Solution

  • If I understood you well you can do it with link-module-alias npm package.

    1. Add this to your package.json:

      "scripts": {
        "postinstall": "link-module-alias",
        "preinstall": "command -v link-module-alias && link-module-alias clean || true"
        "test": "ava"
      },
      "_moduleAliases": {
        "@": "src"
      }
      
    2. npm i && npm test

    If you need further details you can download a working example here.