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

4
  • It should be .toBe instead of .toBeA?
    – Tan Duong
    Commented Apr 4, 2018 at 10:17
  • 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)
    – Priya
    Commented Apr 4, 2018 at 10:30
  • Follow your error message. I think message.createdAt return number instead of string.
    – Tan Duong
    Commented Apr 4, 2018 at 10:35
  • Yes bcoz of the use of .toBe instead of .toBeA
    – Priya
    Commented Apr 4, 2018 at 10:40

2 Answers 2

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.