SMell
SMell8mo ago

ES Module mocking workaround

Hello all, I've been using Deno and Fresh for the past few weeks on a personal project and I love it. My only issue I have is testing. Basic testing on routes and pages is fine but I am having issues mocking 3rd party ES Modules such as mocking out my getSessionId function from kv_oauth . I've tried using Sinon and Deno's stubs but I haven't had any luck, would people recommend using a deno or npm tool to convert to CommonJS just to mock in tests is there a better way of doing this? Happy to provide more info or examples if needed, thanks! 🙂
15 Replies
lim
lim8mo ago
I'm also finding testing pretty hard to implement. I wonder if you got anywhere with this?
marvinh.
marvinh.8mo ago
ESM + module mocking don't really play well together in general
Leokuma
Leokuma8mo ago
GitHub
GitHub - uki00a/fresh-testing-library: Utilities for testing fresh ...
Utilities for testing fresh apps. Contribute to uki00a/fresh-testing-library development by creating an account on GitHub.
cdoremus
cdoremus8mo ago
You might want to look at this test where I used fresh-testing-library mocking capabilities: https://github.com/cdoremus/fresh/blob/fresh-testing-lib/tests/www/islands/SearchButton.test.tsx
GitHub
fresh/tests/www/islands/SearchButton.test.tsx at fresh-testing-lib ...
The next-gen web framework. Contribute to cdoremus/fresh development by creating an account on GitHub.
cdoremus
cdoremus8mo ago
I sent in a mock version of docsearch in the test and had the runtime impl use the real version of the function. You could do that for a module too. You also might want to take a look at my blog post on fresh-testing-library: https://deno-blog.com/Testing_Fresh_Components,_Middleware_and_Handlers_with_fresh-testing-library.2023-10-15
SMell
SMell8mo ago
Sorry for late response gents, been ill and have a presentation to write up. I will install the fresh-testing-library and see if its fn can successfully mock out ESModules, if so that would be great but if not then I will explore other ways. I question I haven't asked but maybe one has looked into it is using Jest in a Deno project. Probably because it doesn't follow spec its hard to run using Deno? Or am I mistaken?
cdoremus
cdoremus8mo ago
@SMell feel free to DM me if you have any questions.
SMell
SMell7mo ago
so I have tried this out and it doesn't work for me. Here is example: so the issue is that the module will need to be mocked in order for jest fn or mocked functions to work as expected. I went down the idea of using unstable_mockModule but I would need to switch entirely to using jest as the test runner then use this and try that out as trying import { jest } from 'npm:@jest/globals@29.7.0' causes an error.
No description
cdoremus
cdoremus7mo ago
Take a look at the mocking done in the example at the bottom of this section done with the fn function: https://deno-blog.com/Testing_Fresh_Components,_Middleware_and_Handlers_with_fresh-testing-library.2023-10-15#using-assertions-for-test-verification
Craig's Deno Diary
Craig's Deno Diary is a blog covering the JavaScript and TypeScript runtime Deno.
cdoremus
cdoremus7mo ago
fn has a function to mock return values I believe.
cdoremus
cdoremus7mo ago
This is the API that fresh-testing-library uses under the covers: https://jestjs.io/docs/mock-functions#mock-return-values
Mock Functions · Jest
Mock functions allow you to test the links between code by erasing the actual implementation of a function, capturing calls to the function (and the parameters passed in those calls), capturing instances of constructor functions when instantiated with new, and allowing test-time configuration of return values.
cdoremus
cdoremus7mo ago
Or you can use fn.mockImplementation jest.fn out-if-the-box will not work. Use the fn that comes with fresh-testing-library instead.
SMell
SMell7mo ago
I used fn then mocked from the ‘fresh-testing-library’ initially and neither worked for me I then installed the jest global library it uses and tried to use that instead but because it’s not using jest as the runner with jest config it fails. Happy to provide more examples if needed
cdoremus
cdoremus7mo ago
I think you should create an issue in the fresh-testing-library repo with example code he can run to confirm the problem. The dev has always been responsive to issues that I have filed and made changes to the lib based on my issues.
SMell
SMell6mo ago
Just to update people this is now solved, here is the issue tracking: https://github.com/uki00a/fresh-testing-library/issues/62
GitHub
Unable to test mocked function using fn or mocked · Issue #62 ·...
Hello, I have been trying to mock my 3rd party functions in a fresh handler and I have used createHandlerContext test my handler. I am able to get back fields such as params and status as expected ...