Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mocha hooks, only methods not working

Tags:

mocha.js

The mocha hooks like before(), after(), beforeEach(), afterEach() are not working. Also the only method too not working. None of the beforeEach is called. I get an error has no method 'only'. Below is the code.

describe('Array', function(){
    beforeEach(function(){
      console.log('before every test')
    })
  describe.only('#indexOf()', function(){
    beforeEach(function(){
      console.log('before every test')
    })
    it.only('should return -1 unless present', function(){
        assert.equal(1,2)
    })
    it('should return the index when present', function(){
        assert.equal(1,2);
    })
  })
})

beforeEach(function(){
  console.log('before every test')
})
like image 679
Rajkamal Subramanian Avatar asked Dec 06 '25 03:12

Rajkamal Subramanian


1 Answers

Your code works for me. You need to add:

var assert = require('assert');

at top. And, be sure you npm install -g mocha.

Then, I get:

$ mocha -R spec temp.js


  Array
    #indexOf()
      ◦ should return -1 unless present: before every test
before every test
before every test
      1) should return -1 unless present
      ◦ should return the index when present: before every test
before every test
before every test
      2) should return the index when present


  0 passing (16ms)
  2 failing

  1) Array #indexOf() should return -1 unless present:
     AssertionError: 1 == 2
      at Context.<anonymous> (/Users/dan/Dropbox/Documents/dev/spock/temp.js:12:16)
      at Test.Runnable.run (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:211:32)
      at Runner.runTest (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:355:10)
      at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:401:12
      at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:281:14)
      at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:290:7
      at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:234:23)
      at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:253:7
      at Hook.Runnable.run (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:213:5)
      at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:246:10)
      at Object._onImmediate (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:258:5)
      at processImmediate [as _immediateCallback] (timers.js:330:15)

  2) Array #indexOf() should return the index when present:
     AssertionError: 1 == 2
      at Context.<anonymous> (/Users/dan/Dropbox/Documents/dev/spock/temp.js:15:16)
      at Test.Runnable.run (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:211:32)
      at Runner.runTest (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:355:10)
      at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:401:12
      at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:281:14)
      at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:290:7
      at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:234:23)
      at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:253:7
      at Hook.Runnable.run (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:213:5)
      at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:246:10)
      at Object._onImmediate (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:258:5)
      at processImmediate [as _immediateCallback] (timers.js:330:15)

Note that your it.only is being ignored because the describe.only is encountered first. If you aren't able to replicate this output, please report what version of node and npm you're using.

like image 169
Dan Kohn Avatar answered Dec 08 '25 03:12

Dan Kohn