Test Babel transpiled(AMD) ES6 code in Karma + Jasmine - jasmine

I have written test in ES6, which is transpiled to ES5 in AMD format using Babel.
Now I want to run the test using Karma(+requirejs) and Jasmine, but getting following error:
Error: Mismatched anonymous define() module: function (exports) {
"use strict";
describe("App", function () {
it("tests something", function () {
expect(true).toBe(true);
});
});
}
The test file looks like:
define(["exports"], function (exports) {
"use strict";
describe("App", function () {
it("tests something", function () {
expect(true).toBe(true);
});
});
});
//# sourceMappingURL=app.js.map

I'm not transpiling to ES5 first, but use karma-babel-preprocessor.

Related

Using classes in different .ts files in jasmine

I am writting simple test in jasmine framework. I have following files stored in one folder:
maintest.ts
helper.ts
Workflow1.ts
Workflow2.ts
Workflow files have content as following (example):
import {element, by, browser, protractor} from "protractor";
import {Helper} from "../../helper";
export class Workflow1/2
{
static Foo1() {
let element1;
let element2;
describe('check all fields', function () {
it('check foobar', function () {
element1.isVisible();
});
it('check foobar2', function () {
element2.isVisible();
});
}
static Foo2() {
let element3;
let element4;
describe('check all fields', function () {
it('check foobar', function () {
element4.isVisible();
});
it('check foobar2', function () {
element3.isVisible();
});
}
}
And the maintest.ts is:
import {browser} from "protractor";
import {Helper} from "./helper";
import {Workflow1} from "./Workflow1";
import {Workflow2} from "./Workflow2";
describe ('Regression Tests', function() {
beforeAll(function () {
console.log('====================Start');
});
describe('Basic workflow', function () {
Workflow1.Foo1();
Workflow1.Foo2();
Workflow2.Foo2();
Workflow2.Foo2();
});
});
but when I run it, nothing has run correctly - I get this error:
Error: Error while waiting for Protractor to sync with the page: "window.angular is undefined. This could be either because this is a non-angular page or because your test involves client-side navigation, which can interfere with Protractor's bootstrapping. See http://git.io/v4gXM for details"
but if I comment:
//Workflow1.Foo2();
//Workflow2.Foo2();
//Workflow2.Foo2();
the Workflow1.Foo1 works perfectly fine.
Can't I use different methods from different files? It works with helper, where I have login and logout methods...
I think I got this. My code was 'quite' long with different describes, when I minimalized it to 2, it started working :)
EDIT: As I mentioned in comment below, each method in Workflow1 and Workflow2 files must have at least one describe and at least one it inside - having only describe without it throws error

Jasmine Protractor: Test case failing when run with grep option

I created few test cases in jasmine/protractor. When i run these using
protractor conf.js
all test case run and pass successfully.
But when i run using
protractor conf.js --grep="dummytag3"
then selected test case get executed, but it fails. I am finding it really strange that why a test case may fail with tagging and pass without tagging.
spec.js
'use strict';
var HomePage = require('./pages/homePage.js');
var BusPage = require('./pages/busPage.js');
describe('Login cases' , function() {
var homePage= new HomePage();
it('Login without username & password #dummytag3' , function() {
homePage.mainLoginButtonClick();
homePage.popupLoginButtonClick();
expect(homePage.errMsgUsername).toEqual('Please enter valid Email or Mobile Number');
});
});
homePage.js
'use strict';
var HomePage = function () {
browser.get('https://www.mobikwik.com');
browser.driver.manage().window().maximize();
};
HomePage.prototype = Object.create({}, {
//Login
mainLoginButton: { get: function () { return element(by.id('qa-mbkLogin')); }},
mainLoginButtonClick: { value: function () { this.mainLoginButton.click(); }},
popupLoginButton: { get: function () { return element(by.xpath('//*[#id="loginInfo"]/div[3]/p/button')); }},
popupLoginButtonClick: { value: function () { this.popupLoginButton.click(); }},
errMsgUsername: { get: function () { return element(by.xpath('//*[#id="loginInfo"]/div[1]/span')).getText(); }},
});
module.exports = HomePage;
error:
Error: Error while waiting for Protractor to sync with the page: "window.anular is undefined. This could be either because this is a non-angular page or ecause your test involves client-side navigation, which can interfere with Protactor's bootstrapping. See http://git.io/v4gXM for details"

How to send the success of a test to testingBot from a Protractor project?

Following the testingBot example for protractor-based projects I got this code
var TestingBot = require('testingbot-api');
describe('Protractor Demo App', function () {
var tb;
beforeEach(function () {
tb = new TestingBot({
api_key: "master_key",
api_secret: "secret_007"
});
});
afterEach(function () {
browser.getSession().then(function (session) {
tb.updateTest({
'test[success]': true/*where do I get this 'test[success]' attribute? */
}, session.getId(), function () {
console.log("Hi! :D");
});
})
});
it('should have a title', function () {
browser.get('http://juliemr.github.io/protractor-demo/');
expect(browser.getTitle()).toEqual('Super Calculator');
});
});
I need to send the success of the test back through the tb.updateTest() but I don't know where I get the value of a passed or failed test. For now the value is a static true. I'd appreciate a jasmine approach too.
You can use a custom reporter with Jasmine.
There you can hook into specDone or suiteDone which has a result parameter, containing the test's success state.
You can then use this state to write a custom report or send it to somewhere else.

Using Jasmine spyOn to mock function in Browserify module

I'm trying to run a unit test on a function (testFunc). testFunc calls another function (secondFunc) which I would like to mock. Can I mock secondFunc so that when it is called in the context of testFunc, the spiedOn version of secondFunc is called? If not, how should I reformat my browserify module to make it testable?
Currently the setup looks something like this:
app.js (Browserify Module)
module.exports = (function () {
function testFunc() {
secondFunc();
}
function secondFunc(){
console.log('not mocked!');
}
return {
testFunc, secondFunc
};
})();
test.js (Jasmine Test)
describe("testFunc", () => {
let app = require('./app');
beforeEach(() => {
spyOn(app, 'secondFunc');
});
it("should call secondFunc spy", () => {
app.testFunc();
expect(app.secondFunc).toHaveBeenCalled();
});
});
The way you have it now, the spyOn is replacing the secondFunc property on your returned object with a proxy, but your code calls the secondFunc function that is inside the closure of the anonymous function. There are several ways to restructure your code to better expose the functions.
You could structure your module this way:
exports.testFunc = function() {
exports.secondFunc();
}
exports.secondFunc = function(){
console.log('not mocked!');
}
which is a lot smaller, easier to read, and let you mock the secondFunc function.
The reason this is happening is because you are setting up a mock on the returned object, but the code is calling the internal function. What I've done in the past is something like this:
module.exports = (function () {
function testFunc() {
api.secondFunc(); // Call the API function, which is what is mocked
}
function secondFunc(){
console.log('not mocked!');
}
var api = {
testFunc, secondFunc
};
return api;
})();

Expected spy but got function error message with Sinon stubs?

I have the following test suite:
describe('rendering Bundle View', function () {
beforeEach(function () {
this.view = new Backbone.View();
this.renderStub = Sinon.stub(this.view, 'render', function () {
this.el = document.createElement('div');
return this;
});
this.view.render();
});
it('should have called render once', function () {
console.info('RENDERRRR' + (typeof this.renderStub));
expect(this.renderStub.calledOnce).toBe(true); // this passes
expect(this.renderStub).toHaveBeenCalled(); // this fails
});
});
Why does the first expect statement pass but the second fail? The second gives the error message: expected Spy but got Function even though Sinon stubs implement the spy API so it should return a spy??
figured it out. I think its because I was using a Sinon spy with a jasmine function that expected a jasmine spy hence it didn't allow me to use Sinon expect statements

Resources