最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: expect(...).toEqual is not a function - Stack Overflow

programmeradmin6浏览0评论

I am following Dan Abramov's tutorial on Redux. ()

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.

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.

Share Improve this question asked Apr 20, 2017 at 20:15 dropWizarddropWizard 3,53812 gold badges69 silver badges92 bronze badges 1
  • The source of truth is always in the official documentation not in someone's blog: chaijs.com/guide/styles/#expect – zerkms Commented Apr 20, 2017 at 20:30
Add a comment  | 

4 Answers 4

Reset to default 11

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

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

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

was using the wrong package

shouldn't use chai,

import expect from 'expect'

发布评论

评论列表(0)

  1. 暂无评论