Skip to content

Commit

Permalink
release: Amplify JS release (#13212)
Browse files Browse the repository at this point in the history
  • Loading branch information
Samaritan1011001 committed Apr 2, 2024
2 parents 83cacf2 + 68a9f28 commit e7317d6
Show file tree
Hide file tree
Showing 78 changed files with 1,723 additions and 632 deletions.
26 changes: 24 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,25 @@ module.exports = {
'setupTests.ts',
'jest.setup.*',
'jest.config.*',
// temporarily disable lint on __tests__
'__tests__',
// 'packages/adapter-nextjs/__tests__',
'packages/analytics/__tests__',
'packages/api/__tests__',
'packages/api-graphql/__tests__',
'packages/api-rest/__tests__',
'packages/auth/__tests__',
// 'packages/aws-amplify/__tests__',
// 'packages/core/__tests__',
'packages/datastore/__tests__',
'packages/datastore-storage-adapter/__tests__',
'packages/geo/__tests__',
'packages/interactions/__tests__',
'packages/notifications/__tests__',
'packages/predictions/__tests__',
'packages/pubsub/__tests__',
'packages/react-native/__tests__',
'packages/rtn-push-notification/__tests__',
'packages/rtn-web-browser/__tests__',
'packages/storage/__tests__',
// will enable lint by packages
// 'adapter-nextjs',
// 'packages/analytics',
Expand All @@ -63,6 +80,10 @@ module.exports = {
'error',
{
allow: [
// exceptions for core package
'phone_number',
'search_indices',
// exceptions for api packages
'graphql_headers',
// exceptions for the legacy config
/^(aws_|amazon_)/,
Expand Down Expand Up @@ -105,6 +126,7 @@ module.exports = {
'no-useless-constructor': 'off',
'no-trailing-spaces': 'error',
'no-return-await': 'error',
'n/no-callback-literal': 'off',
'object-shorthand': 'error',
'prefer-destructuring': 'off',
'promise/catch-or-return': [
Expand Down
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
"type": "node",
"request": "launch",
// The debugger will only run tests for the package specified here:
"cwd": "${workspaceFolder}/packages/datastore",
"cwd": "${workspaceFolder}/packages/api-graphql",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
// Optionally specify a single test file to run/debug:
"connectivityHandling.test.ts",
"generateClientWithAmplifyInstance.test.ts",
"--runInBand",
"--testTimeout",
"600000", // 10 min timeout so jest doesn't error while we're stepping through code
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ResourcesConfig } from '@aws-amplify/core';

import {
generateServerClientUsingCookies,
generateServerClientUsingReqRes,
} from '../../src/api';
import {
getAmplifyConfig,
createRunWithAmplifyServerContext,
getAmplifyConfig,
} from '../../src/utils';
import { NextApiRequestMock, NextApiResponseMock } from '../mocks/headers';
import { createServerRunnerForAPI } from '../../src/api/createServerRunnerForAPI';
Expand Down Expand Up @@ -59,7 +60,7 @@ describe('generateServerClientUsingCookies', () => {
});

it('should call createRunWithAmplifyServerContext to create runWithAmplifyServerContext function', async () => {
const cookies = (await headers).cookies;
const { cookies } = await headers;

generateServerClientUsingCookies({ config: mockAmplifyConfig, cookies });
expect(mockCreateRunWithAmplifyServerContext).toHaveBeenCalledWith({
Expand Down Expand Up @@ -94,7 +95,7 @@ describe('generateServerClient', () => {
}));

jest.mock('@aws-amplify/core/internals/adapter-core', () => ({
getAmplifyServerContext: () => {},
getAmplifyServerContext: jest.fn(),
}));

const client = generateServerClientUsingReqRes({
Expand Down
9 changes: 5 additions & 4 deletions packages/adapter-nextjs/__tests__/createServerRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { ResourcesConfig, sharedInMemoryStorage } from '@aws-amplify/core';

import { NextServer } from '../src/types';

const mockAmplifyConfig: ResourcesConfig = {
Expand Down Expand Up @@ -50,7 +51,7 @@ describe('createServerRunner', () => {
parseAWSExports: mockParseAWSExports,
}));

createServerRunner = require('../src').createServerRunner;
({ createServerRunner } = require('../src'));
});

afterEach(() => {
Expand All @@ -76,7 +77,7 @@ describe('createServerRunner', () => {
describe('runWithAmplifyServerContext', () => {
describe('when amplifyConfig.Auth is not defined', () => {
it('should call runWithAmplifyServerContextCore without Auth library options', () => {
const mockAmplifyConfig: ResourcesConfig = {
const mockAmplifyConfigWithoutAuth: ResourcesConfig = {
Analytics: {
Pinpoint: {
appId: 'app-id',
Expand All @@ -85,12 +86,12 @@ describe('createServerRunner', () => {
},
};
const { runWithAmplifyServerContext } = createServerRunner({
config: mockAmplifyConfig,
config: mockAmplifyConfigWithoutAuth,
});
const operation = jest.fn();
runWithAmplifyServerContext({ operation, nextServerContext: null });
expect(mockRunWithAmplifyServerContextCore).toHaveBeenCalledWith(
mockAmplifyConfig,
mockAmplifyConfigWithoutAuth,
{},
operation,
);
Expand Down
5 changes: 3 additions & 2 deletions packages/adapter-nextjs/__tests__/mocks/headers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Socket } from 'net';
import { IncomingMessage } from 'http';

import { NextApiRequest, NextApiResponse } from 'next/index.js';
import {
NextApiRequestCookies,
NextApiRequestQuery,
} from 'next/dist/server/api-utils/index.js';
import { Socket } from 'net';
import { IncomingMessage } from 'http';

export type NextApiRequestOptions = Partial<NextApiRequestMock>;
export class NextApiRequestMock
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { IncomingMessage, ServerResponse } from 'http';
import { Socket } from 'net';

import { enableFetchMocks } from 'jest-fetch-mock';
import { NextRequest, NextResponse } from 'next/server.js';
import { cookies } from 'next/headers.js';

import {
DATE_IN_THE_PAST,
createCookieStorageAdapterFromNextServerContext,
} from '../../src/utils/createCookieStorageAdapterFromNextServerContext';

// Make global Request available during test
enableFetchMocks();

import { NextRequest, NextResponse } from 'next/server.js';
import { cookies } from 'next/headers.js';
import { createCookieStorageAdapterFromNextServerContext } from '../../src/utils/createCookieStorageAdapterFromNextServerContext';
import { DATE_IN_THE_PAST } from '../../src/utils/createCookieStorageAdapterFromNextServerContext';
import { IncomingMessage, ServerResponse } from 'http';
import { Socket } from 'net';

jest.mock('next/headers', () => ({
cookies: jest.fn(),
}));
Expand Down
117 changes: 85 additions & 32 deletions packages/api-graphql/__tests__/GraphQLAPI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {
import { GetThreadQuery } from './fixtures/with-types/API';
import { AWSAppSyncRealTimeProvider } from '../src/Providers/AWSAppSyncRealTimeProvider';
import { Observable, of } from 'rxjs';
import { GraphQLApiError } from '../src/utils/errors';
import { NO_ENDPOINT } from '../src/utils/errors/constants';
import { GraphQLError } from 'graphql';

const serverManagedFields = {
id: 'some-id',
Expand Down Expand Up @@ -61,13 +64,13 @@ const client = {
isCancelError,
} as V6Client;

afterEach(() => {
jest.restoreAllMocks();
});
const mockFetchAuthSession = (Amplify as any).Auth
.fetchAuthSession as jest.Mock;

describe('API test', () => {
afterEach(() => {
jest.clearAllMocks();
jest.restoreAllMocks();
});

describe('graphql test', () => {
Expand Down Expand Up @@ -738,32 +741,9 @@ describe('API test', () => {
});

test('multi-auth default case api-key, OIDC as auth mode, but no federatedSign', async () => {
Amplify.configure({
API: {
GraphQL: {
defaultAuthMode: 'apiKey',
apiKey: 'FAKE-KEY',
endpoint: 'https://localhost/graphql',
region: 'local-host-h4x',
},
},
});

Amplify.configure({
API: {
GraphQL: {
defaultAuthMode: 'apiKey',
apiKey: 'FAKE-KEY',
endpoint: 'https://localhost/graphql',
region: 'local-host-h4x',
},
},
});
});

test('multi-auth default case api-key, OIDC as auth mode, but no federatedSign', async () => {
const prevMockAccessToken = mockAccessToken;
mockAccessToken = null;
mockFetchAuthSession.mockRejectedValueOnce(
new Error('mock failing fetchAuthSession() call here.'),
);

Amplify.configure({
API: {
Expand Down Expand Up @@ -806,9 +786,6 @@ describe('API test', () => {
authMode: 'oidc',
}),
).rejects.toThrow('No current user');

// Cleanup:
mockAccessToken = prevMockAccessToken;
});

test('multi-auth using CUP as auth mode, but no userpool', async () => {
Expand Down Expand Up @@ -1342,5 +1319,81 @@ describe('API test', () => {
},
);
});

test('throws a GraphQLResult with NO_ENDPOINT error when endpoint is not configured', () => {
const expectedGraphQLApiError = new GraphQLApiError(NO_ENDPOINT);

Amplify.configure({
API: {
GraphQL: {
defaultAuthMode: 'apiKey',
apiKey: 'FAKE-KEY',
region: 'local-host-h4x',
} as any,
},
});

const graphqlVariables = { id: 'some-id' };

expect(() =>
client.graphql({
query: typedQueries.getThread,
variables: graphqlVariables,
authMode: 'iam',
}),
).rejects.toEqual(
expect.objectContaining({
errors: expect.arrayContaining([
new GraphQLError(
expectedGraphQLApiError.message,
null,
null,
null,
null,
expectedGraphQLApiError,
),
]),
}),
);
});

test('throws a GraphQLResult with NetworkError when the `post()` API throws for network error', () => {
const postAPIThrownError = new Error('Network error');
jest
.spyOn((raw.GraphQLAPI as any)._api, 'post')
.mockRejectedValueOnce(postAPIThrownError);

Amplify.configure({
API: {
GraphQL: {
defaultAuthMode: 'userPool',
endpoint: 'https://localhost/graphql',
region: 'local-host-h4x',
},
},
});

const graphqlVariables = { id: 'some-id' };

expect(
client.graphql({
query: typedQueries.getThread,
variables: graphqlVariables,
}),
).rejects.toEqual(
expect.objectContaining({
errors: expect.arrayContaining([
new GraphQLError(
postAPIThrownError.message,
null,
null,
null,
null,
postAPIThrownError,
),
]),
}),
);
});
});
});
Loading

0 comments on commit e7317d6

Please sign in to comment.