.toBeInstanceOf (String) to buffer.toString () in Jest tests?
How can I use expect(str).toBeInstanceOf(String)
in a Jest assertion for a string that was created with Buffer#toString()
?
Or is the sure thing here expect(typeof str).toEqual('string')
instead?
More 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 displayed 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)
+3
source to share
No one has answered this question yet
Check out similar questions: