Home > front end >  Test both source code and bundled code with jest
Test both source code and bundled code with jest

Time:02-01

Let's say I am developing an NPM module.

I am using Jest for the testing, Webpack to bundle it and TypeScript in general.

When I test the source code, everything is fine, with also a very good code coverage and all of that. But I think that it is not enough. It could be possible that something breaks after the Webpack bundle is generated, for instance a dynamic import (a require with a variable instead of a fixed path) that would become incorrect after the bundle, or other possible scenarios.

How should I write tests that cover also the bundle? Should I test against both the source code (so that I get good coverage) and the bundle? Usually I import things directly from a specific files (e.g. /utils/myutil.ts), but with the bundle this would be impossible. How to handle this?

CodePudding user response:

I do test against the bundle for some of my projects. I do this for some libraries (npm). To do this I create some code that imports the bundle and write tests against this code. Don't care about coverage in this case, I just want to verify that my library does what it's supposed to do.

In another case (not a library) I'm testing against the bundle but I'm running more integration/e2e tests.

Don't worry about coverage that much unless every functions (or most of them) of your code are going to be used by the final user. You should test something the way it is used. 100% coverage is nice to see but very impractical to achieve when projects get big and in any case it's a waste of time. Of course, some people will disagree :)

  •  Tags:  
  • Related