简体   繁体   中英

How can I specify the PATH to PhantomJS using GRUNT and QUNIT?

Grunt uses PhantomJS to run headless QUnit tests in a very interesting way (correct me if I'm wrong please). Since I just started experimenting with those tools I don't fully understand it and don't know how to configure nor how to extend it.

I manage to get all working on my machine but I would like to not use the $PATH system variable. Instead, I would like to provide the path to PhantomJS's executable file via a setting which I could easily change and port to other environments.

How can I achieve this?

I suppose there are many ways and I think the Qunit Task from Grunt might have an easy answer. Ideally it would be just a matter of defining the path on the grun.js file, something like this:

qunit: {
      phantomjsPath: 'path/to/phantomjs',
      files: ['test/**/*.html']
    },

My environment is a MacOSX but I accept solutions for any kind of environments like Windows - my build server.

Thanks in advance.

UPDATE The version of Grunt I am using is v0.3.17 . The next big version, v0.4.x , has many changes and some are not backwards compatible.

Well I think you finally migrated onto Grunt 0.4. And propably you got grunt-contrib-qunit plugin for running qunit tests under PhantomJS. Unfortunately you'll encountered the same issue - it's not possible to supply path to phantomjs executable. That's because grunt-contrib-qunit/grunt-contrib-phantomjs use phantomjs npm module which downloads PhantomJS on installation and hard-codes path to the executable in its js code. If you're experiencing such an issue then please check my blog post .

Unfortunately, grunt 0.3.x doesn't have a built-in option to specify a path to phantomjs -- it just executes phantomjs directly on the command line. Take a look at this helper function:

https://github.com/gruntjs/grunt/blob/master/tasks/qunit.js#L231

The situation seems to have changed in the has-yet-to-be-released grunt-0.4, however:

https://github.com/gruntjs/grunt-lib-phantomjs/blob/master/lib/phantomjs.js#L22

As you can see, the next version of grunt uses the npm module phantomjs which "exports a path string that contains the path to the phantomjs binary/executable.". Since the npm module phantomjs is installed locally by grunt, it seems like this would avoid you having to worry about setting the PATH variable or installing a conflicting version of phantomjs .

Anyway, I'd consider taking a look at grunt-0.4 if you're willing to live on the bleeding edge.

Otherwise, you can always fork the qunit task and modify the grunt-qunit task to look at your custom configuration variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM