Skip to content

Commit

Permalink
chore: enable eslint for __tests__ for adapter-nextjs aws-amplify and…
Browse files Browse the repository at this point in the history
… core (#13108)

* chore(repo): setup the base for lint __tests__ codebase

* chore(adapter-nextjs): run yarn lint:fix

* chore(adapter-nextjs): manual fix linter reported erros

* chore(aws-amplify): run yarn lint:fix
 Please enter the commit message for your changes. Lines starting

* chore(aws-amplify): add camelcase var name exception: phone_number

* chore(core): run yarn lint:fix

* chore(core): manual fix linter reported errors under __tests__

* apply suggestions

* turn off node callback function convention check

* do not mock console functions globaly but noopify them

* apply suggestions

* Revert "do not mock console functions globaly but noopify them"

This reverts commit 913b11e.

* refactor a no-op function

* remove // eslint-disable-next-line no-new
  • Loading branch information
HuiSF committed Mar 28, 2024
1 parent 962179b commit c4f1432
Show file tree
Hide file tree
Showing 55 changed files with 508 additions and 302 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
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import {
CognitoAWSCredentialsAndIdentityIdProvider,
DefaultIdentityIdStore,
} from '@aws-amplify/auth/cognito';
import {
CredentialsAndIdentityIdProvider,
AuthConfig,
KeyValueStorageInterface,
} from '@aws-amplify/core';
import { AuthConfig, KeyValueStorageInterface } from '@aws-amplify/core';

import { createAWSCredentialsAndIdentityIdProvider } from '../../../../src/adapter-core';

jest.mock('@aws-amplify/auth/cognito');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
TokenOrchestrator,
refreshAuthTokens,
} from '@aws-amplify/auth/cognito';

import { AuthConfig, KeyValueStorageInterface } from '@aws-amplify/core';

import { createUserPoolsTokenProvider } from '../../../../src/adapter-core';

jest.mock('@aws-amplify/auth/cognito');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { runWithAmplifyServerContext } from '../../src/adapter-core';
import {
createAmplifyServerContext,
destroyAmplifyServerContext,
} from '@aws-amplify/core/internals/adapter-core';

import { runWithAmplifyServerContext } from '../../src/adapter-core';

// mock serverContext
jest.mock('@aws-amplify/core/internals/adapter-core');
const mockCreateAmplifyServerContext = createAmplifyServerContext as jest.Mock;
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-amplify/__tests__/initSingleton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
ResourcesConfig,
defaultStorage,
} from '@aws-amplify/core';

import {
cognitoUserPoolsTokenProvider,
cognitoCredentialsProvider,
cognitoUserPoolsTokenProvider,
} from '../src/auth/cognito';

import { Amplify } from '../src';

jest.mock('@aws-amplify/core');
Expand Down
Loading

0 comments on commit c4f1432

Please sign in to comment.