diff --git a/packages/amplify-codegen-e2e-core/src/categories/codegen.ts b/packages/amplify-codegen-e2e-core/src/categories/codegen.ts index 3580048c8..ab5a83a1f 100644 --- a/packages/amplify-codegen-e2e-core/src/categories/codegen.ts +++ b/packages/amplify-codegen-e2e-core/src/categories/codegen.ts @@ -77,7 +77,10 @@ export function generateTypes(cwd: string) : Promise { // CLI workflow to add codegen to Amplify project export function addCodegen(cwd: string, settings: any = {}): Promise { return new Promise((resolve, reject) => { - const chain = spawn(getCLIPath(), ['codegen', 'add'], { cwd, stripColors: true }); + const params = settings.params + ? ['codegen', 'add', ...settings.params] + : ['codegen', 'add']; + const chain = spawn(getCLIPath(), params, { cwd, stripColors: true }); if (settings.isAPINotAdded) { chain.wait("There are no GraphQL APIs available."); chain.wait("Add by running $amplify api add"); @@ -197,10 +200,9 @@ export function generateModelIntrospection(cwd: string, settings: { outputDir?: } // CLI workflow to add codegen to non-Amplify JS project -export function addCodegenNonAmplifyJS(cwd: string, initialFailureMessage?: string): Promise { +export function addCodegenNonAmplifyJS(cwd: string, params: Array, initialFailureMessage?: string): Promise { return new Promise((resolve, reject) => { - const cmdOptions = ['codegen', 'add']; - const chain = spawn(getCLIPath(), cmdOptions, { cwd, stripColors: true }); + const chain = spawn(getCLIPath(), ['codegen', 'add', ...params], { cwd, stripColors: true }); if (initialFailureMessage) { chain.wait(initialFailureMessage) @@ -229,10 +231,9 @@ export function addCodegenNonAmplifyJS(cwd: string, initialFailureMessage?: stri }); } -export function addCodegenNonAmplifyTS(cwd: string, initialFailureMessage?: string): Promise { +export function addCodegenNonAmplifyTS(cwd: string, params: Array, initialFailureMessage?: string): Promise { return new Promise((resolve, reject) => { - const cmdOptions = ['codegen', 'add']; - const chain = spawn(getCLIPath(), cmdOptions, { cwd, stripColors: true }); + const chain = spawn(getCLIPath(), ['codegen', 'add', ...params], { cwd, stripColors: true }); if (initialFailureMessage) { chain.wait(initialFailureMessage) diff --git a/packages/amplify-codegen-e2e-tests/src/__tests__/add-codegen-js.test.ts b/packages/amplify-codegen-e2e-tests/src/__tests__/add-codegen-js.test.ts index ce07f5340..312ea334d 100644 --- a/packages/amplify-codegen-e2e-tests/src/__tests__/add-codegen-js.test.ts +++ b/packages/amplify-codegen-e2e-tests/src/__tests__/add-codegen-js.test.ts @@ -77,4 +77,12 @@ describe('codegen add tests - JS', () => { it(`Adding codegen works as expected`, async () => { await testAddCodegen(config, projectRoot, schema); }); + + it(`supports add codegen with redundant region parameter`, async () => { + await testAddCodegen(config, projectRoot, schema, ['--region', 'us-fake-1']); + }); + + it(`supports add codegen with redundant apiId parameter`, async () => { + await testAddCodegen(config, projectRoot, schema, ['--apiId', 'mockApiId']); + }); }); diff --git a/packages/amplify-codegen-e2e-tests/src/__tests__/uninitialized-project-codegen-js.test.ts b/packages/amplify-codegen-e2e-tests/src/__tests__/uninitialized-project-codegen-js.test.ts index 9ea04aa14..076e7b952 100644 --- a/packages/amplify-codegen-e2e-tests/src/__tests__/uninitialized-project-codegen-js.test.ts +++ b/packages/amplify-codegen-e2e-tests/src/__tests__/uninitialized-project-codegen-js.test.ts @@ -28,6 +28,16 @@ describe('codegen add tests - JS', () => { }); }); + it(`region is ignored if schema file is provided`, async () => { + await testAddCodegenUninitialized({ + projectRoot, + config: javascriptConfig, + sdlFilename: 'schema.graphql', + expectedFilenames: ['mutations.js', 'queries.js', 'subscriptions.js'], + additionalParams: ['--region', 'us-fake-1'], + }); + }); + it(`json sdl file`, async () => { await testAddCodegenUninitialized({ projectRoot, diff --git a/packages/amplify-codegen-e2e-tests/src/codegen-tests-base/add-codegen.ts b/packages/amplify-codegen-e2e-tests/src/codegen-tests-base/add-codegen.ts index 2ef8e1620..4ae362489 100644 --- a/packages/amplify-codegen-e2e-tests/src/codegen-tests-base/add-codegen.ts +++ b/packages/amplify-codegen-e2e-tests/src/codegen-tests-base/add-codegen.ts @@ -17,7 +17,7 @@ import path from 'path'; import { isNotEmptyDir } from '../utils'; import { getGraphQLConfigFilePath, testSetupBeforeAddCodegen, testValidGraphQLConfig } from "./test-setup"; -export async function testAddCodegen(config: AmplifyFrontendConfig, projectRoot: string, schema: string) { +export async function testAddCodegen(config: AmplifyFrontendConfig, projectRoot: string, schema: string, additionalParams?: Array) { // init project and add API category await initProjectWithProfile(projectRoot, { ...config }); const projectName = createRandomName(); @@ -27,7 +27,7 @@ export async function testAddCodegen(config: AmplifyFrontendConfig, projectRoot: const userSourceCodePath = testSetupBeforeAddCodegen(projectRoot, config); // add codegen succeeds - await expect(addCodegen(projectRoot, { ...config })).resolves.not.toThrow(); + await expect(addCodegen(projectRoot, { ...config, params: additionalParams ?? [] })).resolves.not.toThrow(); // pre-existing file should still exist expect(existsSync(userSourceCodePath)).toBe(true); @@ -45,12 +45,24 @@ export type TestAddCodegenUninitializedProps = { dropAndRunCodegenStatements?: boolean; dropAndRunCodegenTypes?: boolean; initialFailureMessage?: string; + additionalParams?: Array; }; const assertTypeFileExists = (projectRoot: string): void => { expect(existsSync(path.join(projectRoot, 'src', 'API.ts'))).toBe(true) }; +/** + * Ensure that all values provided in the expected set are present in the received set, allowing for additional values in received. + * @param expectedValues the expected values to check + * @param receivedValues the received values to check + */ +const ensureAllExpectedValuesAreReceived = (expectedValues: Array, receivedValues: Array): void => { + const receivedValueSet = new Set(receivedValues); + console.log(`Comparing received values: ${JSON.stringify(receivedValues)} to expected values: ${JSON.stringify(expectedValues)}`); + expectedValues.forEach((expectedFilename) => expect(receivedValueSet.has(expectedFilename)).toBe(true)); +}; + export async function testAddCodegenUninitialized({ config, projectRoot, @@ -60,6 +72,7 @@ export async function testAddCodegenUninitialized({ dropAndRunCodegenStatements, dropAndRunCodegenTypes, initialFailureMessage, + additionalParams, }: TestAddCodegenUninitializedProps) { // Setup the non-amplify project with schema and pre-existing files const userSourceCodePath = testSetupBeforeAddCodegen(projectRoot, config); @@ -73,10 +86,10 @@ export async function testAddCodegenUninitialized({ // add codegen without init switch (config.frontendType) { case AmplifyFrontend.javascript: - await addCodegenNonAmplifyJS(projectRoot, initialFailureMessage); + await addCodegenNonAmplifyJS(projectRoot, additionalParams ?? [], initialFailureMessage); break; case AmplifyFrontend.typescript: - await addCodegenNonAmplifyTS(projectRoot, initialFailureMessage); + await addCodegenNonAmplifyTS(projectRoot, additionalParams ?? [], initialFailureMessage); break; default: throw new Error(`Received unexpected frontendType ${config.frontendType}`); @@ -90,9 +103,7 @@ export async function testAddCodegenUninitialized({ // pre-existing file should still exist expect(existsSync(userSourceCodePath)).toBe(true); // GraphQL statements are generated - const generatedStatementFiles = new Set(readdirSync(path.join(projectRoot, config.graphqlCodegenDir))); - console.log(`Comparing written files: ${JSON.stringify(Array.from(generatedStatementFiles))} to expected files: ${JSON.stringify(expectedFilenames)}`); - expectedFilenames.forEach((expectedFilename) => expect(generatedStatementFiles.has(expectedFilename)).toBe(true)); + ensureAllExpectedValuesAreReceived(expectedFilenames, readdirSync(path.join(projectRoot, config.graphqlCodegenDir))) // graphql configuration should be added expect(existsSync(getGraphQLConfigFilePath(projectRoot))).toBe(true); if (config.frontendType === AmplifyFrontend.typescript) { @@ -111,9 +122,7 @@ export async function testAddCodegenUninitialized({ await generateStatementsAndTypes(projectRoot); // GraphQL statements are regenerated - const regeneratedStatementFiles = new Set(readdirSync(path.join(projectRoot, config.graphqlCodegenDir))); - console.log(`Comparing written files: ${JSON.stringify(Array.from(regeneratedStatementFiles))} to expected files: ${JSON.stringify(expectedFilenames)}`); - expectedFilenames.forEach((expectedFilename) => expect(regeneratedStatementFiles.has(expectedFilename)).toBe(true)); + ensureAllExpectedValuesAreReceived(expectedFilenames, readdirSync(path.join(projectRoot, config.graphqlCodegenDir))) if (config.frontendType === AmplifyFrontend.typescript) { assertTypeFileExists(projectRoot) @@ -124,9 +133,7 @@ export async function testAddCodegenUninitialized({ await generateStatements(projectRoot); // GraphQL statements are regenerated - const regeneratedStatementFiles = new Set(readdirSync(path.join(projectRoot, config.graphqlCodegenDir))); - console.log(`Comparing written files: ${JSON.stringify(Array.from(regeneratedStatementFiles))} to expected files: ${JSON.stringify(expectedFilenames)}`); - expectedFilenames.forEach((expectedFilename) => expect(regeneratedStatementFiles.has(expectedFilename)).toBe(true)); + ensureAllExpectedValuesAreReceived(expectedFilenames, readdirSync(path.join(projectRoot, config.graphqlCodegenDir))) } if (dropAndRunCodegenTypes) {