Karma + Jasmine reporting 0 tests run when there are tests - unit-testing

I'm trying to run some Jasmine tests in Karma but the tests are failing because it's saying that it ran 0 of 0 tests. Can someone tell me what I'm doing wrong?
The async request mock fires and hits the callback. Even when I go to the debugger, it says 2 tests completed in the debugger, but failing in the console. What gives?
describe('User Info Tests:', function () {
describe('Fetch User Info:', function () {
it("User Name should match", function(done) {
// mock async request
getUserProfile(1, 2, function (userProfile) {
var match = userProfile.displayName === 'Unit Test User';
expect(match).toBeTruthy();
done();
}, function (msg) {
done();
throw msg;
}); 
});
});
});
See the screenshot below of the debug console of the tests running. You will see the tests ran with a status of SUCCESS.

So the problem was I wasn't including the karam-requirejs plugin in the karam.conf.js file. Apparently it doesn't want you to include your own copy of require.js in the files collection. Once I added that plugin in, everything just worked.
frameworks: ['jasmine-jquery', 'jasmine', 'requirejs'],
plugins: [
'karma-phantomjs-launcher',
'karma-chrome-launcher',
'karma-jasmine-jquery',
'karma-jasmine',
'karma-requirejs'
],
Make sure the karma-requirejs plugin is actually installed through npm and in your package.json as well!

Related

Disable parse server logger during unit test of cloud code

all. I'm trying to implement an app that using parse server as backend.
And I'm trying to use mocha/chai to do the unit test for the cloud code function.
Like the code below.
const { expect } = require('chai');
const { server } = require('../index.js');
const Parse = require('parse/node');
let loggedUser;
let loggedUserSessionToken;
describe('SMS APIs', function() {
before('Initialize parse server.', function(done) {
Parse.initialize("appId");
Parse.serverURL = 'http://localhost:1337/parse';
done();
});
after('Close server', function(done) {
done();
server.close();
});
it('Pass', function(done) {
expect(1).to.equal(1);
done();
})
)};
After I run yarn mocha. The command line shows lots of log message. It is hard to read the mocha test result. like the picture below. Is there any method to turn off parse logger?
command line logger image
Take a look how the parse-server repo does it: helper.js
the key is to set 'silent: true' in the parse-server configuration.
I do this by using the wonderful config package, creating a test.js config that sets silent to true and then setting NODE_ENV=test when running my unit tests. Sounds like a lot to do, but this pattern is commonly reused for many things. Good luck!

How to exit gulp when unit test broken?

