Back

How to mock appwrite sdk calls during testing function?

  • 1
  • Functions
whataboutno13
4 Apr, 2023, 09:47

I am struggling to get the correct way to mock appwrite sdk calls for testing my nodejs function. I use jest as test framework and want for example mock requests on the users sdk. I was thinking of something like this, but I am not sure if it is the right way to do...

TypeScript
jest.mock('../node_modules/node-appwrite/lib/services/users.js');
const client = new sdk.Client();
const users = new sdk.Users(client);

test("test_example", () => {
//Some testing code
users.list.mockResolvedValue(SOME_VALUE);
}

Is this the way to mock appwrite sdk calls?

TL;DR
Support Thread: - The user is asking for help on how to mock appwrite sdk calls during testing functions. - One suggestion is to create a separate test project, run the Appwrite function on it, check the expected result, and then destroy the project. - The user asks if anyone has found a solution for mocking appwrite sdk calls. - Someone offers to check if there are any existing examples of mocking sdk calls. - The user shares their code for mocking sdk calls using jest.mock() and asks if it is the right way to do it. Solution: - There is no clear solution provided in the support thread.
joeyouss
4 Apr, 2023, 10:38

Hi πŸ‘‹ I am not sure if we have some examples already about mock sdk calls, let me have a look

whataboutno13
4 Apr, 2023, 11:27

That would be great thank you !

whataboutno13
4 Apr, 2023, 12:07

Any luck yet finding something? 🀩

joeyouss
4 Apr, 2023, 14:37

Hmm not yet but - why do you need a mock? What might be better for you is to have a test that creates a plain new project, prepares it, and then run Appwrite Function on it. Check if function gave expected result, and destroy the project. All of that they can do on some kind of test server that you wound wipe every now and then to clear any left-overs.

whataboutno13
4 Apr, 2023, 16:20

Well thats true, using a project just for testing seems fair enough

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more