aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.4k stars 2.11k forks source link

Cant create mock response appsync subscription on V6 #13548

Open lingomksr opened 1 week ago

lingomksr commented 1 week ago

Before opening, please confirm:

JavaScript Framework

React

Amplify APIs

GraphQL API

Amplify Version

v6

Amplify Categories

No response

Backend

Other

Environment information

``` # Put output below this line ```

Describe the bug

I am just upgraded the aws-amplify from v5 to v6. Seems subscriptions working fine. I tried to create mock response for those subscriptions. In v5 I used spyon to create mock response. jest.spyOn(API, "graphql").mockImplementation(() => { return of(mockResponse); });

But in V6 seems API is removed. I am not able to mock those subscription.

Expected behavior

Mock should work with v6

Reproduction steps

create spyon for subscription

Code Snippet

// Put your code below this line.

Log output

``` // Put your logs below this line ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

chrisbonifacio commented 1 week ago

Hi @lingomksr the new API for graphql clients in V6 is from generateClient to initialize a client. You should be able to mock client.graphql instead of API.graphql

https://docs.amplify.aws/react/build-a-backend/data/connect-to-API/#generate-the-amplify-data-client

Let me know if that helps!

lingomksr commented 3 days ago

HI @chrisbonifacio,

I tried to use like this

const amplifyClient = generateClient();
    const apiSpy = jest.spyOn(amplifyClient, "graphql").mockImplementation(() => {
      return of(mockResponse);
    });

But its not throws type mismatch build error!

chrisbonifacio commented 3 days ago

Hi @lingomksr , I would recommend checking out how we test subscriptions in our library:

https://github.com/aws-amplify/amplify-js/tree/main/packages/api-graphql/__tests__

If that doesn't help, I'll try and see if I can get you some feedback and/or guidance on your tests.

Also, can you please share the error message you're seeing?