Karma Unit Testing

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

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 

The index.js file contains:

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

Please help!!

回答1:

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 is not the actual file you need. angular-cookies.js is.

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.

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.



回答2:

require is nodejs feature try install this karma-browserify, it allow you to use "require"



回答3:

Try adding browserify as a

preprocessor: 'test/**/*.js': ['browserify'] 

and add it to your frameworks



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