grunt-contrib-jasmine can't find PhantomJS API?
PhantomJS API声称允许通过标准require接口访问" fs"和其他一些内置的commonJS模块。 grunt-contrib-jasmine声称可以使用phantomJS运行所有规范。 但是当我使用grunt-contrib-jasmine时,require方法似乎不可用?
1 2 3 4 | fs = require('fs') describe 'DesignService', -> it 'test loadFromJSON', -> jsonFile = fs.read("resources/sample_pole.json") |
给我错误:
1 2 | ReferenceError: Can't find variable: require at >> target/spec/Spec.js:3 |
我究竟做错了什么?
如果不清楚,我从coffeescript进行编译,然后将grunt-contrib-jasmine指向编译的输出。 其他规格都运行良好。
原因
可能的解决方案
您可以在
1 | window.jsonFile = { some : json_object } |
并在规范文件中使用
说明
从PhantomJS描述:
PhantomJS is a headless WebKit scriptable with a JavaScript API.
从grunt-contrib-茉莉花描述:
Run jasmine specs headlessly through PhantomJS.
最后,执行以下行:
ode_modules\\grunt-contrib-jasmine\
ode_modules\\grunt-lib-phantomjs\
ode_modules\\phantomjs\\lib\\phantom\\phantomjs .\
ode_modules\\grunt-contrib-jasmine\
ode_modules\\grunt-lib-phantomjs\\phantomjs\\main.js .\\_SpecRunner.html
这是
因此,PhantomJS API在
结果与使用浏览器打开
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <!doctype html> <html> <head> <meta charset="utf-8"> Jasmine Spec Runner <link rel="stylesheet" type="text/css" href=".grunt/grunt-contrib-jasmine/jasmine.css"> <!-- Jasmine test suite --> <script src="./.grunt/grunt-contrib-jasmine/jasmine.js"> <script src="./.grunt/grunt-contrib-jasmine/jasmine-html.js"> <!-- Some vendor libraries --> <script src="./test/vendor/jquery.js"> <!-- Some helpers --> <script src="./test/helpers/ts.js"> <!-- Your spec files --> <script src="./test/main_spec.js"> <!-- Jasmine reporter that displays the result--> <script src="./.grunt/grunt-contrib-jasmine/reporter.js"> <script src="./.grunt/grunt-contrib-jasmine/jasmine-helper.js"> </head> <body> </body> </html> |