使用Webpack,Jasmine(-core),打字稿进行单元测试 [英] Unit testing with Webpack, Jasmine (-core), typescript

查看:195
本文介绍了使用Webpack,Jasmine(-core),打字稿进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目正在使用webpack将所有代码捆绑到一个文件中。该项目正在使用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.

我已经去添加单元测试,而茉莉似乎是这样的方式(多种方式之一) )前进。它实际上是包含在package.json中的jasmine-core - 不确定它有多大区别。

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');

然后当我运行测试时它抱怨,因为它不知道'需要'是什么。
未捕获ReferenceError:未定义require

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.

那么最好的方法是什么?
我应该在webpack.config.js文件中有多个入口点 - 每个* .spec.ts文件一个?
或者有没有办法说接受未知数量的spec文件

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

条目:[* .spec.ts]并让它输出一个js文件对于每一个 - * .spec.js

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

推荐答案

您可以使用业力 / karma-webpack 使用webpack运行所有测试以解决导入问题。您可以查看此存储库以进行简单配置。

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.

您还可以将index.spec.ts指定为入口点,如果您不想为每个 spec.ts创建一个入口点,则使该文件需要所有spec文件在您的webpack配置文件中。

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.

这篇关于使用Webpack,Jasmine(-core),打字稿进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