Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

I am following Dan Abramov's tutorial on Redux. (https://egghead.io/lessons/javascript-redux-avoiding-object-mutations-with-object-assign-and-spread)

In lesson's 9 he introduces testing and using expect and .toEqual()

This is my code:

var expect = require('chai').expect;
var freeze = require('deep-freeze-node');

const testToggleTodo = () => {
    const todoBefore = {
        id: 0,
        text: 'Learn Redux',
        completed: false
    };

    const todoAfter = {
        id: 0,
        text: 'Learn Redux',
        completed: true
    };

    expect(
        toggleTodo(todoBefore)
        ).toEqual(todoAfter)
}

testToggleTodo();
console.log('All tests passed.')

and I keep getting an error:

.../react_redux/src/a.js:24
        ).toEqual(todoAfter)
          ^

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

What am I doing wrong? I am copying his code verbatim, but it leads to errors.

like image 857
Morgan Allen Avatar asked Apr 20 '17 20:04

Morgan Allen


3 Answers

For me, my .toEqual was following the wrong brackets. Make sure it's expect(...).toEqual(..), and not expect(...(...).toEqual(...))

like image 162
Dr-Bracket Avatar answered Nov 20 '22 06:11

Dr-Bracket


in my case to make it work I done following:

1. npm install --save expect                                install package from npm
2. import expect from 'expect';                             import lib in file
3. expect(toggleTodo(stateBefore, action)).toEqual(stateAfter);
like image 3
Alexandr Avatar answered Nov 20 '22 05:11

Alexandr


Not sure if the provided syntax was ever correct for the chai library. (I would assume they are using some other assertion library)

The way the equality should be checked with its expect function is

expect(toggleTodo(todoBefore)).to.equal(todoAfter);

References:

  • http://chaijs.com/api/bdd/#method_equal
like image 1
zerkms Avatar answered Nov 20 '22 04:11

zerkms