【发布时间】:2017-06-02 05:06:10
【问题描述】:
对于使用Buffer#toString() 创建的字符串,如何在 Jest 断言中使用 expect(str).toBeInstanceOf(String)?
或者在这里expect(typeof str).toEqual('string') 是正确的做法吗?
详情:
这个测试用例,使用typeof,通过了:
it('should test a Buffer.toString() - typeof', () => {
const buf = new Buffer('hello world');
const str = buf.toString('hex');
expect(buf).toBeInstanceOf(Buffer);
expect(typeof str).toEqual('string');
// expect(str).toBeInstanceOf(String);
});
但是,这个使用 .toBeInstanceOf() 的测试用例失败了:
it('should test a Buffer.toString()', () => {
const buf = new Buffer('hello world');
const str = buf.toString('hex');
expect(buf).toBeInstanceOf(Buffer);
// expect(typeof str).toEqual('string');
expect(str).toBeInstanceOf(String);
});
这是它的 Jest 输出:
FAIL ./buffer.jest.js
● should test a Buffer.toString()
expect(value).toBeInstanceOf(constructor)
Expected value to be an instance of:
"String"
Received:
"68656c6c6f20776f726c64"
Constructor:
"String"
at Object.<anonymous>.it (password.jest.js:11:15)
at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)
at process._tickCallback (internal/process/next_tick.js:109:7)
【问题讨论】:
标签: javascript node.js testing jestjs