How to test delete function in open sass starter template
Hello I am new to wasp and im trying to figure out how to properly unit test the front end. I have an initial test that is passing, that tests the getAllTasksByUser query returns the tasks and renders to the front end.
My failing test is supposed to follow a similar format as the first. Mock the function to return the expected data but im having trouble with a typescript error when passing in deleteTask to mockQuery -> "Argument of type 'Promise<GetResult<{ id: string; description: string; time: string; isDone: boolean; userId: number; createdAt: Date; }, unknown> & {}>' is not assignable to parameter of type 'Query<unknown, unknown>'.
Type 'Promise<GetResult<{ id: string; description: string; time: string; isDone: boolean; userId: number; createdAt: Date; }, unknown> & {}>' provides no match for the signature '(queryCacheKey: string[], args: unknown): Promise<unknown>'.ts(2345)"
7 Replies
my guess is that its because deleteTask is an action and not a query ? its incompatible ?
been doing research and think i need to use mockApi
Check the docs here 🙂 https://wasp-lang.dev/docs/project/testing#react-component-tests
Testing | Wasp
Wasp is in beta, so keep in mind there might be some kinks / bugs, and possibly some changes with testing support in the future. If you encounter any issues, reach out to us on Discord and we will make sure to help you out!
I believe what you should write is:
mockQuery(deleteTask, {})
When i add that line to my test a get a ts error:
Argument of type '(args: Pick<GetResult<{ id: string; description: string; time: string; isDone: boolean; userId: number; createdAt: Date; }, unknown> & {}, "id">) => Promise<GetResult<{ id: string; description: string; time: string; isDone: boolean; userId: number; createdAt: Date; }, unknown> & {}>' is not assignable to parameter of type 'Query<unknown, GetResult<{ id: string; description: string; time: string; isDone: boolean; userId: number; createdAt: Date; }, unknown> & {}>'.
Types of parameters 'args' and 'queryCacheKey' are incompatible.
Property 'id' is missing in type 'string[]' but required in type 'Pick<GetResult<{ id: string; description: string; time: string; isDone: boolean; userId: number; createdAt: Date; }, unknown> & {}, "id">'.ts(2345)
Wohooo @Supreme, you just became a Waspeteer level 1!
really having trouble deciphering how its supposed to be formatted to get rid of the ts error
i want to write a unit test for the deleteTask method. I want to mock the method and when the user clicks the button I want to verify that it was called and called with the correct paramater. Would you advise using Vitest for this type of test or can i rely on wasp methods to cover this ? Any guidance really helps, thanks.
Solved my failing test. I ended up using vitest to mock the wasp operation methods. I think I was not using the wasp helper methods in the correct context for what i was trying to do with my test.
Nice. good to hear. Let us know if you have any other questions 🙂