Skip to content
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

Handle other spies by default #143

Closed
posva opened this issue Jan 6, 2022 · 10 comments · Fixed by #144
Closed

Handle other spies by default #143

posva opened this issue Jan 6, 2022 · 10 comments · Fixed by #144

Comments

@posva
Copy link
Owner

posva commented Jan 6, 2022

  • Handle other global spies by default (apart from jest) like sinon which is used in peeky or cypress spies
  • Find a way to set the TS type. Is it possible to have this automatic? If not, allowing the user to extend an interface seems like a good middle point
@cexbrayat
Copy link
Contributor

I was going to ask a similar question: is it possible to make it work with vitest for example?
The error message complains about createSpy, but I can't find how to pass it (as we do in Pinia)?

@posva
Copy link
Owner Author

posva commented Jan 6, 2022

Not yet, but I do need to add this

@cexbrayat
Copy link
Contributor

cexbrayat commented Jan 8, 2022

I hacked a working version to unlock our migration to vitest, and ended up with something like this (to mimick what you're currently doing):

const router = createRouterMock({
  createSpy: fn => {
    const spy = vi.fn(fn);
    return [spy, () => spy.mockClear()];
  }
});

Do you imagine something like this, or maybe (haven't tried):

const router = createRouterMock({
  createSpy: vi.fn(),
  clearSpy: spy => spy.mockClear()
});

?

I can work on a PR to add this, if you give me some guidance.

Another path would be to automatically handle vitest, which would be nice, but I'm not sure this is as easily doable as for sinon or jest...

@posva
Copy link
Owner Author

posva commented Jan 11, 2022

maybe grouping spy options in an object:

spy: {
  create: ...
  restore: ...
}

To me the most interesting part is allowing a way to handle the types, probably by extending a global interface as I don't think there is a way to check for global types in TS? (typeof vi extends Record<any, any> ? vi.Spy : handle others doesn't work, does it?). IMO we should automatically handle jest, sinon (as a global), cypress, and vitest

Feel free to work on this, I will come back to this in a week or two and will be able to bring any guidance too!

@cexbrayat
Copy link
Contributor

I'll put together a first version that unblocks the use use of other test fwk, with a spy option 👍

For the type checking part, I'm not sure, we'll need to try, but that might not be easy...

It would be awesome to handle vitest out of the box, but unlike the others, it has to be imported (no globals AFAIK).
So we probably need to use a dynamic import and a try/catch to see if vitest is being used in the project? But that makes createSpy async... Or do you have a better idea?

@posva
Copy link
Owner Author

posva commented Jan 11, 2022

We definitely don't want to make spy.create async. In that case we might not be able to support vitest out of the box. But I think it's fine, since the setup code can be put in one single place (like setupTestFiles or similar in jest)

cexbrayat added a commit to cexbrayat/vue-router-mock that referenced this issue Jan 11, 2022
Refs posva#143

By default the mock will use sinon or jest support to create and restore spies.
This commit adds a `spy` option that allows to use a different testing framework,
by providing a method to create spies, and one to restore them.
For example, with vitest:

```ts
const router = createRouterMock({
  spy: {
    create: fn => vi.fn(fn),
    restore: spy => () => spy.restore()
  }
});
```
cexbrayat added a commit to cexbrayat/vue-router-mock that referenced this issue Jan 11, 2022
Refs posva#143

By default the mock will use sinon or jest support to create and restore spies.
This commit adds a `spy` option that allows to use a different testing framework,
by providing a method to create spies, and one to restore them.
For example, with vitest:

```ts
const router = createRouterMock({
  spy: {
    create: fn => vi.fn(fn),
    restore: spy => () => spy.restore()
  }
});
```
@cexbrayat
Copy link
Contributor

@posva I took a stab at it, and opened #144 : it is a first step allowing to pass a spy option. Even if not super pretty, this will unblock people that want to use other test frameworks

@posva
Copy link
Owner Author

posva commented Jan 21, 2022

Screenshot 2022-01-21 at 18 22 32

Seems to be good, even with typing!

@posva posva linked a pull request Jan 21, 2022 that will close this issue
@cexbrayat
Copy link
Contributor

Nice! 👌

@posva
Copy link
Owner Author

posva commented Jan 21, 2022

Published in 0.1.4. Let me know if it works. For adding automatic types for vitest I wrote this:

import { createRouterMock } from 'vue-router-mock'
import { JestMockCompatFn } from 'vitest'

declare module 'vue-router-mock' {
  export interface RouterMockSpy<Fn> {
    spy: JestMockCompatFn<Parameters<Fn>, ReturnType<Fn>>
  }
}

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

Successfully merging a pull request may close this issue.

2 participants