繁体   English   中英

TypeError:无法读取undefined的属性'toBeA'

[英]TypeError: Cannot read property 'toBeA' of undefined

我试图在nodejs中使用expect来测试我的代码。 我需要在代码中期望并且我想使用期望的函数toBeA()。 但不幸的是,我收到错误,无法解决它。 所以,我在这里发帖。

 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'); }); 
这是我的utils.js文件

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

当我运行代码时,我收到此错误

  TypeError: Cannot read property 'toBeA' of undefined 

你不能连锁测试。 toBe不返回任何内容,因此错误。 你要

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

(虽然第一个意味着另一个,所以你也可以省略它)

这工作正常而不是toBeA()

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

期望断言库已经改变了所有权。 它被移交给Jest团队,他们以无限的智慧创造了一个新的API

您仍然可以像以前一样安装expect,“npm install expect --save-dev”,目前版本为21.2.1。 大多数方法名称将保持不变,除了少数,包括'toExist(),toBeA()'。

当使用npm i expect --save-dev命令for install expect时,自动安装expect的最后版本。 在最后一个版本链中不起作用。

要解决此问题,您应该使用以下命令:

npm i expect@1.20.2

在这个版本中你可以使用链接期望toNotBe

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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