简体   繁体   English

使用不带Webpack的Jasmine Karma对Angular 2 App进行单元测试

[英]Unit Testing for Angular 2 App Using Jasmine Karma Without Webpack

Can Jasmine-Karma be implemented for Unit Testing in Angular 2 application without using Webpack? 是否可以在不使用Webpack的情况下将Jasmine-Karma实现为在Angular 2应用程序中进行单元测试? Instead of Webpack, SystemJS is a solution but is it correct to use SystemJS and Webpack in an application simultaneously? 代替Webpack,SystemJS是一种解决方案,但是同时在应用程序中使用SystemJS和Webpack是否正确? I'm using Webpack for development mode and production mode of the application. 我正在将Webpack用于应用程序的开发模式和生产模式。

Karma and Jasmine has nothing to do with Angular2, they're separate libraries that could run completely separately. Karma和Jasmine与Angular2无关,它们是可以完全独立运行的独立库。

For Karma to run, you'd need : 要使Karma运行,您需要:

 npm install -g karma-cli
 karma init my.conf.js // this will create a configuration file for Karma
 karma start my.conf.js

This will look for all your test files and run them and then shows the result in the command line. 这将查找所有测试文件并运行它们,然后在命令行中显示结果。

But, the problem is when you're dealing with Angular2 code which you normally write in Typescript and the code itself might have some extra dependencies, the Karma configuration might become painful. 但是,问题在于,当您处理通常用Typescript编写的Angular2代码并且代码本身可能具有一些额外的依赖关系时,Karma配置可能会很痛苦。 You might have sass files that needs to be converted into css files first for Karma ( you can think of Karma as the browsers, it needs only JS and CSS and HTML, nothing more). 您可能有一些sass文件,它们首先需要为Karma转换为css文件(您可以将Karma视为浏览器,它仅需要JS,CSS和HTML,仅此而已)。

And like I said, you'd need your typescript code to first be transpiled to Javascript to give it to karma right? 就像我说的那样,您需要将打字稿代码首先转换为Javascript才能将其提供给业力,对吗?

So you need someone to handle this small tasks for you, which is normally webpack in this days, and used to be gulp and grunt. 因此,您需要有人来为您处理这些小任务,这通常是现今的webpack,曾经是大吃大喝的。

Webpack is a sophisticated bundler and it does almost everything you might ever need when working with Angular. Webpack是一个复杂的捆绑器,它几乎可以处理与Angular一起使用时可能需要的一切。

I wouldn't recommend using SystemJS and Webpack together because Webpack already does what SystemJS does ( loading files and ... ). 我不建议同时使用SystemJS和Webpack,因为Webpack已经完成了SystemJS的工作(加载文件和...)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM