简体   繁体   中英

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM