Can I hide failure details in mocha output? - javascript

Sometimes when running a set of mocha tests I don't care about failure details; I only want a list of tests with pass or fail. I've tried several reporters, but they all seem to output details for failures. I like the default spec reporter structure, but I can't find how to hide the details.
Here's an illustrative example. For these tests:
const assert = require('assert')
describe('test test', function() {
it('should pass', function() {
})
it('should fail', function() {
assert(false)
})
})
Which gives output like this:
test test
✓ should pass
1) should fail
1 passing (9ms)
1 failing
1) test test
should fail:
AssertionError [ERR_ASSERTION]: false == true
+ expected - actual
-false
+true
at Context.<anonymous> (test-solution.js:69:5)
but what I want is just this:
test test
✓ should pass
1) should fail
1 passing (9ms)
1 failing
Am I missing something obvious, or are these details just not something I can suppress?

I wanted to hide this too and for me it seems also that most default reporters are not so nice. Each line that is useless will cost us time. In my eyes it should be very simple to customize the output.
Building a own custom reporter is the correct answer to your question. However - as this took me to long - here a very short and easy alternative: Disable the reporter and do some logs on the events.
const Mocha = require('mocha');
let file = './devtest.js';
let passCount = 0;
let errors = [];
// start with disabled reporter
const mocha = new Mocha({ reporter: function () {} });
mocha.addFile(file);
console.log('\n===== start mocha file ' + file);
mocha.run()
.on('pass', function (test) {
passCount++;
logSuccess(test.title);
})
.on('fail', function (test, err) {
errors.push({test, err});
logError(test.title);
})
.on('end', function () {
console.log();
console.log(' -------------------------');
logSuccess(passCount + ' tests passed');
logError(errors.length + ' tests failed');
// do something here - like:
// callback(errors)
});
function logSuccess (str) {
console.log('\u001b[32m ✓ \u001b[0m\u001b[90m' + str + '\u001b[0m');
}
function logError (str) {
console.log('\u001b[31m ✖ ' + str + '\u001b[0m');
}
Of course this has some features less compared to the standard reporter, but extending is pretty simple - you have all the errors and data. So it is very fast.
Perhaps anybody else can post a very simple working example a custom reporter for that - for me the custom reporter broke my console output and I was not interested in more debugging.

Related

How do I get everything covered?

I have a node module I'm trying to write unit tests for. Here's a part of the module:
function _write(level, message) {
if (level <= _current) {
message = message || "No message provided.";
const consoleFn = consoleFunction[level];
const logFn = console[consoleFn];
logFn(`${levelPrefix[level]}: ${message}`);
}
}
When I run the tests including this one:
test('test writing to error log', () => {
logger.__Rewire__('_write', function (level, message) {
console.error(`ERROR: ${message}`);
});
const spy = jest.spyOn(logger, 'error');
logger.error('error message');
expect(spy).toHaveBeenCalledTimes(1);
});
However after running the tests it still looks like the _write function isn't showing up in the coverage results. Is there a way to get jest to understand all the lines of code are getting exercised? Note that "Uncovered Line #s" refers to the exact line numbers of the _write function. I had hoped that by rewiring the function this would make my coverage 100%.
It looks to me like you are mocking the _write function for that test. I don’t expect those lines to get run in this case.
You could write another test that actually uses the _write function.

Externally determine which test cases fail - Javascript

I am working on an problem for which i need to detect which test cases fail for any javascript/node.js application, when that application's test suite is run. I need to determine this in a programmatic manner.
Mocha testsuite output result
Consider an example of test output above, for this example I would like to write an external javascript script that can tell me which particular test case failed.
Currently the only solution in my mind is; executing npm test in a javascript child process and read its output from the stdout stream, parse the output and extract necessary information, something like this.
const { spawn } = require('child_process');
const chalk = require('chalk');
const child = spawn('npm.cmd',['test']);
line = 0
child.stdout.on('data', (data) => {
console.log(`${chalk.bgBlue('line = ' + line)} , data = ${data}`);
line++;
});
However, this would be a very strict approach. I would like a more generic way of going about it, that can work for a variety of test modules(not just mocha).
Help would be appreciated !
You can get the state for every test after the execution into the code. So you can know if the test has been passed or not.
The code you need is quite simple. Something like this:
afterEach(function () {
const state = this.currentTest.state;
if (state !== "passed") {
//Do whatever you want with this value
}
});
For example, if you want to store into code, which test has been failed, then you can code this:
var testFailed = []
describe('test', function () {
afterEach(function () {
const state = this.currentTest.state;
if (state !== "passed") {
testFailed.push(this.currentTest.title)
}
});
after(function(){
console.log(testFailed)
})
it('test1', () => {
assert.equal(1, 1)
});
it('test2', () => {
assert.equal(1, 2)
});
})
And the output will be:
test
√ test1
1) test2
[ 'test2' ]
1 passing (15ms)
1 failing
Now you can play with this. You can use the variable to do your work, or even you can create a file or whatever you want to store the info.

Javascript sync with wd-sync in mocha test

