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

mockTypes does not forward to withTag #183

Closed
1 task
antfu opened this issue Apr 27, 2023 · 3 comments
Closed
1 task

mockTypes does not forward to withTag #183

antfu opened this issue Apr 27, 2023 · 3 comments

Comments

@antfu
Copy link
Member

antfu commented Apr 27, 2023

Describe the feature

Context:

In the tailwind module, we use useLogger to create a scoped logger.

const logger = useLogger('nuxt:tailwindcss')

When we want to test it with mockTypes it doesn't work. Because useLogger uses withTag, which creates a new instance of consola and does not forward the mockTypes. And since withTag creates a new instance inside the module, there is no way for a test runner to mock it without exporting the instance.

I have two solutions in mind:

Solution 1

Expose a queryByTag global function to get the logger instance with the tag, or even make each tag singleton

Solution 2

A global mockTypes function that you can pass a tag to, and for each consola instance it will search for the registered mock function to run. This way we could test only the scoped logger.

Additional information

  • Would you be willing to help implement this feature?
@pi0 pi0 closed this as completed in 6864928 Apr 28, 2023
@pi0
Copy link
Member

pi0 commented Apr 28, 2023

6864928 should solve it. (notice that logFn - the factory -) is inherited from parent instance.

Regarding global instance usage, consola < v3 was always using a global and shared instance to reuse but it could make backward compatibility issues if we started to do this. maybe later and specially for this kind of mocks to avoid re-running mock-fn, we add it back.

@antfu
Copy link
Member Author

antfu commented Apr 28, 2023

Been able to inherit is good, but do you think it would be better to also able to have the tag passed to the mock function so we can test based on different scope/tag?

@pi0
Copy link
Member

pi0 commented May 3, 2023

Yes that would be also nice! (feel free to make a PR for fix)

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

2 participants