问题
I've got a repository which is integrated with travis. I've got QUnit tests which I'd like to run from grunt/node server side and AMD (requirejs). This is the source of my AMD init.js:
(function () {
require.config({
baseUrl: "../src"
});
require(["../test/suites/basic",
'../test/qunit-extend',
'qunit'
], function(BasicTests) {
QUnit.config.autoload = false;
QUnit.config.autostart = false;
BasicTests.run();
QUnit.load();
QUnit.start();
});
}());
When I run those QUnit tests within my browser - everything works perfectly. But when I try to run them from grunt level (server-side using phantomjs), it fails. I get:
Running "qunit:all" (qunit) task
Testing test/index.html
Warning: PhantomJS timed out, possibly due to a missing QUnit start() call. Use --force to continue.
all the time. I was trying to do evetyrhing the same way as it's done in this tutorial, but still I get wrong results (phantom being hanged instead serving QUnit tests)...
回答1:
I am using grunt-contrib-qunit to run QUnit tests via grunt. It uses phantomjs internally.
I was getting the same error as the OP after upgrading grunt-contrib-qunit to the latest version (0.7.0):
PhantomJS timed out, possibly due to a missing QUnit start() call.
To fix this problem, I had to first load QUnit via require()
and then execute QUnit.start()
and define all my QUnit modules and tests after that.
The HTML file looks something like this:
<!DOCTYPE html>
<html>
<head>
<title>QUnit + RequireJS + PhantomJS</title>
<link rel="stylesheet" href="lib/qunit/qunit/qunit.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="lib/requirejs/require.js"></script>
<script src="mytests.js"></script>
</body>
</html>
Then the mytests.js
file:
require.config({
paths: {
'qunit': 'lib/qunit/qunit/qunit'
}
});
require(['qunit'], function(QUnit) {
QUnit.start();
QUnit.module('My Module');
QUnit.test('some normal test', function(assert) {
assert.ok(true, 'can run a normal QUnit test');
});
QUnit.test('some asynchronous test', function(assert) {
var done = assert.async();
setTimeout(function() {
assert.ok(true, 'can run an asynchronous QUnit test');
done();
}, 50);
});
});
回答2:
It's because the bridge that is injected into the page by grunt qunit is placed there before qunit is loaded by requirejs.
And it needs to be after. So your tests probably run, but grunt qunit does not know about it because it does not report back.
I did a quick test placing the bridge code at the end in your qunit extend module and it worked fine.
You could probably create a qunit bridge module and call that as well in your qunit extend or similar.
The code from the official bridge should work fine. Just make sure it's fetched after qunit.
Grunt qunit will still inject the script but just fail since QUnit is undefined, but probably won't do any harm to your tests.
来源:https://stackoverflow.com/questions/18392463/grunt-not-running-qunit-tests-on-phantom