jdcataldo / grunt-mocha-phantomjs Goto Github PK
View Code? Open in Web Editor NEWA simple grunt wrapper for mocha-phantomjs to allow for ci integration
License: MIT License
A simple grunt wrapper for mocha-phantomjs to allow for ci integration
License: MIT License
I am trying to use the watch event triggered by grunt-contrib-watch to only run tests on the files that have been changed. Unfortunately grunt-mocha-phantomjs does not seem to pick up the new options.
Basically I am doing this :
grunt.initConfig({
mocha_phantomjs : {
all : ['develop/test/**/*.html']
}
);
grunt.event.on('watch', function(action, filepath) {
grunt.config('mocha_phantomjs.all', [filepath]);
});
I can see the value changing, but the value is simply not being picked up after loading the task. Is there any way of making them be picked up?
When I run this project against my tests with Ubuntu 13.04, everything works fine. Running under OS X, I get
Running "mocha_phantomjs:test"
Warning: 8 tests failed Use --force to continue
Even if I deactivate all tests or change the test index.html in the GruntFile to an invalid address, I get this error :( I kind of stuck here, has anyone experience running this on OS X? Thx for any advice :)
Gruntfile extract:
mocha_phantomjs: {
cov : {
options : {
run : false,
urls : [ 'http://localhost:<%= connect.options.port %>/test/index.html' ],
reporter: 'xunit',
output: 'app/test/cov/report/TEST-all.xml'
}
},
test : {
options : {
run : false,
urls : [ 'http://localhost:<%= connect.options.port %>/test/index.html' ],
reporter: 'spec'
}
},
},
...
grunt.registerTask('test', [
'clean:dist',
'compass',
'connect:app',
'mocha_phantomjs:test' ]);
I am getting the same error mentioned in this stackoverflow thread. Is there any workaround? I don't like the idea to downgrade my nodeJS
:(
My code works with grunt-mocha but I like to use the report capabilities of this plugin. Therefore I doubt the error is in my code. If it helps, I will, of course, post snippets.
Thx in advance
edit
I just tried mocha-phantomjs
only and it fails with the same error. :( I'll open up an issue there as well.
mocha-phantomjs -R dot http://localhost:9000/test/index.html
Failed to start mocha: Init timeout
Index.html:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Mocha Spec Runner</title>
<link rel="stylesheet" href="../components/mocha/mocha.css">
<script src="../components/mocha/mocha.js" type="text/javascript" charset="utf-8"></script>
<script src="../components/sinon/index.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" type="text/css" href="../styles/aktionsmenue.css">
</head>
<body>
<div id="mocha"></div>
<script type="text/javascript" src="../components/requirejs/require.js" data-main="runner/specRunner.js"> </script>
</body>
</html>
The phantomjs code was separated out from the CLI in mocha-phantomjs
in preparation for phantomjs2 and to give build plugin authors like you for grunt full control over what version of phantomjs you use, where you get it from, and how you call it. Full discussion is in nathanboktae/mocha-phantomjs#127, and will prevent issues like #36 .
The main difference is options except reporter and URL are passed as a JSON blob. See the documentation, as well as how gulp-mocha-phantomjs
is already doing this
This already exist: https://github.com/kmiyashiro/grunt-mocha
If it's missing something then please explain in the readme how your task is improving upon it.
If there is no errors and we're reporting to stdout, following error is being thrown:
Fatal error: Unable to write "undefined" file (Error code: undefined).
I'm running into an issue using this on Windows. When I try to run the task I get:
λ grunt mocha_phantomjs
Running "mocha_phantomjs:all" (mocha_phantomjs) task
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
It looks like this is a known issue with Spawn and is detailed a bit here: baudehlo/node-phantom-simple#142
When I run xunit tests using v0.4.1, I get the following error:
19-Feb-2014 08:38:26 <testcase classname="" name=""after all" hook" time="0" message="write after end"><failure classname="" name=""after all" hook" time="0" message="write after end"><![CDATA[Error: write after end
19-Feb-2014 08:38:26 at writeAfterEnd (_stream_writable.js:130:12)
19-Feb-2014 08:38:26 at WriteStream.Writable.write (_stream_writable.js:178:5)
19-Feb-2014 08:38:26 at Socket.ondata (stream.js:51:26)
19-Feb-2014 08:38:26 at Socket.EventEmitter.emit (events.js:117:20)
19-Feb-2014 08:38:26 at Socket.<anonymous> (_stream_readable.js:746:14)
19-Feb-2014 08:38:26 at Socket.EventEmitter.emit (events.js:92:17)
19-Feb-2014 08:38:26 at emitReadable_ (_stream_readable.js:408:10)
19-Feb-2014 08:38:26 at emitReadable (_stream_readable.js:404:5)
19-Feb-2014 08:38:26 at readableAddChunk (_stream_readable.js:165:9)
19-Feb-2014 08:38:26 at Socket.Readable.push (_stream_readable.js:127:10)]]></failure></testcase>
19-Feb-2014 08:38:26 </testsuite>
I then tried with v0.4.0 and everything runs just fine. The error could be related to this commit?
Here are some hopefully relevant bits of my Gruntfile:
xunit: {
src: ["js/test/**/*.html"],
options: {
reporter: "xunit",
output: "xunit_results/phantomjs.xml"
}
}
Please let me know if there's any other info that would be helpful!
In our tests we pull in a lot of files and are experiencing mocha timeouts - possibly because the files take time to download.
What options do I use to configure the mocha timeout when using grunt-mocha-phantomjs plugin?
Thanks,
Kiran.
Now i have another trouble...
Module mocha-phnatomjs does not have phantomjs in dependencies... But when spawn phantom process path for binary resolved this way:
var phantomjs;
for (var i=0; i < module.paths.length; i++) {
var bin = path.join(module.paths[i], '.bin/phantomjs');
if (process.platform === 'win32') {
bin += '.cmd';
}
if (exists(bin)) {
phantomjs = spawn(bin, spawnArgs);
break;
}
}
if (phantomjs === undefined) { phantomjs = spawn('phantomjs', spawnArgs); }
Because grunt-mocha-phantomjs have phantom in package.json dependencies phantomjs spawn by last line... To resolve this issue i suggest remove phantomjs from dependencies and add notation on readme about need to include phantomjs in the list of dependencies in the project.
And one another thing: in cf1d867 commit resolving path should bubble from down to top level as in code above. Should i send pull request?
Hi,
I have the issue that the installed dependencies do not work
I now wrote a little task myself based on http://pastebin.com/Vy53zmTT
It uses the global dependencies installed by phantomjs and mocha-phantomjs
Please try to strip your module down so it doesn't install everything again.
Whenever I run the task with default options (only specifying the src
), I get the following output (includes the --verbose
flag):
Running "mocha_phantomjs:src" (mocha_phantomjs) task
Verifying property mocha_phantomjs.src exists in config...OK
Files: C:/testRunner.html -> src
Options: reporter="spec", urls=[]
Unable to open file 'spec'
Introducing an explicit reporter, such as dot
, results in the same thing but with dot
replacing spec
in the error.
Here's my npm list
output:
http://i.imgur.com/6UGHu0H.png
Anyone run into this?
It seems the path to mocha-phantomjs is wrong when running on Windows. It should have *.cmd
.
If I define
mocha_phantomjs:{
options: {
'urls' : [ 'http://localhost:1337/test/ajaxtests.html']
}
}
The grunt task runs with nothing done - as there are no "files" to run - but If only want to test a specific test script that runs on a webserver I cannot
Can we build in support for disabling security options in phantom within mocha_phantomjs Grunt task options?
it seems that this module installs phantomjs. my question today is: how can I change the version of phantomjs that gets installed? at present it's 1.9.8 but that version is broken. see: ariya/phantomjs#12697
so I'd like to downgrade to 1.9.7 but having done:
# npm uninstall phantomjs
# npm install [email protected] --save-dev
in my project I still get 1.9.8 to run during tests because the version under grunt-mocha-hpantomjs didn't change. so how can I change it?
Hi there, I was wondering if there is a grunt-mocha-phantomjs
best practice for using the phantomjs onConfirm
callback. I would like to test some things that trigger window.confirm
. Maybe writing tests for window.confirm
isn't really a thing.
If you haven't specified an output file, this will cause the build to crash:
Callback component
with invalid callback
✓ should throw an error when ERROR port is not attached
✓ should transmit an Error when ERROR port is attached
with valid callback
✓ should call the callback with the given data
108 tests complete (1 seconds)
Fatal error: Unable to write "undefined" file (Error code: undefined).
mocha-phantomjs just landed for v3.1.0. Would an update be possible? :)
npm i grunt-mocha-phantomjs
As of today, yields:
npm ERR! Darwin 14.4.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "i" "grunt-mocha-phantomjs"
npm ERR! node v0.12.7
npm ERR! npm v2.11.3
npm ERR! code EPEERINVALID
npm ERR! peerinvalid The package phantomjs does not satisfy its siblings' peerDependencies requirements!
npm ERR! peerinvalid Peer [email protected] wants [email protected] - 1.9.7-15
npm ERR! Please include the following file with any support request:
npm ERR! /Users/lmarkus/dev/scratch/t1/npm-debug.log
node_modules/grunt-mocha-phantomjs/node_modules/mocha-phantomjs/node_modules/which/which.js:83
throw new Error("not found: "+cmd)
^
Error: not found: phantomjs
at Function.whichSync [as sync] (/Users/okonet/Projects/jetbrains/jing/webui/node_modules/grunt-mocha-phantomjs/node_modules/mocha-phantomjs/node_modules/which/which.js:83:9)
at Object.<anonymous> (/Users/okonet/Projects/jetbrains/jing/webui/node_modules/grunt-mocha-phantomjs/node_modules/mocha-phantomjs/bin/mocha-phantomjs:97:15)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
I am getting an error when adding 'reporter: "nyan"' to the options.
Doing a little research it looks like nyan is not on the list of reporters that are in coffeescript. Does it just need to be converted to coffeescript?
I see that the task will always complete with a zero code when the output option is provided. Is there a way to coerce a failure, since CircleCI is marking the task as successful?
Grunt allows for multiple configurations ....something like:
grunt_task: {
foo: {
options: { test: true; }
},
bar: {
options: { test:false; }
}
}
...which can be called like grunt_task:foo
or grunt_task:bar
. I'm trying to setup something similar with grunt-mocha-phantomjs
b/c I want to use different reporters depending on if I'm running dev tests or running tests in CI, but so far have not been successful.
Is this something you support in your plugin - or will support?
How hard is it to implement this?
Hi,
When generating a Xunit report for example debugging messages from my app end up in the report. I could deactivate the debug messages for the test or filter them myself afterwards but I think it would be nice to have only the report being written into the output file, if that's possible. Thx for your help.
More of a request than a bug -- and I may be interested and willing to contribute work if the idea seems cogent: when running grunt mocha_phantomjs
on more than one HTML file, it'd be handy to have a custom reporter that compiles all the output into a single result.
As it stands right now, if it's running on, say, 20 files, and there's a test failure in the first test file, you have to scroll all the way back up through 20 sets of test results to read the error output, rather than a single concatenated set of results.
If the idea seems worth pursuing, a potential follow-up concept would be making it more performant on a large number of files by not starting a fresh Phantom process for each file. That may be more of a concern for the mocha-phantomjs
project than this, though. I'm not intimate enough with the code of either project to know where that line is drawn.
Let me know your thoughts! Like I said, I'd be happy to contribute if you think the idea has merit.
Are the only two pass-through options that are supported the list of urls
and the reporter
?
mocha-phantomjs supports quite a few other options, is there any way to just allow a pass-through via grunt so they are all supported?
Usage: mocha-phantomjs [options] page
Options:
-h, --help output usage information
-V, --version output the version number
-R, --reporter <name> specify the reporter to use
-t, --timeout <timeout> specify the test startup timeout to use
-A, --agent <userAgent> specify the user agent to use
-c, --cookie <name>=<value> specify cookie
-h, --header <name>=<value> specify custom header
-s, --setting <key>=<value> specify specific phantom settings
-v, --view <width>x<height> specify phantom viewport size
-C, --no-color disable color escape codes
Hey @jdcataldo - the latest update isn't working for me anymore.
I see this in my console when I run my tests now:
>> Can't open 'node_modules/grunt-mocha-phantomjs/node_modules/mocha-phantomjs/lib/mocha-phantomjs.coffee'
>> Exited with code: 255.
Currently if you have failing tests, the Grunt build will still complete, which means CI environments like Travis think everything works.
✖ 1 of 106 tests failed:
1) Network with a simple graph should contain two nodes:
TypeError: 'null' is not an object (evaluating 'n.processes')
at file:///home/bergie/Projects/noflo/noflo/spec/Network.js:88
at file:///home/bergie/Projects/noflo/noflo/node_modules/mocha/mocha.js:4039
at file:///home/bergie/Projects/noflo/noflo/node_modules/mocha/mocha.js:4404
at file:///home/bergie/Projects/noflo/noflo/node_modules/mocha/mocha.js:4463
at next (file:///home/bergie/Projects/noflo/noflo/node_modules/mocha/mocha.js:4330)
at file:///home/bergie/Projects/noflo/noflo/node_modules/mocha/mocha.js:4339
at next (file:///home/bergie/Projects/noflo/noflo/node_modules/mocha/mocha.js:4287)
at file:///home/bergie/Projects/noflo/noflo/node_modules/mocha/mocha.js:4307
at timeslice (file:///home/bergie/Projects/noflo/noflo/node_modules/mocha/mocha.js:5279)
Done, without errors.
when there are multiple url in mocha_phantomjs configuration, the run is always reported as successful; despite one (or multiple) url failing
is there an option to 'fail fast'? (or at least report non zero status when one of the urls is non-zero?)
here is an example, the test:full_testing will always succeed, but individual tests will fail
mocha_phantomjs: {
options: {
//'reporter': 'progress',
'output': 'test/reports/' + (grunt.option('testname') || 'mocha/discovery')
},
local_testing: ['test/' + (grunt.option('testname') || 'mocha/discovery') + '.spec.html'],
web_testing: {
options: {
urls: [
'http://localhost:<%= local.port || 8000 %>/test/' + (grunt.option('testname') || 'mocha/discovery') + '.spec.html'
]
}
},
full_testing: {
options: {
urls: [
'http://localhost:<%= local.port || 8000 %>/test/mocha/discovery.spec.html',
'http://localhost:<%= local.port || 8000 %>/test/mocha/discovery-ui.spec.html',
'http://localhost:<%= local.port || 8000 %>/test/mocha/discovery-qb.spec.html'
]
}
}
},
you can see it at work https://travis-ci.org/adsabs/bumblebee/jobs/33810821 - line 2400
I get the notice Unsafe JavaScript attempt to access frame with URL about:blank
when I run grunt_mochaphantomjs in my terminal.
My Gruntfile config looks like this:
mocha_phantomjs: {
all: ['tests/**/*.html']
},
My tests.html file looks like this:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Example Mocha Test</title>
<link rel="stylesheet" href="../node_modules/mocha/mocha.css" type="text/css" charset="utf-8" />
</head>
<body>
<!-- Required for browser reporter -->
<div id="mocha"></div>
<!-- mocha -->
<script src="../node_modules/mocha/mocha.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
// This will be overridden by mocha-helper if you run with grunt
mocha.setup('bdd');
</script>
<!-- Include your assertion lib of choice -->
<script src="../node_modules/chai/chai.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
// Setup chai
var should = chai.Should();
</script>
<!-- Include anything you want to test -->
<script src="../dist/app.js" type="text/javascript" charset="utf-8"></script>
<!-- Spec files -->
<script src="startGame.js" type="text/javascript" charset="utf-8"></script>
<!-- run mocha -->
<script type="text/javascript" charset="utf-8">
// If tests run in a real browser
// Can alternatively do a check on window.PHANTOMJS
mocha.run();
</script>
</body>
</html>
My test file looks like:
'use strict';
describe('Feature: Start Game', function() {
context('Scenario 1: Given a game has started', function() {
describe('When application is initatiated', function() {
it('Should throw an error - Game has initiated', function() {
app.should.throw(Error);
});
});
});
context('Scenario 2: Given no game has started', function() {
describe('When application is initiating', function() {
console.log(app);
it('Should start the game', function() {
app.game.should.be.an.instanceof(Game);
});
});
});
});
The actual error I get only occurs in terminal, and not when I run the html directly in the browser:
Unsafe JavaScript attempt to access frame with URL about:blank from frame with U
RL file:///c:/workspace/projects/demo.battleship3/node_modules/grunt-mocha-phant
omjs/node_modules/mocha-phantomjs-core/mocha-phantomjs-core.js. Domains, protoco
ls and ports must match.
Unsafe JavaScript attempt to access frame with URL about:blank from frame with U
RL file:///c:/workspace/projects/demo.battleship3/node_modules/grunt-mocha-phant
omjs/node_modules/mocha-phantomjs-core/mocha-phantomjs-core.js. Domains, protoco
ls and ports must match.
Unsafe JavaScript attempt to access frame with URL about:blank from frame with U
RL file:///c:/workspace/projects/demo.battleship3/node_modules/grunt-mocha-phant
omjs/node_modules/mocha-phantomjs-core/mocha-phantomjs-core.js. Domains, protoco
ls and ports must match.
Unsafe JavaScript attempt to access frame with URL about:blank from frame with U
RL file:///c:/workspace/projects/demo.battleship3/node_modules/grunt-mocha-phant
omjs/node_modules/mocha-phantomjs-core/mocha-phantomjs-core.js. Domains, protoco
ls and ports must match.
Hi, I have been trying to make this work but I haven't been able at all:
mocha_phantomjs: {
all: './tests.html',
options: {
coverage: {
reporter: 'istanbul'
}
}
}
The tests work perfectly, but there's no code coverage at all, which I'd love to have. Do you have any clue how to include it? I'd prefer using Istanbul since I've used it before for pure Node.js testing, but I'm having trouble integrating it with phantomjs. I have tried many different things, but none seem to help me getting closer to make a test coverage:
mocha_phantomjs: {
all: './tests.html',
options: {
coverage: {
htmlReport: 'coverage/html'
}
}
}
I've set up this module in a test project but am not having much luck making it work , partly because there are things I don't understand. I've explained my question in detail here: http://stackoverflow.com/questions/33769832/require-fails-on-a-phantomjs-test but thought perhaps someone here could address it.
any hints or suggestions most welcome
I need to use the 'hooks' option of mocha-phantomjs but I cannot set it correctly in Gruntfile.
The following instruction works correctly in terminal:
mocha-phantomjs ./test/test.html --hooks ./test/phantom_hooks.js -R xunit -f test/results/result.xml
In my Gruntfile.js
, I have put the following configuration:
grunt.initConfig({
...
mocha_phantomjs: {
options: {
reporter: 'xunit',
output: 'test/results/result.xml',
config: {
"hooks": './test/phantom_hooks.js'
}
},
all: 'test/test.html'
}
});
When I run grunt mocha_phantomjs
I get the following error:
Error loading hooks: Cannot find module './test/phantom_hooks.js'
Am I doing something wrong or is this an issue of the plugin?
I'm using mocha-lcov-reporter and grunt-mocha-phantomjs to generate code coverage data. It works great! But the reported data always gets dumped to stdout, even though I've configured an output
in my Gruntfile.js
:
mocha_phantomjs: {
cov: {
src: ['test/coverage.html'],
options: {
reporter: 'node_modules/mocha-lcov-reporter/lib/lcov.js',
output: 'build/bundled.lcov'
}
}
}
Is this the desired behavior? Is there any way to force the reporter's output to just go to the file, rather than both to the file and stdout?
When I use the json-cov reporter and output to a file (like in the setting below) the contents of the file get truncated. It ends up being invalid json with missing data.
mocha_phantomjs: { coverage: { options: { reporter: 'json-cov', output: 'coverage.json', urls: ['http://url/to/index.html'] }, }, }
If I run mocha_phantomjs from the command line like so...
mocha-phantomjs -R json-cov http://url/to/index.html > coverage.json
... then I get a completely populated file. This leads me to believe it's either an issue with the grunt plugin or with grunt itself. If it matters, the truncated file ends up being about 648k and the fully-populated file is 965k.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.