mirror of
https://github.com/podman-desktop/podman-desktop
synced 2026-04-21 17:47:22 +00:00
docs: in code guidelines, use vi.mocked (#10353)
* docs: use vi.mocked Signed-off-by: Philippe Martin <phmartin@redhat.com> * fix: remove unnecessary code Signed-off-by: Philippe Martin <phmartin@redhat.com> --------- Signed-off-by: Philippe Martin <phmartin@redhat.com>
This commit is contained in:
parent
5aef2f0b2d
commit
87cd8cada6
1 changed files with 39 additions and 0 deletions
39
CODE-GUIDELINES.md
Normal file
39
CODE-GUIDELINES.md
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
# 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`:
|
||||
|
||||
```ts
|
||||
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`:
|
||||
|
||||
```ts
|
||||
Object.defineProperty(global, 'window', {
|
||||
value: {
|
||||
windowMethod: vi.fn(),
|
||||
},
|
||||
});
|
||||
|
||||
test('...', () => {
|
||||
vi.mocked(window.windowMethod).mockResolvedValue('a string');
|
||||
});
|
||||
```
|
||||
Loading…
Reference in a new issue