Skip to content

Commit

Permalink
feat(amplify-util-mock): import custom port from mock.json (#10978)
Browse files Browse the repository at this point in the history
chore: fix lint

Co-authored-by: Takahiro Suzuki <tkasuz@amazon.co.jp>
  • Loading branch information
tkasuz and Takahiro Suzuki committed Jun 16, 2023
1 parent 19ed508 commit 31f250c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
19 changes: 18 additions & 1 deletion packages/amplify-util-mock/src/__tests__/api/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import { $TSContext, AmplifyError, pathManager } from '@aws-amplify/amplify-cli-core';
import { $TSContext, AmplifyError, JSONUtilities, pathManager } from '@aws-amplify/amplify-cli-core';
import { APITest } from '../../api/api';
import * as lambdaInvoke from '../../api/lambda-invoke';
import { getMockSearchableTriggerDirectory } from '../../utils';
import { ConfigOverrideManager } from '../../utils/config-override';
import { run } from '../../commands/mock/api';
import { start } from '../../api';

jest.mock('@aws-amplify/amplify-cli-core', () => ({
...(jest.requireActual('@aws-amplify/amplify-cli-core') as Record<string, unknown>),
Expand Down Expand Up @@ -139,4 +140,20 @@ describe('Test Mock API methods', () => {
await run(mockContext);
await expect(mockContext.print.error).toHaveBeenCalledWith('Failed to start API Mocking.');
});

it('attempts to set custom port correctly', async () => {
const GRAPHQL_PORT = 8081;
const mockContext = {
amplify: {
getEnvInfo: jest.fn().mockReturnValue({ projectPath: mockProjectRoot }),
pathManager: {
getGitIgnoreFilePath: jest.fn(),
},
},
} as unknown as $TSContext;
const startMock = jest.spyOn(APITest.prototype, 'start').mockResolvedValueOnce();
jest.spyOn(JSONUtilities, 'readJson').mockReturnValue({ graphqlPort: GRAPHQL_PORT });
await start(mockContext);
expect(startMock.mock.calls[0][1]).toBe(GRAPHQL_PORT);
});
});
4 changes: 3 additions & 1 deletion packages/amplify-util-mock/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { APITest } from './api';
import { addMockDataToGitIgnore, addMockAPIResourcesToGitIgnore } from '../utils';
import { getMockConfig } from '../utils/mock-config-file';

export async function start(context) {
const testApi = new APITest();
try {
addMockDataToGitIgnore(context);
addMockAPIResourcesToGitIgnore(context);
await testApi.start(context);
const mockConfig = await getMockConfig(context);
await testApi.start(context, mockConfig.graphqlPort, mockConfig.graphqlPort);
} catch (e) {
console.log(e);
// Sending term signal so we clean up after ourselves
Expand Down

0 comments on commit 31f250c

Please sign in to comment.