简体   繁体   中英

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 

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)

这工作正常而不是toBeA()

expect(typeof result).toBe('number');

The expect assertion library has changed ownership. It was handed over to the Jest team, who in their infinite wisdom, created a new API .

You can still install expect as before, "npm install expect --save-dev", which is currently at version 21.2.1. Most methods names will remain unchanged except for a few, including 'toExist(), toBeA()'.

when use npm i expect --save-dev command for install expect, automatically install last version of expect. in last version chain do not work.

for solve this problem you should use below command:

npm i expect@1.20.2

in this version you can use chain expect like toNotBe .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM