Writing tests using mocha and chai - mocha.js

I am trying to write the test for the following function. I am reading chai documentation and there is a very large list of tests and I'm not getting an idea how to accomplish this.
it('should add a device', async function () {
await DB.add_device(1, 'fsrpup', 'NE2', undefined, 'minc', 0.15, 1444);
});

Related

Nightwatch.js Global afterEach not working

I am trying to use the global hook afterEach to close the browser after each test, but once the first test completes it does not perform the global afterEach. Here is an example of my global.js, any help would be amazing!
module.exports = {
afterEach: function (browser, done) {
browser.end(function () {
done();
})
},
}
documentation says it'll run after each test suite, not after each test, so that might be what you're experiencing :)

How to pass custom data to Jasmine or a Reporter?

I have some Protractor tests using Axe (AxeBuilder) like the following:
var AxeBuilder = require('path_to_the/axe-webdriverjs');
describe('Page under test', function() {
'use strict';
it('should be accessible', function() {
AxeBuilder(browser.driver).analyze(function(results) {
expect(results.violations.length).toBe(0);
});
});
});
How would I go about passing results.violations out to Jasmine so that it can be reported in my Jasmine Reporter?
I am currently looking to use the following Jasmine JSON Reporter:
https://github.com/DrewML/jasmine-json-test-reporter
But I will eventually customise this to output HTML.
I found a fix for this in the end.
It turns out that the solution is to write a custom Jasmine matcher, like this: http://jasmine.github.io/2.4/custom_matcher.html
This allows you to control what information is passed out to the result.message.

mocha returns a promise, false positive passes, and throws exception indicating error

I'm writing unit tests using Mocha and shouldjs, and bluebird.
According to the documentation (http://shouldjs.github.io/#assertion-finally) I should be able to return a Promise, and get it tested.
It is being run, but not tested. An assertion is thrown, but the test seemingly passes
Here is my code. It's pretty well straight out of the shouldjs docs:
'use strict';
require('should');
var Promise = require('bluebird');
describe('demo should error', function () {
it('I should fail - but Im not', function () {
var prm = new Promise(function(resolve, reject) { resolve(10); });
return prm.should.be.finally.equal(9);
});
});
When I run this in mocha, I get the following:
>>> mocha tests/demo.js
(node) child_process: options.customFds option is deprecated. Use options.stdio instead.
․Unhandled rejection AssertionError: expected 10 to be 9
at Assertion.fail (/Users/andrew/projects/DELETE_ME/2016-02-07/node_modules/should/lib/assertion.js:91:17)
at Assertion.Object.defineProperty.value (/Users/andrew/projects/DELETE_ME/2016-02-07/node_modules/should/lib/assertion.js:163:19)
...
1 passing (14ms)
So an exception is thrown, but the test seemingly passes.
I also get a false positive when I use native Promise, not bluebird, but the stack trace isn't shown.
Any help gratefully received...
I was using an outdated Mocha...
npm i -g mocha
Did the trick

Running spec after promise has been resolved

I came across an issue with running a spec that should be executed after a promise has been resolved. See the commented simplified example below.
I tried using IIFE or calling done() function in the spec but none of these seemed to work.
// getIds() is a simple promise which returns an array of ids
getIds().then(function (ids) {
console.log('IDS: ' + ids); // all good so far
// This test is never run
it('dummy test', function () {
console.log('TEST HAS BEEN RUN');
});
});
You can use browser.wait() to wait until your promise is complete. Or you can put your test inside the then block:
it('should test', function() {
getIds().then(function (ids) {
// some action.
expect()...
});
});
Also, you can put the promise in a beforeEach or a beforeAll (jasmine 2). Assign the ids to a variable declared inside a describe. The value should be available for your test to use.

Using Qunit module setUp tearDown to test ajax

I have been finding it quite difficult to get up and running with Qunit for testing jQuery ajax.
In particular I am stumped at trying to use Qunit's module construct with a setUp and tearDown method to reduce repeated code. The following works:
test("ajax request is 200 OK", function () {
var xhr = sinon.useFakeXMLHttpRequest();
var requests = sinon.requests = [];
xhr.onCreate = function (request) {
requests.push(request);
};
var callback_success = sinon.spy();
$.ajax('/course/data', {
success: callback_success,
});
equal(sinon.requests.length, 1);
equal(sinon.requests[0].url, "/course/data");
requests[0].respond(200, { "Content-Type": "application/json" }, '[]');
ok(callback_success.called);
});
I have a JSFiddle which shows a failing test (number 11). (The earlier tests I wrote as I was trying to get my head around everything).
Specifically my question is: why does the test report failure with 'requests is undefined' when I have declared var requests; at global scope on line 115?
Explanation gratefully received! (Edit: For some reason the JSFiddle linked shows problems with sinon.js, not evident when I run the JSFiddle from my fiddle account??)
Found solution to problem with requests being undefined in my JSFiddle. My test module's setup function was never being called. For some reason I was using camelCase to define setup and teardown:
setUp: function () { .....},
tearDown: function() {....}
When of course is should be just plain
setup: function () {...},
teardown: function () {...}
So, basically a case-sensitivity error on my part, compounded with the fact that I'm finding ajax, qunit and sinon quite mind-bending.

Resources