Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 624
delp Avatar asked Oct 18 '22 01:10

delp


1 Answers

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.

like image 100
Frank Bessou Avatar answered Oct 20 '22 16:10

Frank Bessou