[Vuejs]-Resetting global state of composable between tests

0👍

I think I’ve did it using jest.isolateModules with require instead of import:

beforeEach(() => {
  jest.isolateModules(() => {
    myModule = require("./useFoo.");
  });
});

it("working test", () => {
  const { state } = myModule.useRoutes();
  expect(state.value).toEqual([]);
}

it("previously failed test", () => {
  const { state } = myModule.useRoutes();
  expect(state.value).toEqual([]); // this is now working 
}

This ensures that the module is reimported before each test, which initializes the singleton/state again.

Leave a comment