How to pass custom data to Jasmine or a Reporter? - jasmine

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.

Related

Writing tests using mocha and chai

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);
});

Jasmine failed tests automation

Is there any automation that I can use when a test fails on jasmine to prepare the same environment for a specific failing test? I'm using karma as the spec runner.
For example:
describe("this plugin", function(){
beforeEach(function(){
$("body").append("<input type='text' id='myplugintester'>");
this.plugin = $("#myplugintester").plugin({
cond1: true,
cond2: new Date(),
condetc: null
});
}
afterEach(function(){
$("#myplugintester").data("plugin").destroy();
$("#myplugintester").remove();
}
it("should show the correct value", function(){
expect(this.plugin.value).toEqual("somevalue");
});
it("should display 'disabled' when cond3 is not null", function(){
this.plugin.cond3 = "blabla";
expect(this.plugin.value).toEqual("somevalue");
});
});
When the second case fails, I have to write this to a test page to debug what goes wrong with the code.
var expect = function(){
// filler code
};
$("body").append("<input type='text' id='myplugintester'>");
this.plugin = $("#myplugintester").plugin({
cond1: true,
cond2: new Date(),
condetc: null
});
this.plugin.cond3 = "blabla";
console.log(this.plugin.value);
$("#myplugintester").data("plugin").destroy();
$("#myplugintester").remove();
Does any node package automate this? Or how do other developers react in this cases?
Note: I switched from grunt-jasmine to grunt-karma because of the speed. grunt-jasmine allowed me to run single test cases on browsers which I could debug with Chrome Developer Tools. I looked for several html reporters for karma but they only state the result on output HTML. They are not running the specs which I can interrupt and debug.
At last, I wrote a karma plugin to fill that gap. If someone needs that:
https://www.npmjs.com/package/karma-code-reporter

Webdriverio Node.js Mocha Chai test skips describe block

I am running this test and it seems that when the test get's to the function portion of my describe block, it skips the whole thing and gives a false positive for passing.
// required libraries
var webdriverio = require('webdriverio');
var describe = require('describe');
var after = require('after');
console.log("Lets begin");
describe('Title Test for google site', function() {
console.log("MARTY!!");
// set timeout to 10 seconds
this.timeout(10000);
var driver = {};
console.log("before we start");
// hook to run before tests
before( function (done) {
// load the driver for browser
console.log("before browser");
driver = webdriverio.remote({ desiredCapabilities: {browserName: 'firefox'} });
driver.init(done);
});
it('should load correct page and title', function () {
// load page, then call function()
return driver
.console.log("before site")
.url('http://www.ggogle.com')
// get title, then pass title to function()
.getTitle().then( function (title) {
// verify title
(title).should.be.equal("google");
// uncomment for console debug
// console.log('Current Page Title: ' + title);
});
});
});
// a "hook" to run after all tests in this block
after(function(done) {
driver.end(done);
});
console.log ("Fin");
This is the output I get
Lets begin
Fin
[Finished in 0.4s]
As you can see it skips everything else.
This is wrong and should be removed:
var describe = require('describe');
var after = require('after');
Mocha's describe and after are added to the global space of your test files by Mocha. You do not need to import them. Look at all the examples on the Mocha site, you won't find anywhere where they tell you to import describe and its siblings.
To get Mocha to add describe and its siblings, you need to be running your test through mocha. Running node directly on a test file won't work. And for mocha to be findable it has to be in your PATH. If you installed it locally, it is (most likely) not in your PATH so you have to give the full path ./node_modules/.bin/mocha.

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.

How can i test to make sure the file name is SpecRunner.html in Jasmine?

I want to build a test that makes sure the Jasmine Test is using a file called SpecRunner.html".
How do I do that?
You can check window.location (at least if you are running tests in browser).
Quick POC code:
describe("File name", function () {
it("ends with SpecRunner.html", function () {
var fileName = window.location.pathname.split("/").pop();
expect(window.location.pathname).toMatch(/SpecRunner.html$/);
});
});

Resources