Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
write Jasmine testcase for error cases so that it becomes green

I have a function that returns the largest number of a variant list of parameters, and if there are more than 5 parameters it will throw an error:

if (params.length > 5) {
  throw new Error('Too many arguments');
}

When we write testcase for unit-test in Jasmine, how do we expect the value for the case of such errors (so that the testcase would be successful - green color), because it is not the returned value of the function? Here is my code in testing:

const myLocalVariable1 = require('../src/get-bigest-number');
describe('CommonJS modules', () => {
  it('should import whole module using module.exports = getBiggestNumber;', async () => {
    const result = myLocalVariable1.getBiggestNumber(1, 3, 6, 5, 4, 7);
    expect(result).toBe(new Error('Too many arguments'));
  });
});
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

There is a Jasmine matcher toThrowError that can be used to check that an error is thrown. You can optionally pass a custom error and/or an error message as argument to check that you get the error you expect and not some other error.

In your case, it would be:

expect(() => {
  myLocalVariable1.getBiggestNumber(1, 3, 6, 5, 4, 7);
}).toThrowError('Too many arguments');

Note that you have to place your function call inside another function, otherwise the execution of your function triggers the error and breaks the test.

However, if your function does not take any arguments, you can simply pass the function to the expect:

expect(myFunctionWithNoArgs).toThrowError();
about 4 years ago · Juan Pablo Isaza Report

0

I think it should be like this (Don't assign it to result and then assert result):

expect(myLocalVariable1.getBiggestNumber(1, 3, 6, 5, 4, 7)).toThrow();

You can also do it the stock JavaScript way with try/catch:

it('should import whole module using module.exports = getBiggestNumber;', async () => {
    try {
     const result = myLocalVariable1.getBiggestNumber(1, 3, 6, 5, 4, 7);
    } catch (e) {
     expect(e.message).toBe('Too many arguments');
     // Not sure about the below one but you can try it. We have to use .toEqual and not .toBe since it's a reference type
     expect(e).toEqual(new Error('Too many arguments');
    }
  });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!