3

This issue on the Mocha Github issue tracker is interesting

https://github.com/mochajs/mocha/issues/3180

this code works as expected:

describe('before/after with data-driven tests', () => {
  before(() => console.log('before worked'));
  beforeEach(() => console.log('beforeEach worked'));
  afterEach(() => console.log('afterEach worked'));
  after(() => console.log('after worked'));

  ['foo'].forEach((item) => {
    it(`works for item ${item}`, () => {
      console.log('item is', item)
    })
  })
})

but this code acts strangely:

describe('before/after with data-driven tests', () => {
  before(() => console.log('before worked'))
  beforeEach(() => console.log('beforeEach worked'))
  afterEach(() => console.log('afterEach worked'))
  after(() => console.log('after worked'))
  [ 'foo' ].forEach((item) => {
    it(`works for item ${item}`, () => {
      console.log('item is', item)
    })
  })
})

if you execute the second example code with mocha, it tries to read 'foo' from an undefined variable. Does anyone know why? Here is the error trace:

    [ 'foo' ].forEach((item) => {
    ^

TypeError: Cannot read property 'foo' of undefined

pretty weird! But I am sure there is a good explanation.

Alexander Mills
  • 1
  • 80
  • 344
  • 642
  • 4
    Possible duplicate of [What are the rules for JavaScript's automatic semicolon insertion (ASI)?](https://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi) – JJJ Dec 26 '17 at 21:51
  • Possible duplicate of `What is JavaScript?` lol no – Alexander Mills Dec 26 '17 at 21:58
  • Not a duplicate in my opinion. We can just link to language features or framework documentations for any question. – Hristo Vrigazov Dec 26 '17 at 22:05
  • That is the canonical duplicate for literally dozens of identical questions like this. We really don't need a separate question every time someone forgets to add a semicolon. – JJJ Dec 27 '17 at 07:15
  • Then why do people keep forgetting semi-colons? – Alexander Mills Dec 27 '17 at 07:44

1 Answers1

7

The reason is the missing ; in the second example:

after(() => console.log('after worked'))
  [ 'foo' ].forEach((item) => {
    it(`works for item ${item}`, () => {
      console.log('item is', item)
    })
  })

When there is no semicolon, this means "Invoke the function after" and select this key from the result it returns. But after returns undefined, so you get the error.

Pointy
  • 371,531
  • 55
  • 528
  • 584
Hristo Vrigazov
  • 1,237
  • 2
  • 10
  • 18
  • ah yes of course, good catch, it wasn't my code, I always use semi-colons, for these types of reasons – Alexander Mills Dec 26 '17 at 21:54
  • 1
    Such a classic example of the dangers of not putting in semi-colons. I've never understood the folks that think leaving out semicolons is a good idea. Spend just one three hour debugging session trying to debug some mysterious bug because of this and one will never leave out semi-colons again. – jfriend00 Dec 26 '17 at 21:54
  • 1
    I agree absolutely 100%...people are crazy – Alexander Mills Dec 26 '17 at 21:55
  • agreed, not having semicolons leads to all sorts of weird things going on :) if this answers your question, consider accepting it as an answer – Hristo Vrigazov Dec 26 '17 at 21:55
  • It won't allow me to accept the answer until 5 minutes from now..but in the meantime, I believe there is no time restriction on upvoting a question :) – Alexander Mills Dec 26 '17 at 21:56