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.
What is JavaScript?
lol no