before/afterAll() is not defined in jasmine-node - javascript

I'm trying to use the methods beforeAll and afterAll of jasmine, to create a suite of tests with frisby.js, because actually, frisby doesn't have a support for this methods. So, this is what I'm trying to do:
var frisby = require('frisby');
describe("setUp and tearDown", function(){
beforeAll(function(){
console.log("test beforeAll");
});
afterAll(function(){
console.log("afterAll");
});
//FRISBY TESTS
}); //end of describe function
If I change the methods before/afterAll to before/afterEach, is working, but when I'm using before/afterAll this error appears on console:
Message:
ReferenceError: beforeAll is not defined
Stacktrace:
ReferenceError: beforeAll is not defined
I have the jasmine version 2.3.2 installed on my project, so, I don't know what I need to do to integrate this method.

Use the jasmine library not the jasmine-node library. The second one does not support beforeAll and afterAll methods.
1- npm install -g jasmine
2- jasmine init
3- write the test in the spec folder:
describe("A spec using beforeAll and afterAll", function() {
var foo;
beforeAll(function() {
foo = 1;
});
afterAll(function() {
foo = 0;
});
it("sets the initial value of foo before specs run", function() {
expect(foo).toEqual(1);
foo += 1;
});
it("does not reset foo between specs", function() {
expect(foo).toEqual(2);
});
});
4- Run the tests --> jasmine

The current version of frisby doesnt suport this kind of setup. The community, like myself is eager to this feature like in this issue describes.
The team is working on this feature, but it will come in version 2 of the package that is in the way for more than a year now. More info at this link.

Related

Conflict between zone.js and Jasmine's clock

I am working with a Jasmine testing suite that includes both "vanilla" Jasmine tests along with Jasmine tests for some Angular 2 components. Because of Angular 2's inclusion, zone.js gets loaded. This creates a conflict with Jasmine's clock. For example, the following test fails with the error, Error: Jasmine Clock was unable to install over custom global timer functions. Is the clock already installed?
describe('an async test with zone.js present', function() {
beforeEach(function() {
jasmine.clock().install();
});
afterEach(function() {
jasmine.clock().uninstall();
});
it('cannot install jasmine\'s mock clock', function() {
var callback = jasmine.createSpy('setTimeoutCallback')
setTimeout(callback, 55);
jasmine.clock().tick(56);
expect(callback).toHaveBeenCalled();
});
})
Here is plunker for above code.
Short of delivering the Angular 2 tests separately from the "vanilla" tests, I am wondering what options might be available. For example, is it possible to perform the Jasmine clock's job with the zone? For example, is it possible to simulate the tick with the zone or flush all of the scheduled tasks before the assertion?
For me it works if you uninstall the clock in beforeEach. Its not recommended by jasmine and a bit strange because for uninstall it makes more sense to use afterEach. But calling uninstall before the first install call happens fixed it for me.
As stated on Angular documentation you should use tick function in a fakeAsync body which is part of the #angular/core/testing module.
Using your example and TypeScript it would look something like this...
import { fakeAsync, tick } from '#angular/core/testing';
...
it('cannot install jasmine\'s mock clock', fakeAsync(() => {
var callback = jasmine.createSpy('setTimeoutCallback')
setTimeout(callback, 55);
tick(56);
expect(callback).toHaveBeenCalled();
}));
The code which throws this here.
It implies that jasmine was loaded before Zone.js. Switch the loading order. Zone always needs to be loaded first.
Here's the fixed fork of your plunker:
<script data-require="zone.js#*" data-semver="0.4.1" src="https://cdn.rawgit.com/angular/zone.js/v0.4.1/zone.js"></script>
<script data-require="zone.js#*" data-semver="0.4.1" src="https://cdn.rawgit.com/angular/zone.js/v0.4.1/long-stack-trace-zone.js"></script>
<script data-require="jasmine#*" data-semver="2.4.1" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/boot.js"></script>
https://plnkr.co/edit/6iuvWFOZLqHWJIo4
This have been resolved by https://github.com/angular/zone.js/pull/1009.
zone.js and future angular will support jasmine.clock().

Jasmine unit-testing on Titanium module

I am currently trying jasmine-node to unit test my Titanium app. I am open to suggestions about switching to an other unit-testing framework if it can solve my problem but first, here is my question.
My installation of jasmine-node is working, I can perform very simple tests like this one :
var util = require('../app/controllers/utils.js');
describe("util test", function(){
it('should compute the sum between 1 & 2', function(){
var sum = util.computeSum(1, 2);
expect(sum).toEqual(3);
});
});
The above code tests the following function and works as expected.
exports.computeSum = function(a,b) {
return a+b;
};
When I try to test some code which call the Ti module though, it fails, saying "Ti is not defined".
describe("Ti.UI",function(){
it("create custom alert", function(){
var view = util.displayCustomAlert("title", "message");
should(view).not.be.null;
});
});
Above function is tested by the following test :
exports.displayCustomAlert = function(customTitle, customMessage){
return Ti.UI.createAlertDialog({
title:customTitle,
message:customMessage
});
};
How can I make jasmine-node work with Titanium ?
I'd recommend using TiShadow to run Jasmine tests or TiO2 to run Mocha tests for Titanium apps.

Jasmine running on Grunt cant find async functions

