Let's say I have a function that returns an id (integer) that increments every time.
let nextId = 0;
const getId = () => nextId++;
export default getId;
I will have multiple tests for this function. Normally, in each test, I expect the id to be reset, meaning that the first time i call the function in each test, the returned value should be 0, but that's not the case. The value is persisted cross tests.
import getId from './getId'
describe('getId()', () => {
it('test1', () => {
expect(getId()).toEqual(0); //passes
expect(getId()).toEqual(1); //passes
})
it('test2', () => {
expect(getId()).toEqual(0); //fails
})
})
How to address this problem? Is having a global variable nextId
a bad idea?