Unit testing with Webpack, Jasmine (-core), typescript

前提是你 提交于 2019-12-01 17:49:44

问题


I have a project that is using webpack to bundle all code into a single file. The project is using Typescript and it is working fine at the moment.

I've gone to add unit testing and jasmine seems to be the way (one of the many ways) forward. Its actually jasmine-core that is included in the package.json - not sure how much of a difference that makes.

So running a very simple test such as

it('true is true', function(){ expect(true).toEqual(true); });

works fine. But when I add tests that require the use of an import - eg

import MyService = require('./MyServices');

then when I run the tests it complains as it doesn't know what 'require' is. Uncaught ReferenceError: require is not defined

Now I'm guessing this is because I need to package up the test module in a similar way that I package up the main project.

So what is the best way to do this? Should I have multiple entry points in the webpack.config.js file - one for each *.spec.ts file? Or is there a way to have say accept an unknown number of spec files

entry:[ *.spec.ts ] and have it output a js file for each one - *.spec.js


回答1:


You can use karma/karma-webpack to run all the tests using webpack for resolving the imports. You can take a look at this repository for a simple configuration.

You can also specify an index.spec.ts as en entry point and make this file require all the spec files if you don't want to make one entry point for each spec.ts in your webpack's configuration file.



来源:https://stackoverflow.com/questions/39487487/unit-testing-with-webpack-jasmine-core-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!