can you help with my sync-problem in mocha test?
I have this test in mocha, I use wd-sync and resemble packages.
wd-sync is for avoiding Promises and callback hell and it is great package, but I figured out that it has some problems. It doesn't wait for resemble so this part is executed out of test cases. I tried add resemble to Promise but without success so now I am out of ideas.
This is my log from mocha - As you can see, console.log(inside) is executed out of test cases PROMISE_002, test pass, but it should fails. After all hook fails, because expected is executed during this step.
Screenshot functionality
1
2
3
4
✓ PROMISE_002 (3714ms)
inside
inside2
1) "after all" hook
1 passing (16s)
1 failing
1) Screenshot functionality "after all" hook:
Uncaught AssertionError: expected '0.01' to equal 5
My test:
it.only('PROMISE_002', wrap(function(){
const date = new Date();
const path = `${date.getFullYear()}${date.getMonth()}${date.getDate()}`;
const dir = `archiveServer/${path}`;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
console.log(1);
driver.saveScreenshot(`archiveServer/baseline/PROMISE_002.png`);
console.log(2);
driver.saveScreenshot(`${dir}/PROMISE_002.png`);
console.log(3);
resemble(`${dir}/PROMISE_002.png`)
.compareTo('archiveServer/baseline/PROMISE_002.png')
.onComplete(function (data) {
//if (data.misMatchPercentage > 0.5) {
console.log('inside');
data.getDiffImage()
.pack()
.pipe(fs.createWriteStream(`${dir}/PROMISE_002-diff.png`));
console.log('inside2');
expect(data.misMatchPercentage).to.equal(5);
});
console.log('4');
}));

How to test a custom module running node-fluent-ffmpeg (an async module)?

How do I test a custom module which is simply running a node-fluent-ffmpeg command with Mocha&Chai?
// segment_splicer.js
var config = require('./../config');
var utilities = require('./../utilities');
var ffmpeg = require('fluent-ffmpeg');
module.exports = {
splice: function(raw_ad_time, crop) {
if (!raw_ad_time || !crop) throw new Error("!!!!!!!!!! Missing argument");
console.log("##### LAST SEGMENT IS BEING SPLITTED.");
var segment_time = utilities.ten_seconds(raw_ad_time);
var last_segment_path = config.akamai_user_base + 'segment' + (segment_time + 1) + "_" + config.default_bitrate + "_av-p.ts?sd=10&rebase=on";
var command = ffmpeg(last_segment_path)
.on('start', function(commandLine) {
console.log('##### COMMAND: ' + commandLine);
})
.seekInput('0.000')
.outputOptions(['-c copy', '-map_metadata 0:s'])
.duration(crop)
.on('error', function(err, stdout, stderr) {
throw new Error('##### VIDEO COULD NOT BE PROCESSED: ' + err.message);
console.log('##### VIDEO COULD NOT BE PROCESSED: ' + err.message);
})
.output('public/' + 'segment' + (segment_time + 1) + "_" + config.default_bitrate + "_av-p.ts").run();
}
}
Here is what I tried:
// test/segment_splicer.js
var expect = require('chai').expect;
var segment_splicer = require('../lib/segment_splicer');
describe('Segment Splicer', function() {
it('should work', function(done) {
expect(segment_splicer.splice(1111111, 20)).to.throw(Error);
done();
});
});
I get this:
1) Segment Splicer should work:
AssertionError: expected undefined to be a function
Because I receive undefined from segment_splicer.spice method.
Thank you!
This test should be passing.
A test will only fail if you either assert or expect something in the test which is not true, or if the subject under test throws an uncaught error.
You are not asserting anything in your test, and the only error your subject will throw is if you pass less than 2 arguments, which is not the case in your test.
The ffmpeg method also seems to be asynchronous, which is not compatible with the way you have structured your test.
There are many examples available on setting up async tests, including:
How Mocha Makes Testing Asynchronous JavaScript Processes
Fun
Asynchronous Unit Tests With Mocha, Promises, And
WinJS
Testing Asynchronous
JavaScript
You've gone some way to doing this by referencing the done argument. When this is specified, Mocha will wait until it is called before considering the test finished.

Testing ajax requests using jasmine returns TypeError

The description of the task. I want to test the code that loads a list of resources using $.get.
So, the source code:
fetchTemplates: function(list, cb){
var promises = [],
$container = $('#templates');
Object.keys(list).forEach(function(tplSelector){
if($(tplSelector).length > 0){ return; }
var promise = $.get(list[tplSelector]);
promise
.done(function(tplHtml){
$container.append(tplHtml);
})
.fail(function(){
console.warn('Template "' + tplSelector + " not found by url:" + list[tplSelector]);
});
promises.push( promise );
});
return $.when.apply($,promises).done(cb);
}
The test suite:
it("Correct template fetching", function (done) {
var fetchResult = viewManager.fetchTemplates({
'#helpTpl': 'somecorrectaddress'
});
fetchResult.done(function () {
expect(true).toBeTruthy();
done();
});
fetchResult.fail(function () {
expect(false).toBeTruthy();
done();
});
});
What it generates. Test passes, but generates an error:
TypeError: 'null' is not an object (evaluating 'this.results_.addResult')
at jasmine.js?2348
So, the test case marks as passed. But whole test suite still generates the error above (and this method is the only one async, other parts are trivial to test). My thought was that since the tested method contains async operations and promises - results were not properly handled and thus TypeError. So I added jasmine async "done()" to handle the issue - unfortunately nothing changed. Also worth noting that if I leave only one test in the suite using "iit" - no error is generated. Search didn't find similar cases. Any ideas?
You need to wrap your async calls in 'runs()' with 'waitsFor()' after, read the documentation here. I've never worked with jquery, but try something like the following inside your it function:
var done = false;
var that = this;
runs( function() {
//your async test goes here
that.done = true;
});
waitsFor( function() {
return that.done;
}, "async code failed", 2000);

Categories

Resources