I am using gulp and mocha to run unit tests which is part of a gulp workflow to run my reactjs app. The unit test works:
gulp.task('mocha', function () {
return gulp
.src(['test/*.js'])
.pipe(mocha({
compilers: {
js: babel
}
})
})
However if the unit test is broken I would like to exit the whole gulp workflow. How can I do this?
You could try to just kill the process and restarting it when you want? Prehaps i do not fully understand your question, if so please ellaborate.
In cmd where you run the gulpscript you can press CTRL + C, and than Y to affirm. This stops the current script.
Mocha runs the tests. Gulp simply groups files, folder locations and pipe invokes mocha with this grouped information. What you are asking for is for a mechanism for mocha to communicate back to gulp the test results instead of its stdout if I read your question correctly. gulp automatically exits when mocha exits but if it does not then either you have a watch task or there is a allback in your gulp file that has not been resolved or this issue - [https://github.com/sindresorhus/gulp-mocha/issues/1][1]
You can use
.on('error', process.exit.bind(process, 1))
to check if the process exits
Or, if it is a callback issue, resolve the call with a done()
gulp.task('taskname', function (done) {
gulp.src('test/testfile.js')
.pipe(gulpmocha(),setTimeout(function() {
done(null);
}, 5000))
.on('error', process.exit.bind(process, 1))
});

When I run ember test and visit /tests the results are inconsistent, how can I troubleshoot why these are different?

I have been working with ember for a little over a month now and I have yet to find a solution to some testing inconsistencies I have been experiencing.
The problem is that when I run ember test from the command line and visit /tests in the browser sometimes I see a different total number of tests. It seems like ember test with phantomjs as the test runner is skipping some tests. On top of that the results seem to be inconsistent as well.
For instance, I have a simple acceptance test:
import Ember from 'ember';
import startApp from '../helpers/start-app';
var App;
module('Acceptance: Login', {
setup: function() {
App = startApp();
},
teardown: function() {
Ember.run(App, 'destroy');
}
});
test('Page contents', function() {
visit('/login');
andThen(function() {
equal(find('form.login').length, 1);
});
});
When I visit /tests, all of my tests pass, however when I run Ember test I get one failure:
not ok 1 PhantomJS 1.9 - Acceptance: Login: Page contents
---
actual: >
0
expected: >
1
Log: >
...
Thanks in advance for any help.
I had the same frustration as you until I looked a bit closer at what was being counted.
When I run my tests in a browser, it shows how many assertions are being run. When I run phantomjs (via 'ember test' command line), the log only reports how many tests are run. There can be many assertions in a test.
If I scroll to the very bottom of the page after a test run is complete in a browser, I see that the number next to the final test matches the total number of tests run in phantomjs.
As for why your test is breaking in phantomjs, it could be due to a number of things. Without seeing your handlebars and implementation it can be hard to tell, but I've seen problems with timing and also jquery binding issues that fail only in a headless browser (aka phantomjs).
If you post some more specifics, I may be able to help.

Stack trace for Jasmine test with the Resharper 7 test runner

How do you get the Resharper 7 test runner to show the stacktrace for Jasmine tests.
My setup is Resharper 7 (build in Jasmine) testrunner and PhantomJs. When executing any failing test the error message always ends with:
Exception doesn't have a stacktrace
In the 1.6 "Lavender" version of Phantom has added the feature to print the stacktrace when an error occurs.
To replicate this just create a mytest.js file and add the following code to it:
describe("A suite", function() {
it("contains spec with an expectation", function() {
expect(true).toBe(false);
});
});
Sorry, I don't use Resharper, but I used to face the same issue with phantomjs and jasmine ConsoleReporter.
I think it boils down to the fact that jasmine does not throw an Error message for failed expectations and that the stack is captured by phantomjs only when error is actually thrown (jasmine.js):
jasmine.ExpectationResult = function(params) {
...
var trace = (params.trace || new Error(this.message));
};
Changing that line as follows fixed it for me:
var err;
try { throw new Error(this.message); } catch(e) { err = e };
var trace = (params.trace || err);
In the spec file, where you have your javascript (jasmine) unit tests, you need a reference to the source that is being tested.
Normally you have this in the SpecRunner.html, but Resharper rolls it's own SpecRunner.
Add this reference line to the top of the XyzSpec.js file
/// <reference path="/js/path-goes-here/your-file-here.js" />
describe("Utils", function () {
describe("when calculating quantity", function() {
...
Drove me almost nuts until I started looking around in Resharper's spec runner.
PS: If a new problem shows up 'Unit Test Runner failed to load test assembly' and you have Chrome as default browser, change the browser for javascript unit tests in the Resharper options.
Got a good response from the Jetbrains Resharper team when I logged the issue. They fixed it and it's in the 7.1 release of Resharper, which can be downloaded from their EAP site

Anyone have a working Jasmine unittest that runs in Resharper 7 EAP?

I'd really like to be using the R#r test running for my javascript unittests. R#r 7 EAP recognizes the tests but when I launch them the runner shows '... Test wasn't run' for all tests.
I can't find any info on what the R#r test runner expects in terms of configuration/directory structure.
Directory structure and sample test posted here
A basic/hardcoded jasmin unit test does work under R#r 7 EAP. This implies that jasmine is baked in to R#r i guess. Have an open question regarding same.
describe('resharper jasmine testrunner', function () {
describe('simplest possible test', function () {
it('should execute', function() {
expect(true).toBe(true);
});
});
});
Got a pointer on the R#r forum that doc comment references are your 'includes' (or jsTestDriver.conf equiv).
/// <reference path="../../libs/testing/jasmine/jasmine.js"/>
/// <reference path="../../libs/crunch/jquery-1.6.2.js"/>
Have yet to get my real tests passing though the now run. Investigating fixture paths next.
I'm not sure how advanced of a solution/structure you're looking for, as I just started looking into this myself, but i have a simple example test "working"*... (using Resharper 7 EAP 7.0.56.103). As far as i know, you can structure your test files anyway/where you want, as long as you include references to all of its dependencies with <reference path="foo/path/file.js" />)
*These pass inside the ReSharper unit tests session window. The browser does not show me any sort of test info but rather a blank screen with some jasmine-related html in the source.
/scripts/cheese.js
function Cheese() {
return {
isGood: true
};
}
/tests/cheeseTest.js
/// <reference path="/scripts/cheese.js"/>
describe('simplest possible test', function() {
var cheese = null;
it('cheese is good', function() {
cheese = new Cheese();
expect(cheese.isGood).toBe(true);
});
});