this is my message.test.js file.
var expect = require('expect');
var {generateMessage} = require('./message');
describe('generateMessage', () => {
it('should generate correct message object', () => {
var from = 'Jen';
var text = 'Some message';
var message = generateMessage(from, text);
expect(message.createdAt).toBeA('number');
expect(message).toInclude({from, text});
});
});
ERROR: priya@priya-pro:~/node-chat-app$ npm test
[email protected] test /home/priya/node-chat-app mocha server/**/*.test.js generateMessage 1) should generate correct message object
0 passing (12ms) 1 failing
1) generateMessage should generate correct message object: TypeError: expect(...).toBeA is not a function at Context.it (server/utils/message.test.js:12:31)
npm ERR! Test failed. See above for more details.
Help me...
this is my message.test.js file.
var expect = require('expect');
var {generateMessage} = require('./message');
describe('generateMessage', () => {
it('should generate correct message object', () => {
var from = 'Jen';
var text = 'Some message';
var message = generateMessage(from, text);
expect(message.createdAt).toBeA('number');
expect(message).toInclude({from, text});
});
});
ERROR: priya@priya-pro:~/node-chat-app$ npm test
[email protected] test /home/priya/node-chat-app mocha server/**/*.test.js generateMessage 1) should generate correct message object
0 passing (12ms) 1 failing
1) generateMessage should generate correct message object: TypeError: expect(...).toBeA is not a function at Context.it (server/utils/message.test.js:12:31)
npm ERR! Test failed. See above for more details.
Help me...
Share Improve this question asked Apr 4, 2018 at 10:09 PriyaPriya 1022 silver badges14 bronze badges 4 |2 Answers
Reset to default 23The ownership of the expect library has been donated to jest from v21+. Since then some of the method names and their functionality has been changed. The following modification to code will help to overcome issue.
var expect = require('expect');
var {generateMessage} = require('./message');
describe('generateMessage', () => {
it('should generate correct message object', () => {
var from = 'Jen';
var text = 'Some message';
var message = generateMessage(from, text);
expect(typeof message.createdAt).toBe('number');
expect(message).toMatchObject({from, text});
});
});
In a similar way, I made a basic syntax error, my terminal was saying:
TypeError: sum(...).toBe is not a function.
Here's the code I was using in both my main.js file and my main.test.js file:
↓ main.js ↓
function sum(a, b){
return a + b
}
module.exports = sum;
↓ main.test.js ↓
const sum = require('./main')
test('adds two numbers together', ()=>{
expect(sum(2,2).toBe(4))
})
I kept thinking that my module.exports/require syntax was somehow wrong. After many attempts, I meticulously followed a Jest.js tutorial - and the unit test passed! I then compared it with my nonfunctional copy - and I spotted the error in my main.test.js file :
main.test.js
expect(sum(2,2).toBe(4))
what it needs to be is:
expect(sum(2,2)).toBe(4)
Notice the position of the parentheses - rather than chaining the .toBe() matcher to the expect() function, I was unwittingly passing .toBe() into the expect() function with my sum(2,2) value, resulting in : TypeError: sum(...).toBe is not a function !
A super obvious mistake in hindsight, but hopefully this helps someone in the future! :-)
.toBe
instead of.toBeA
? – Tan Duong Commented Apr 4, 2018 at 10:17message.createdAt
return number instead of string. – Tan Duong Commented Apr 4, 2018 at 10:35