Tuesday 16 May 2017

Mocking imports with webpack 2 / karma

I am trying to figure out how to speed up the unit testing on my project. I am using webpack 2 and karma.

I have a custom npm module which I am bundling with my application. This has a lot of exports which have dependencies (using tree shaking in project).

For example index.js of my npm module "Test" looks like this:

export Button from "./Button"
export Unused1 from "./Unused1"
export Unused2 from "./Unused2"
...many more imports follow

My component tests will use "Button" from "Test"

import {Button} from "Test"

I want to create a simple test which will be bundled by webpack and fed to karma/phantomjs. The test should only bundle/resolve what is needed, so for example it should only resolve "Button" in the above example.

I have tried mocking "Test" using https://github.com/plasticine/inject-loader and https://github.com/speedskater/babel-plugin-rewire

Basically, I tried injecting "Test" : { Button}, where Button I imported from "Test/Button" in my test file.

They both seem to still bundle the whole index.js of "Test" which includes all the exports.

Any tips? Thanks!



via emooheo

No comments:

Post a Comment