Search code examples
javascriptnode.jssocket.ionode.js-connect

TypeError: expect(...).toBeA is not a function


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


Solution

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