I'm trying to add tests around the use of a simple config object. Let's say I have:
config.ts
export default {
foo: "bar",
};
app.ts
import config from "./config";
export default () => {
return config.foo;
};
app.test.ts
import App from "./app";
import config from "./config";
describe("App", () => {
beforeEach(() => {
jest.mock("./config", () => ({ default: { foo: "mock foo" } }));
});
it("returns a mocked value of config.foo", () => {
config.foo = "baz";
expect(App()).toBe("baz");
});
it("returns the default value of config.foo", () => {
expect(App()).toBe("bar");
});
});
The second test fails because the real config has been modified in the first test, but I want it to fail because the mock in the beforeEach has set the value to "mock foo". I think this has something to do with the config export not being a function, and so I can't do something like jest.spyOn...
. However, if there's a way to mock an object, it'd save me a large refactor in the project I'm working on!