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.

5
  • 4
    Possible duplicate of What are the rules for JavaScript's automatic semicolon insertion (ASI)?
    – JJJ
    Commented Dec 26, 2017 at 21:51
  • Possible duplicate of What is JavaScript? lol no Commented Dec 26, 2017 at 21:58
  • Not a duplicate in my opinion. We can just link to language features or framework documentations for any question. Commented Dec 26, 2017 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
    Commented Dec 27, 2017 at 7:15
  • Then why do people keep forgetting semi-colons? Commented Dec 27, 2017 at 7:44

1 Answer 1

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.

5
  • ah yes of course, good catch, it wasn't my code, I always use semi-colons, for these types of reasons Commented Dec 26, 2017 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
    Commented Dec 26, 2017 at 21:54
  • 1
    I agree absolutely 100%...people are crazy Commented Dec 26, 2017 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 Commented Dec 26, 2017 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 :) Commented Dec 26, 2017 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.