I am testing javascript code that throws an exception (temporarily, early TDD state) but jasmine passes the test.
Is there any way to set up jasmine such that it fails with an unexpected exception?
Are there other javascript unit test frameworks that do not pass such tests?
Make sure you are using the latest version 1.2.0.
The code bellows fails:
describe("must fail on error", function(){
it("a + 1 should produce an error", function(){
expect(a + 1).toEqual(2);
})
});
I stumbled on this post searching for something else related to Jasmine testing. Before seeing this question, I saw this a blog post on testing exceptions with Jasmine. In that post, the author uses the bind command like this:
it('should allow us pass in parameters', function () {
expect(myOtherProcedure.bind(null, 10, 'you generated: ')).not.toThrow();
});
Related
I know how to check if a test failed in the afterEach() method of mocha: That's explained here: detecting test failures from within afterEach hooks in Mocha
But what about the people using suite and test (tdd) instead of describe and it??
How can I check if the current test failed here? The same code won't work because state would be undefined:
teardown(async () => {
// check if failed:
if (this.currentTest.state === 'failed') {
console.log("fail");
}
});
It seems that it works a little bit different with tdd (using suite and test).
Accessing this.ctx.currentTest instead of this.currentTest worked for me.
Example:
if (this.ctx.currentTest.state === 'failed') {
console.log(`'${this.ctx.currentTest.title}' failed`);
}
I am trying to test my node.js code using Mocha, Sinon, and chai.
var callback = function (err, resultSet) {
should.exist(resultSet);
stubbedExecuteSqlQuery.should.be.called;
done();
};
stubbedExecuteSqlQuery.yields(null, expectedResultSet);
db.getResults(param1,param2, user, callback);
when the above code gets executed, it throws an error :
Invalid Chai property: called. Did you mean "all"?
The code used to work fine on chai version ^3.5.0 but after my recent package upgrade to ^4.1.2 the code has stopped working and has started throwing such errors.
I tried searching for it on the internet but could not find any useful information.
Any help will be appreciated. Thanks in advance!
I had a similar issue, I think it had something to do with using .yields
I ended up using .calledOnce . Try the following:
assert(stubbedExecuteSqlQuery.calledOnce);
The upside with this is that if needed you can do .calledTwice etc..
I have a Karma test which looks like this:
it('testing if test will fail on exception', function () {
expect(true).toBe(true);
throw "an exception";
expect(false).toBe(true);
});
The problem is this test returns valid, because the exception is thrown before the second test condition was checked. I need a solution that will work on all tests, so implementing try/catch is not the solution I am searching for. Is there a way to set Karma/Jasmine configs so that all tests fail on exception thrown? Any other good ideas?
Help me please to find way to validate test result in Protractor not from "expect" function.
I have such code:
describe("The 'toEqual' matcher", function() {
it("works for simple literals and variables", function() {
expect(12).toEqual(12);
if (this.results_.failedCount === 0) {
console.log("This test passed")
}
else{
console.log("This test failed")
}
});});
but when I execute this test I have such message:
Failed: Cannot read property 'failedCount' of undefined.
May be you know such another solution to fix my problem?
Thanks for support.
You should not be counting passes and failures yourself directly in the test. Let the protractor and jasmine runner worry about it. If you need to have a control over it, look into making a jasmine reporter or using one of the many existing, like the jasmine-spec-reporter, for instance.
I am creating HTTP tests with frisby.js which works on top of jasmine.js.
I also have to create some mongoDB objects to test against.
The problem is when I want to clean up these DB objects. When one of the expects fail I want to intercept that and call my own cleanup function. This means that after each failed test, I won't be able to remove the test objects from the DB.
The afterEach function in jasmine does not work properly and jasmine does not have any support for afterAll or beforeAll yet.
That is why I have made the tests as they are today.
it("testing userform get with correct userID and expect correct return", function() {
var innerUserId = userID;
frisby.create('Should retrieve correct userform and return 200 when using a valid userID')
.get(url.urlify('/api/userform', {id: innerUserId}))
.expectStatus(200)
.afterJSON(function(userform){
// If any of these fail, the after function wont run.
// I want to intercept the error so that I can make sure that the cleanUp function is called
// afterEach does not work. I have tried with done()
var useridJSON = userform.UserId.valueOf();
var firstnameJSON = userform.firstname.valueOf();
var surnameJSON = userform.surname.valueOf();
expect(firstnameJSON).toMatch(testUser.firstName);
expect(surnameJSON).toMatch(testUser.surname);
expect(useridJSON).toMatch(innerUserId);
})
.after(function(){
cleanUp(innerUserId);
})
.toss();
});
I am wondering if there is a way to intercept the error for "expect" in frisby or jasmine so that I can make a call to my own cleanup function before exiting.
Full example here
The quickest solution to this problem is to wrap the error code in a try-catch.
This is because if a javascript error occurs, jasmine will NOT keep running assertions. This is different from an assertion error. If an assertion error occurs, jasmine and frisby will keep on testing all the other assertions and then do the "after"-function.
.afterJSON(function(userform){
try {
var useridJSON = userform.UserId.valueOf();
var firstnameJSON = userform.firstname.valueOf();
var surnameJSON = userform.surname.valueOf();
catch(e) {
cleanUp(innerUserId);
// Can do a throw(e.message); here aswell
}
expect(firstnameJSON).toMatch(testUser.firstName);
expect(surnameJSON).toMatch(testUser.surname);
expect(useridJSON).toMatch(innerUserId);
})
This is not the pretty way, but works.
I ended up adding the throw(e) and placed the expects in a finally scope. This way I got jasmine to present all the errors that occured in the test.
As for "before exiting", how about this:
process.on('uncaughtException', function(err) {
console.error(' Caught exception: ' + err);
});