I have the following in state.js
:
var state = {};
function getState() {
return state;
}
function setItem(key, value) {
state[key] = value;
}
function resetState() {
state = {};
}
exports.getState = getState;
exports.setItem = setItem;
exports.resetState = resetState;
In my Mocha test file, I have the following:
const { getState, setItem, resetState } = require("./state");
var assert = require("assert");
describe("...", function () {
beforeEach(function() {
resetState();
})
it("Case 1", function (done) {
setItem("a", "b");
assert.equal(JSON.stringify(getState()), JSON.stringify({"a": "b"}));
done();
});
it("Case 2", function (done) {
setItem("x", "y");
assert.equal(JSON.stringify(getState()), JSON.stringify({"x": "y"}));
done();
});
});
Unfortunately, Case 2 fails because it looks like the resetState()
function does not work here even though I’m calling it in beforeEach
. What am I doing wrong?