TypeError: expect(...).toBeA is not a function
Asked Answered
K

2

6

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...

Kilo answered 4/4, 2018 at 10:9 Comment(4)
It should be .toBe instead of .toBeA?Claybourne
Error: expect(received).toBe(expected) // Object.is equality Expected value to be: "number" Received: 1522837788748 Difference: Comparing two different types of values. Expected string but received number. at Context.it (server/utils/message.test.js:12:31)Kilo
Follow your error message. I think message.createdAt return number instead of string.Claybourne
Yes bcoz of the use of .toBe instead of .toBeAKilo
W
23

The 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});
   });

});
Wayside answered 11/4, 2018 at 10:36 Comment(0)
G
0

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! :-)

Gouda answered 21/3, 2021 at 11:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.