grunt-contrib-jasmine can't find PhantomJS API?

gruntjs, phantomjs

Solution

Cause

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

Possible solution

You could create a JavaScript file in `helpers` folder with the content like this:

window.jsonFile = { some : json_object }

And use a `jsonFile` reference in your spec files.

Explanation

From the PhantomJS description:

PhantomJS is a headless WebKit scriptable with a JavaScript API.

From grunt-contrib-jasmine description:

Run jasmine specs headlessly through PhantomJS.

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

In 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 `main.js` file is to open the page and bind alerts (`alert(jsonString)`) that are thrown to the grunt logging.

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

The result is the same as if opened `_SpecRunner.html` with the browser, except all messages will be intercepted by jasmine reporter and displayed on 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>

Problem

The PhantomJS API claims to allow access to 'fs' and a few other built-in commonJS modules through the standard require interface. grunt-contrib-jasmine claims to run all specs using phantomJS. But when I use grunt-contrib-jasmine the require method doesn't seem to be available? ``` fs = require('fs') describe 'DesignService', -> it 'test loadFromJSON', -> jsonFile = fs.read("resources/sample_pole.json") ``` Gives me the error: ``` ReferenceError: Can't find variable: require at >> target/spec/Spec.js:3 ``` What am I doing wrong? In case it isn't clear, I am compiling from coffeescript, and then pointing the grunt-contrib-jasmine to the output of the compilation. The other specs are all running fine.

Original source