denoland / denoland/std

@std/expect unify internals with @std/testing/mock

Open
#6,289 0 comments 2 reactions 0 assignees View on GitHub
suggestion
Dominant language
TypeScript
Stars
3.6k
Forks
681
PR merge metrics
No merged PRs in 30d

Description

**Is your feature request related to a problem? Please describe.**

For now, there is a misalignment between `@std/expect` and `@std/testing/mock`, which causes inability to use `@std/expect`'s `fn` in `@std/testing/mock`'s `assertSpyCalls` api, and `@std/testing/mock` in `@std/expect`'s `expect().toHaveBeenCalled()` api. Which and prevents us from using both of them at the same time (totally possible for most of the other's apis of both modules).

This seems highly unreasonable, taking into account that both modules uses `@std/internal` and also `@std/expect` using `@std/assert` for some of it's functionalities like:
- same `AssertError` class
- `toBe/toBeDefined/toBeUndefined/toBeNull` uses `assertNotStrictEquals` and `assertStrictEquals`
- `toBeInstanceOf` uses `assertInstanceOf` and `assertNotInstanceOf`
- etc.

**Describe the solution you'd like**

Implement some core mock internals functionality in `@std/internal` and use it in both modules, agnostic to specifics of `@std/assert` api and `@std/expect` jest compatibility, like:
- detection if function is mock function
- gathering of internal calls state

**Describe alternatives you've considered**

Using compatible between modules storage of mocked function calls, like in `@std/expect` storing calls in `.calls` field array, or the other way around in `@std/testing/mock` storing calls in `[Symbol.for("@MOCK")]` field

**Expected result**

Expected result for this would be working code like this:

```ts
Deno.test("mock function", async ({ step }) => {
await step("@std/expect should support @std/testing/mock", () => {
const spyFn = spy(() => 5);
spyFn();
expect(spyFn).toHaveBeenCalledTimes(1);
});

await step("@std/testing/mock should support @std/expect/fn", () => {
const mockFn = fn(() => 5);
mockFn();
assertSpyCalls(mockFn, 1);
});
});
```

**PS**

I'm willing to start working on this issue my self in my free time, if that's ok

**PS**

And my other issue about [`@std/expect`'s `fn` not being 'Jest compatible'](https://github.com/denoland/std/issues/6290)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.