简体   繁体   中英

Testing node.js code that is built by webpack

I am building the backend of my app using webpack (making use of babel-loader and other loaders). I want to test individual modules of said backend using any of the common testing tools such as Mocha just like I could do with plain Node modules.

My build process creates one big bundle of my files (node_modules are externalized though) which obviously doesn't allow me to test individual modules contained in that bundle.

What I want:

  • Test modules that can be compiled by Webpack.
  • Modules that can be compiled by Webpack and tested by said test modules.
  • Be able to use tools like rewire to inject fake dependencies.
  • Run a single test module.
  • Be able to use Webpack's watch mode.

Possible approach:

I would say that each test modules equates to a entry point in terms of Webpack. Both test modules and the tested modules could then built by Webpack. This would also automatically allow Webpack's watch mode to be used. However, every new test module has to be added entry point to the Webpack configuration, which feels kind of wrong and tedious.

Using this approach, most testing tools would just work because could just use the output of those test module entry points.

Injecting fake dependencies is another problem using this approach. To make this work the test modules have to use the actual Node require function, instead of the using the one provided by Webpack which loads the module from the test module bundle itself. I have absolutely no clue how to solve this.

Any idea how to actually pull this off? Webpack is really nice but I just can't figure out how to test my modules properly with it.

我认为我的问题现在通过各种Babel插件解决,例如babel-plugin- rewire,只使用Mocha和合适的.babelrc。

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