Mocha: TypeError: Cannot read property 'equal' of undefined

Created on 8 Aug 2020  路  6Comments  路  Source: mochajs/mocha

Prerequisites

  • [x] Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • [x] Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • [x] 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • [x] Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend that you _not_ install Mocha globally.

Description

describe("Array", () => {
  describe("#indexOf()", () => {
    it("shold return -1 when the value is not present", () => {
      [1, 2, 3].indexOf(5).to.equal(-1);
      [1, 2, 3].indexOf(2).to.equal(-1);
    });
  });
});

Steps to Reproduce

I followed the mocha getting started guide

  • npm init
  • update scripts to "test": "mocha" in package.json file
  • yarn add mocha
  • make test.js
  • add the above code
  • yarn test

Expected behavior: [What you expect to happen]
I think it should run without an error
Actual behavior: [What actually happens]
it displays the following:

1) Array
       #indexOf()
         shold return -1 when the value is not present:
     TypeError: Cannot read property 'equal' of undefined
      at Context.<anonymous> (test.js:8:16)
      at processImmediate (internal/timers.js:456:21)

Reproduces how often: [What percentage of the time does it reproduce?]

Versions

  • The output of mocha --version and node node_modules/.bin/mocha --version: 8.1.1
  • The output of node --version: v12.18.1
  • Your operating system

    • name and version: macOS High Sierra 10.13.6

    • architecture (32 or 64-bit): 64

  • Your shell (e.g., bash, zsh, PowerShell, cmd): bash
  • Your browser and version (if running browser tests): It's not a browser test
  • Any third-party Mocha-related modules (and their versions): nothing
  • Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version): nothing

Additional Information

This code is on the mochajs.org page 'Synchronous Code'

image

unconfirmed-bug

All 6 comments

Assuming you're using Chai, it appears you forgot to wrap your actual value in expect().

Thanks for your answer. But I don't use Chai. I just followed the instructions on getting started page of mocha.
I just installed mocha.

That would explain it. Mocha is only a test runner rather than an assertion framework, so you'll have to add those separately if you want to use them.

Cool. Thak makes sense. Then the getting started page of mocha should comment about that in this section, I think.

Mocha_-_the_fun__simple__flexible_JavaScript_test_framework

Even in my experience, it was difficult for me to first understand exactly what mocha does too.
I think it would be nice if it was added more clearly to the documentation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

enigmatic00 picture enigmatic00  路  3Comments

jamietre picture jamietre  路  3Comments

wzup picture wzup  路  3Comments

luoxi001713 picture luoxi001713  路  3Comments

eschwartz picture eschwartz  路  3Comments