业力单元测试 [英] Karma Unit Testing

查看:124
本文介绍了业力单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试启动业力时,我很难找出此错误的原因:

I'm having trouble trying to figure out the reason for this error when trying to start karma:

bash-3.2$ NODE_ENV=test karma start
INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Mac OS X)]: Connected on socket FONtQ1oNxIxRecRxYgnS with id 73511945
PhantomJS 1.9.8 (Mac OS X) ERROR
ReferenceError: Can't find variable: require
at /public/components/angular-cookies/index.js:1

index.js文件包含:

The index.js file contains:

require('./angular-cookies');
module.exports = 'ngCookies';

请帮助!

推荐答案

一种解决方法是npm install karma-browserify --save-dev.那应该行得通,如果您需要的实际文件使用 require .

One way to fix this is to npm install karma-browserify --save-dev. That should work and you will have to do that if the actual file you need uses require.

index.js不是您需要的实际文件. angular-cookies.js是.

index.js is not the actual file you need. angular-cookies.js is.

我发现使用browserify会使调试测试错误更加困难,因为从业力获得的堆栈跟踪将引用测试完成时已删除的临时.browserify捆绑包中的行号.

I find that using browserify makes debugging test errors more difficult because the stack trace you get from karma would refer to line numbers from temporary .browserify bundles that already got deleted by the time your test is done.

一个简单的解决方案是将karma.conf.js中的files直接指向angular-cookies.js,而不使用通配符.这样,index.js及其对 require 的使用不会引起不必要的问题.

A simple solution is to point files in karma.conf.js directly to angular-cookies.js instead of using wildcards. This way, index.js and its usage of require will not cause unnecessary issue.

这篇关于业力单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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