Skip to content

std/expect: add spyOn function #6449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kt3k opened this issue Feb 27, 2025 · 5 comments
Open

std/expect: add spyOn function #6449

kt3k opened this issue Feb 27, 2025 · 5 comments

Comments

@kt3k
Copy link
Member

kt3k commented Feb 27, 2025

https://jestjs.io/docs/jest-object#jestspyonobject-methodname

@gmzacharydovel
Copy link

I'll see if I can make a PR on this. Probably next week.

@csvn
Copy link

csvn commented May 15, 2025

There is already mocks/spying in the @std/testing/mock package? What I wish though is that it would be possible to use these with expect correctly:

import { expect } from "@std/expect";
import { stub } from "@std/testing/mock";

Deno.test(() => {
  using logSpy = stub(console, "log");
  console.log("hello");
  expect(logSpy).toHaveBeenCalledTimes(1);
});

This does not work currently, since getMockCalls() will throw. It's possible to work-around with fn, but it's messy for several reasons:

import { expect } from "@std/expect";
import { stub } from "@std/testing/mock";

Deno.test(() => {
  using logSpy = stub(console, "log", fn(() => {}) as any);
  console.log("hello");
  expect(logSpy.fake).toHaveBeenCalledTimes(1);
});
  • The return signature Function is not compatible with stub types, it should likely use a generic
  • stub can't be wrapped, because then the disposal won't work, so we need to pass a bogus method to fn
  • We must get the function returned by fn, so .fake has to be used on the stub spy

It would be great if @std/testing/mock worked out of the box, or if there at least was simpler way to integrate them. I think it may be a mistake to duplicate similar code in two different @std packages.

@gmzacharydovel
Copy link

Hey @csvn I think that is a given. Unfortunately, whenever I think I have some free time I get drowned in some client work. I would implement this such that it fits into the current architecture.

@csvn
Copy link

csvn commented May 20, 2025

My comment was mostly because the issue title was not clear whether this was about adding something new or re-exporting/wrapping @std/testing/mock. And I was not commenting at all about who was to work on this. Just writing how I hope it could be simplified 😃

@kt3k
Copy link
Member Author

kt3k commented May 21, 2025

@std/expect generally tries to be compatible with jest APIs (and we currently don't have plan to make @std/expect and @std/testing/mock compatible).

So the idea of this issue is to implement something like:

import { expect } from "@std/expect";
import { spyOn } from "@std/expect/unstable-spy-on";

const spy = spyOn(console, "log");

console.log("hello");

expect(spy).toHaveBeenCalledWith("hello");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants