D
Deno

help

ES Module mocking workaround

SSMell10/27/2023
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! 🙂
Llim11/3/2023
I'm also finding testing pretty hard to implement. I wonder if you got anywhere with this?
Mmarvinh.11/3/2023
ESM + module mocking don't really play well together in general
LLeokuma11/3/2023
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.
Ccdoremus11/4/2023
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.
Ccdoremus11/4/2023
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
SSMell11/8/2023
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?
Ccdoremus11/8/2023
@SMell feel free to DM me if you have any questions.
SSMell11/18/2023
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
Ccdoremus11/18/2023
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.
Ccdoremus11/18/2023
fn has a function to mock return values I believe.
Ccdoremus11/18/2023
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.
Ccdoremus11/19/2023
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.
SSMell11/19/2023
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
Ccdoremus11/20/2023
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.
SSMell12/31/2023
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 ...

Looking for more? Join the community!

Recommended Posts
FontAwesome Import into island component not working as expectedHey, I want to use FontAwesome with a Fresh component inside my island folder. I followed the follKV - Not possible to maintain in real world?I have a question regarding Deno **KV**: I just checked the docs and it seems straight forward so faGet a Parameter Value in Oakso if I set up a router like this ```router.get("/:id", (ctx) => {/*handle router context*/})```thenreadTextFile() not working?what is this? its there but its not workingkv store key granualityHow cheap are the `kv.get` operations? Say I have a userprofile with 3 fields, should I create 3 kedeno tap metadataHello. I am running deno tests with `deno test --reporter=tap`. I would like to add additional fieldUsing ESBuild with DenoHey, is anyone more familiar with ESBuild? I am currently trying to transpile my Deno TypeScript proMultiple subdomainsHow can I listen for incoming http request on multiple subdomains?Can't upgrade request to websocketI want to use a websocket in one of my middlewares. But I keep getting an error saying I can't upgrAccess `navigator.geolocation` in deno runtimeHello, is it possible to access `navigator.geolocation` in the deno runtime?Tracking Down Lost MemoryHi, I have a long running service that creates many `JsRuntime`s over time and I have found that I `deno run` ignores @deno-types?```ts Uncaught SyntaxError: The requested module 'pg' does not provide an export named 'Pool' importWhere is my Twitter access token with Deno KV Oauth?I signed in successfully and now the Deno KV database is just storing `true`. It's not enough to intHow do I import the stripe SDK into my fresh.js projectHi there! How do I import the stripe SDK into my fresh.js project? Im struggling with my deno.json fUnable to find NPM:Rollup 4.1.4 on M1 MacHi all, hooking up a new build process for UI project, trying to use Rollup on an M1 Mac. My build How to get custom folder structure with Fresh?Hello, I want to integrate Fresh into my existing project. I want to use the 4 directories: componenFresh error: Could not fetch latest versionTotal newbie here. I'm getting "Update check failed: Could not fetch latest version." when I make a VSCode Test Debugging```json { "name": "Test", "request": "launch", "type": "node", "cwd": "$Can you recommend a mysql driver for deno that isn't a complete dead end?mysql2 port: never updated. mysql driver: doesn't support strings mysql connector from Oracle: no onHelp with Slack CLI DeploymentSorry if this has already been answered, but I am trying to deploy my Slack app and I keep getting t