Rollup: how to resolve the alias in Mocha test framework?

Created on 13 Jul 2018  Â·  3Comments  Â·  Source: rollup/rollup

Hi, I use rollup.js to compile my project and define alias in rollup.config.js and it's awesome!

However, when I use mocha.js to test my unit function, the alias I define in rollup.config.js can't be realize by mocha.

For example,
className: path.join(__dirname,'src/_DOM/className'),
and in my src file,
I use it like
import className from 'className'
but when I test this unit, an error thrown that can't find 'className' module。
is it any plugins or libraries can be use ?

All 3 comments

It's probably better to have import className from '../src/_DOM/className' in your test here as that should be guaranteed to work between environments.

I'm not sure how to configure aliases for Mocha's loader, but there may well be a way to do that too.

Depending on the size of your project and the number of test suites you have, you could also use your test files as entry points and run them through rollup with a similar configuration. That way, you could test any other plugins you have in your rollup config as well. With the new code-splitting, this could even be manageable performance-wise but it would probably need a little tinkering to collect the entry points and then run your chunks through mocha.

Might also be an idea for a very interesting plugin.

Alternatively, you could try experimentalPreserveModules (which is still not properly documented I fear)

Hi, sorry for replying so late.
Actually I found a plugin called babel-plugin-module-resolver
and increase the alias list in .babelrc file the same as rollup.config.js like
"plugins": [
[ "module-resolver", {
"alias": {
"className":"./src/_DOM/className"}}]]
so mocha can know what the alias mean as well.
Thank you very much for the reply :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alendit picture alendit  Â·  3Comments

MuYunyun picture MuYunyun  Â·  3Comments

Announcement picture Announcement  Â·  3Comments

jeffsaremi picture jeffsaremi  Â·  3Comments

good-idea picture good-idea  Â·  3Comments