Skip to content

Latest commit

 

History

History
39 lines (28 loc) · 1019 Bytes

CODE-GUIDELINES.md

File metadata and controls

39 lines (28 loc) · 1019 Bytes

Guidelines for Podman Desktop Code

Production code

Unit tests code

Use vi.mocked, not a generic myFunctionMock

If you define a mock with const myFunctionMock = vi.fn(); its type is Mock<Procedure>, which is a generic type.

For example, do not write this, or Typescript won't be able to detect that you passed an object instead of a string to mockResolvedValue:

const windowMethodMock = vi.fn();

Object.defineProperty(global, 'window', {
  value: {
    windowMethod: windowMethodMock,
  },
});

test('...', () => {
  windowMethodMock.mockResolvedValue({ msg: 'a string' }); // here, Typescript is not able to detect that the type is wrong
});

Instead, you can write vi.mocked(window.windowMethod).mock..., and Typescript will check that you correctly pass a string to mockResolvedValue:

Object.defineProperty(global, 'window', {
  value: {
    windowMethod: vi.fn(),
  },
});

test('...', () => {
  vi.mocked(window.windowMethod).mockResolvedValue('a string');
});