For some reason the async functions (waits, waitsFor, runs) in Jasmine aren't available when I run it from Grunt.
In Grunt:
jasmine:{
pivotal:{
src: 'src/**/*.js',
options:{
specs: 'spec/**/*.spec.js'
}
}
}
In Jasmine spec:
describe('jasmine', function(){
it("should find 'waits'", function(){
waits(1000);
});
it("should find 'waitsFor'", function(){
waitsFor(function(){}, 1000);
});
it("should find 'runs'", function(){
runs(function(){});
});
})
Jasmine output:
jasmine
× should find 'waits'
ReferenceError: Can't find variable: waits in file:///G:/Projects/myproj/spec/test.spec.js (line 3) (1)
× should find 'waitsFor'
ReferenceError: Can't find variable: waitsFor in file:///G:/Projects/myproj/spec/test.spec.js (line 6) (1)
× should find 'runs'
ReferenceError: Can't find variable: runs in file:///G:/Projects/myproj/spec/test.spec.js (line 9) (1)
Am I missing something?
turns out I'm used to using Jasmine 1.3, and the version used by grunt is 2.0. The syntax has changed in 2.0 and the tests need to be written according to the docs on http://jasmine.github.io/2.0/introduction.html

Mocha + RequireJS = AMD testing

I have a hard time connecting Mocha to RequireJS based application, may be You'll be able to come up with something :). After many hours when I've been trying to load AMD modules and simply console.log some 'fired' info that the module has been loaded... nothing happend had happened - the program just ended and printed out some mocha info.
var facade = requirejs(['../../public/js/scripts/widgets/widgets/article/main.js'],
function(mod) {
console.log('fired')
});
// run with: $ mocha -u tdd test.js --reporter spec
and than I've come up with the idea to fire just this to test callbacks:
setTimeout((function() {
console.log('fired');
}), 5000);
// run with: $ mocha -u tdd test.js --reporter spec
also didn't work. So finally I've run both with
$ node test.js
and finally it worked. So question is than: How to run Mocha test with callbacks handling, as those are essential for AMD testing?
The way you are doing it, mocha is not going to do anything with your file because it does not see a test suite in it. RequireJS is scheduled to call the callback but mocha exits before this has a chance to happen. Same with your timeout example.
The following gives you an example.
File test.js:
'use strict';
var requirejs = require("requirejs");
requirejs.config({
baseUrl: '.',
nodeRequire: require
});
suite('Something', function(){
var foo;
suiteSetup(function (done){
// This saves the module foo for use in tests. You have to use
// the done callback because this is asynchronous.
requirejs(['foo'],
function(mod) {
console.log("fired!");
foo = mod;
done();
});
});
suite('blah', function(){
test('blah', function(){
if (foo.test !== "test")
throw new Error("failed!");
});
});
});
File foo.js:
define(function () {
return {test: "test"};
});
When you run:
mocha -u tdd test.js
You'll see that the callback is fired and the test passes.
For the benefit of people reading this question and confused by the use of suite, suiteSetup, test... Mocha supports multiple interfaces. The code here is using the TDD interface (the OP invokes Mocha with -u tdd), which exports suite, suiteSetup, test, etc. In the default BDD interface, the equivalents are describe, before and it, respectively.
I have configured related boilerplate for using mocha in environment of RequireJS. It may be not exact what you want, but it may be helpful.
https://github.com/x2es/boilerplate-karma-mocha-chai-requirejs
One more note - assuming that your script placed in "/public" it makes sense to test it in browser environment instead nodejs. For this purposes you should to look at some test-runner like JsTestDriver (https://code.google.com/p/js-test-driver/) or karma-runner (http://karma-runner.github.io/). Or another...
In snipped provided in karma documentation (http://karma-runner.github.io/0.8/plus/RequireJS.html)
var tests = [];
for (var file in window.__karma__.files) {
if (window.__karma__.files.hasOwnProperty(file)) {
if (/Spec\.js$/.test(file)) {
tests.push(file);
}
}
}
requirejs.config({
// Karma serves files from '/base'
baseUrl: '/base/src',
paths: {
'jquery': '../lib/jquery',
'underscore': '../lib/underscore',
},
shim: {
'underscore': {
exports: '_'
}
},
// ask Require.js to load these files (all our tests)
deps: tests,
// start test run, once Require.js is done
callback: window.__karma__.start
});
introduced way when we force requirejs to preload all necessary spec-files using
require.config({
deps: ['array', 'of', 'our', 'spec', 'files']
})
In this environment each spec-file should be a regular RequireJS module.
Example of test spec for such environment:
define(['chai'], function(chai) {
var expect = chai.expect;
describe('bootstrap', function() {
it('should...', function() {
expect('a').to.equal('a');
});
});
});

Chai exports are not found in Mocha test

I have created simple Mocha test. It works perfectly when Node "assert" module is used. I run it from command line (Mocha is installed as a global node module):
$ mocha myTest.js
․
1 test complete (6 ms)
The script looks like this:
var assert = require("assert")
describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
assert.equal(-1, [1,2,3].indexOf(5));
assert.equal(-1, [1,2,3].indexOf(0));
})
})
})
Well, I tried to add Chai instead of assert library. I installed it first:
npm install chai
So, the directory node_modules has been created in my project. Great so far. Then, I altered the script to use Chai:
var chai = require("chai");
describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
[1,2,3].indexOf(5).should.equal(-1);
expect([1,2,3].indexOf(5)).to.equal(-1);
assert.equal([1,2,3].indexOf(5),-1);
})
})
});
It does not work, Mocha test fails with TypeError:
TypeError: Cannot call method 'equal' of undefined
I assume Chai did not define should so it is undefined.
How is this possible?
How can I make my tests run with Chai? I have tried to install Chai globally with no effect. I also ran the script with -r chai with no effect as well.
Apparently, Chai module is loaded, but does not define the variables (Object.prototype properties). How can I fix this?
var expect = require('chai').expect;
That will get your expect calls working. However, you also have a should call which comes from a different library entirely, so change
[1,2,3].indexOf(5).should.equal(-1);
to
expect([1,2,3].indexOf(5)).to.equal(-1);

Categories