Grunt-contrib-jasmine cannot find PhantomJS API?

The PhantomJS API claims to allow access to 'fs' and several other commonJS built-in modules through a standard interface. grunt-contrib-jasmine claims to run all specifications using phantomJS. But when I use grunt-contrib-jasmine, does the require method seem to be inaccessible?

fs = require('fs') describe 'DesignService', -> it 'test loadFromJSON', -> jsonFile = fs.read("resources/sample_pole.json") 

Gives me an error:

  ReferenceError: Can't find variable: require at >> target/spec/Spec.js:3 

What am I doing wrong?

If this is unclear, I compile from coffeescript and then pointing grunt-contrib-jasmine to the compilation output. Other specifications work fine.

+6
source share
1 answer

Cause

require method is available only on the server side (Nodejs / PhantomJS), but all jasmine tests (specs) are run on the client side.

Possible Solution

You can create a JavaScript file in the helpers folder with the following contents:

 window.jsonFile = { some : json_object } 

And use the jsonFile link in your spec files.

Description

From the PhantomJS description:

PhantomJS is a mute WebKit script with a JavaScript API.

From grunt-contrib-jasmine description:

Run jasmine specs silently through PhantomJS.

grunt-contrib-jasmine automatically creates the _SpecRunner.html file (see below, for example) with all user specifications and passes it to PhantomJS. PhantomJS is a separate executable file that in Nodejs is only wrapped as a package. This is the same executable file as downloaded from Phantomjs.org .

At the end, this line is executed:. .\node_modules\grunt-contrib-jasmine\node_modules\grunt-lib-phantomjs\node_modules\phantomjs\lib\phantom\phantomjs .\node_modules\grunt-contrib-jasmine\node_modules\grunt-lib-phantomjs\phantomjs\main.js .\_SpecRunner.html . Here, the main.js file should open the page and link the alerts ( alert(jsonString) ) that are thrown into the grunt log.

So, the PhantomJS API is available in main.js , but not in the _SpecRunner.html and jasmine spec files.

The result will be the same as if it was opened by _SpecRunner.html with a browser, except that all messages will be intercepted by the jasmine reporter and displayed on the screen.

 <!doctype html> <html> <head> <meta charset="utf-8"> <title>Jasmine Spec Runner</title> <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> <script src="./.grunt/grunt-contrib-jasmine/jasmine-html.js"></script> <!-- Some vendor libraries --> <script src="./test/vendor/jquery.js"></script> <!-- Some helpers --> <script src="./test/helpers/ts.js"></script> <!-- Your spec files --> <script src="./test/main_spec.js"></script> <!-- Jasmine reporter that displays the result--> <script src="./.grunt/grunt-contrib-jasmine/reporter.js"></script> <script src="./.grunt/grunt-contrib-jasmine/jasmine-helper.js"></script> </head> <body> </body> </html> 
+6
source

Source: https://habr.com/ru/post/949563/


All Articles