Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After updating @aws-amplify/auth to latest version(1.5.0) test cases is failing because of failed to mock Auth module. #4499

Closed
anis-shaikh opened this issue Dec 3, 2019 · 11 comments
Assignees
Labels
Auth Related to Auth components/category bug Something isn't working

Comments

@anis-shaikh
Copy link

anis-shaikh commented Dec 3, 2019

After updating @aws-amplify/auth to latest version(1.5.0) test cases is failing because of failed to mock Auth module.

Steps to reproduce the behavior:

  1. Import Auth API by
    import Auth from '@aws-amplify/auth';

  2. mock it by jest
    jest.mock('@aws-amplify/auth');

  3. See error
    Failed to get mock metadata: /Users/an***/Documents/s***/node_modules/@aws-amplify/auth/index.js

` Test suite failed to run

Failed to get mock metadata: /Users/an***/Documents/s***/node_modules/@aws-amplify/auth/index.js

See: https://jestjs.io/docs/manual-mocks.html#content

> 1 | import Auth from '@aws-amplify/auth';`
@anis-shaikh anis-shaikh added the to-be-reproduced Used in order for Amplify to reproduce said issue label Dec 3, 2019
@anis-shaikh anis-shaikh changed the title Failed to mock metadata while running test cases. After updating @aws-amplify/auth to latest version(1.5.0) test cases is failing because of failed to mock Auth module. Dec 3, 2019
@ericclemmons ericclemmons added bug Something isn't working Tests and removed to-be-reproduced Used in order for Amplify to reproduce said issue labels Dec 3, 2019
@ericclemmons
Copy link
Contributor

@anis-shaikh Thanks for bringing this to your attention, and your clear steps to reproduce 👏!

I was able to reproduce this here:

https://codesandbox.io/s/httpsgithubcomaws-amplifyamplify-jsissues4499-sr9l1

It looks like the regression was introduced in #4007 and the bump either in 233dd1c or c0fa439.

We'll need to investigate the difference in what's being installed so that Jest is able to mock it successfully...

@aws-amplify/auth@1.3.3

1 3 3

@aws-amplify/auth@1.4.2

1 4 2

@aws-amplify/auth@1.4.3

1 4 3

@aws-amplify/auth@1.5.0

1 5 0

@ericclemmons ericclemmons added the Auth Related to Auth components/category label Dec 3, 2019
@ericclemmons
Copy link
Contributor

For an easy way to compare the differences between what's published, I've been using:

  1. https://unpkg.com/browse/@aws-amplify/auth@1.4.2/
  2. https://unpkg.com/browse/@aws-amplify/auth@1.4.3/

@ericclemmons
Copy link
Contributor

For comparison, v1.4.2's entry-point is https://unpkg.com/browse/@aws-amplify/auth@1.4.2/lib/index.js, while v1.4.3's entry is https://unpkg.com/browse/@aws-amplify/auth@1.4.3/index.js.

Related, react-ga had similar issues (react-ga/react-ga#316) that was resolved via react-ga/react-ga#317.

@ericclemmons
Copy link
Contributor

@Amplifiyer I'd appreciate some additional 👀 on this ☝️

@ericclemmons
Copy link
Contributor

There are consistent threads regarding Jest & mocking failing due to "meta-programming":

jestjs/jest#471

As best I can tell, it's due to the dynamic require that happens, so Jest isn't aware of what the exports without using Proxies.

Still researching the solution to this...

@ifielder
Copy link

ifielder commented Dec 10, 2019

I was having the same issue with @aws-amplify/api

After looking at @ericclemmons comparisons it seems v1.4.3's conditional entry point is causing problems with jest's mocking mechanism.

I was able to fix this issue by using

import API, { graphqlOperation } from '@aws-amplify/api/lib';

instead of

import API, { graphqlOperation } from '@aws-amplify/api';

This is how I mocked in my test


import API from '@aws-amplify/api/lib';


// arrange 
jest.mock('@aws-amplify/api/lib');


// act
API.graphql.mockResolvedValue({});

@anywhereiromy
Copy link

Did you find a fix for this? Tried the above workaround on a similar problem but to no avail.

@anis-shaikh
Copy link
Author

anis-shaikh commented Dec 30, 2019

@anywhereiromy I tried by above solution and it works for me but for that I need to replace all the import statement to " import API from '@aws-amplify/api/lib' ".

So I found another solution and that is by manually mocking Auth module.

// __mocks__/auth.js
const Auth = jest.genMockFromModule('@aws-amplify/auth');
module.exports = Auth;

// __tests__/Test.js
import Auth from '@aws-amplify/auth';

describe('test', () => {
    it('should test signin functions', () => {
        Auth.signIn = jest.fn(() => {
            console.log('eep');
            return 'eep'
        });
        expect(Auth.signIn()).toBe('eep');
    });
});

@Amplifiyer
Copy link
Contributor

@anis-shaikh @anywhereiromy @ifielder We have just merged in the fix. Can you please try with aws-amplify@unstable version and let us know if the issue is resolved for you?

@stale
Copy link

stale bot commented Jan 15, 2020

This issue has been automatically closed because of inactivity. Please open a new issue if are still encountering problems.

@stale stale bot closed this as completed Jan 15, 2020
@github-actions
Copy link

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Auth Related to Auth components/category bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants