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:
Import Auth API by
import Auth from '@aws-amplify/auth';
mock it by jest
jest.mock('@aws-amplify/auth');
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 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 233dd1c56cb637193e4b44437c38853359d0de59 or c0fa439b28672a02662bc160058e2a0fb2bf4c13.
We'll need to investigate the difference in what's being installed so that Jest is able to mock it successfully...
@aws-amplify/[email protected]
@aws-amplify/[email protected]
@aws-amplify/[email protected]
@aws-amplify/[email protected]
For an easy way to compare the differences between what's published, I've been using:
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 (https://github.com/react-ga/react-ga/issues/316) that was resolved via https://github.com/react-ga/react-ga/pull/317.
@Amplifiyer I'd appreciate some additional 馃憖 on this 鈽濓笍
There are consistent threads regarding Jest & mocking failing due to "meta-programming":
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...
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
```// myAPITestfile.js
import API from '@aws-amplify/api/lib';
// arrange
jest.mock('@aws-amplify/api/lib');
// act
API.graphql.mockResolvedValue({});
Did you find a fix for this? Tried the above workaround on a similar problem but to no avail.
@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');
});
});
@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?
This issue has been automatically closed because of inactivity. Please open a new issue if are still encountering problems.
Most helpful comment
I was having the same issue with
@aws-amplify/apiAfter looking at @ericclemmons comparisons it seems
v1.4.3'sconditional 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
```// myAPITestfile.js
import API from '@aws-amplify/api/lib';
// arrange
jest.mock('@aws-amplify/api/lib');
// act
API.graphql.mockResolvedValue({});