admin管理员组文章数量:1310542
How can I use expect(str).toBeInstanceOf(String)
in a Jest assertion for a string that has been created using Buffer#toString()
?
Or is the correct thing to do here expect(typeof str).toEqual('string')
instead?
Details:
This test case, using typeof
, passes:
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);
});
However, this test case, using .toBeInstanceOf()
, fails:
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);
});
Here's the Jest output for it:
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)
How can I use expect(str).toBeInstanceOf(String)
in a Jest assertion for a string that has been created using Buffer#toString()
?
Or is the correct thing to do here expect(typeof str).toEqual('string')
instead?
Details:
This test case, using typeof
, passes:
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);
});
However, this test case, using .toBeInstanceOf()
, fails:
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);
});
Here's the Jest output for it:
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)
Share
Improve this question
edited Jun 2, 2017 at 5:29
bguiz
asked Jun 2, 2017 at 5:06
bguizbguiz
28.5k49 gold badges163 silver badges251 bronze badges
1 Answer
Reset to default 8If you have a look at the toBeInstanceOf implementation, you'll see that instanceof
is used for checking, but as you can see it demonstrated in Mozilla docs, string primitive
is not the same thing as String
derived from Object
.
Your first variant is the correct way to check:
expect(typeof str).toEqual('string');
本文标签: javascripttoBeInstanceOf(String) on buffertoString() in Jest testsStack Overflow
版权声明:本文标题:javascript - .toBeInstanceOf(String) on buffer.toString() in Jest tests? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741807410a2398588.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论