Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot read property 'toBeA' of undefined

I am trying to test my code using expect in nodejs. I required expect in code and I wanted to use a feature of expect called toBeA(). But unfortunately I am receiving error and unable to solve it. So,I am posting it here.

const utils = require('./utils');
const expect = require('expect');
it('should add two numbers', () => {
    var result = utils.add(33,17);
    
    expect(result).toBe(50).toBeA('number');
}); 

   
This is my utils.js file

module.exports.add = (a,b) => {
    return a+b;
};

When I run the code I receive this error

 TypeError: Cannot read property 'toBeA' of undefined
like image 912
Adil Chowdhury Avatar asked Nov 30 '25 16:11

Adil Chowdhury


1 Answers

You cannot chain tests. toBe doesn't return anything, hence the error. You want

expect(result).toBe(50);
expect(result).toBeA('number');

(although the first one implies the other so you might as well omit it)

like image 148
Felix Kling Avatar answered Dec 02 '25 06:12

Felix Kling