Skip to content

Commit

Permalink
feat: add e2e tests for codegen add when in a non-amplify project (#705)
Browse files Browse the repository at this point in the history
* feat: add e2e tests for codegen add when in a non-amplify project

* chore: add some additional test cases
  • Loading branch information
alharris-at authored Sep 19, 2023
1 parent a9d22c3 commit ff4e221
Show file tree
Hide file tree
Showing 7 changed files with 1,508 additions and 62 deletions.
9 changes: 5 additions & 4 deletions .codebuild/e2e_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,23 @@ batch:
depend-on:
- publish_to_local_registry
- identifier: >-
build_app_ts_uninitialized_project_modelgen_android_uninitialized_project_modelgen_flutter_uninitialized_project_modelgen_ios
build_app_ts_uninitialized_project_codegen_js_uninitialized_project_modelgen_android_uninitialized_project_modelgen_flutter
buildspec: .codebuild/run_e2e_tests.yml
env:
compute-type: BUILD_GENERAL1_LARGE
variables:
TEST_SUITE: >-
src/__tests__/build-app-ts.test.ts|src/__tests__/uninitialized-project-modelgen-android.test.ts|src/__tests__/uninitialized-project-modelgen-flutter.test.ts|src/__tests__/uninitialized-project-modelgen-ios.test.ts
src/__tests__/build-app-ts.test.ts|src/__tests__/uninitialized-project-codegen-js.test.ts|src/__tests__/uninitialized-project-modelgen-android.test.ts|src/__tests__/uninitialized-project-modelgen-flutter.test.ts
CLI_REGION: ap-southeast-1
depend-on:
- publish_to_local_registry
- identifier: uninitialized_project_modelgen_js
- identifier: uninitialized_project_modelgen_ios_uninitialized_project_modelgen_js
buildspec: .codebuild/run_e2e_tests.yml
env:
compute-type: BUILD_GENERAL1_LARGE
variables:
TEST_SUITE: src/__tests__/uninitialized-project-modelgen-js.test.ts
TEST_SUITE: >-
src/__tests__/uninitialized-project-modelgen-ios.test.ts|src/__tests__/uninitialized-project-modelgen-js.test.ts
CLI_REGION: ap-southeast-2
depend-on:
- publish_to_local_registry
Expand Down
102 changes: 84 additions & 18 deletions packages/amplify-codegen-e2e-core/src/categories/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,40 @@ export const generateModelsWithOptions = (cwd: string, options: Record<string, a
});
});

export function generateStatementsAndTypes(cwd: string) : Promise<void> {
export function generateStatementsAndTypes(cwd: string, errorMessage?: string) : Promise<void> {
return new Promise((resolve, reject) => {
spawn(getCLIPath(), ['codegen'], { cwd, stripColors: true })
const chain = spawn(getCLIPath(), ['codegen'], { cwd, stripColors: true })

if (errorMessage) {
chain.wait(errorMessage);
}

return chain.run((err: Error) => {
if (!err) {
resolve();
} else {
reject(err);
}
})
});
}

export function generateStatements(cwd: string) : Promise<void> {
return new Promise((resolve, reject) => {
spawn(getCLIPath(), ['codegen', 'statements'], { cwd, stripColors: true })
.run((err: Error) => {
if (!err) {
resolve();
} else {
reject(err);
}
})
});
}

export function generateTypes(cwd: string) : Promise<void> {
return new Promise((resolve, reject) => {
spawn(getCLIPath(), ['codegen', 'types'], { cwd, stripColors: true })
.run((err: Error) => {
if (!err) {
resolve();
Expand All @@ -46,7 +77,10 @@ export function generateStatementsAndTypes(cwd: string) : Promise<void> {
// CLI workflow to add codegen to Amplify project
export function addCodegen(cwd: string, settings: any = {}): Promise<void> {
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");
Expand Down Expand Up @@ -166,22 +200,26 @@ export function generateModelIntrospection(cwd: string, settings: { outputDir?:
}

// CLI workflow to add codegen to non-Amplify JS project
export function addCodegenNonAmplifyJS(cwd: string): Promise<void> {
export function addCodegenNonAmplifyJS(cwd: string, params: Array<string>, initialFailureMessage?: string): Promise<void> {
return new Promise((resolve, reject) => {
const cmdOptions = ['codegen', 'add'];
const chain = spawn(getCLIPath(), cmdOptions, { cwd, stripColors: true });
chain
.wait("Choose the type of app that you're building")
.sendCarriageReturn()
.wait('What javascript framework are you using')
.sendCarriageReturn()
.wait('Choose the code generation language target').sendCarriageReturn()
.wait('Enter the file name pattern of graphql queries, mutations and subscriptions')
.sendCarriageReturn()
.wait('Do you want to generate/update all possible GraphQL operations')
.sendLine('y')
.wait('Enter maximum statement depth [increase from default if your schema is deeply')
.sendCarriageReturn();
const chain = spawn(getCLIPath(), ['codegen', 'add', ...params], { cwd, stripColors: true });

if (initialFailureMessage) {
chain.wait(initialFailureMessage)
} else {
chain
.wait("Choose the type of app that you're building")
.sendCarriageReturn()
.wait('What javascript framework are you using')
.sendCarriageReturn()
.wait('Choose the code generation language target').sendCarriageReturn()
.wait('Enter the file name pattern of graphql queries, mutations and subscriptions')
.sendCarriageReturn()
.wait('Do you want to generate/update all possible GraphQL operations')
.sendLine('y')
.wait('Enter maximum statement depth [increase from default if your schema is deeply')
.sendCarriageReturn();
}

chain.run((err: Error) => {
if (!err) {
Expand All @@ -192,3 +230,31 @@ export function addCodegenNonAmplifyJS(cwd: string): Promise<void> {
});
});
}

export function addCodegenNonAmplifyTS(cwd: string, params: Array<string>, initialFailureMessage?: string): Promise<void> {
return new Promise((resolve, reject) => {
const chain = spawn(getCLIPath(), ['codegen', 'add', ...params], { cwd, stripColors: true });

if (initialFailureMessage) {
chain.wait(initialFailureMessage)
} else {
chain
.wait("Choose the type of app that you're building").sendCarriageReturn()
.wait('What javascript framework are you using').sendCarriageReturn()
.wait('Choose the code generation language target').sendKeyDown().sendCarriageReturn()
.wait('Enter the file name pattern of graphql queries, mutations and subscriptions').sendCarriageReturn()
.wait('Do you want to generate/update all possible GraphQL operations').sendLine('y')
.wait('Enter maximum statement depth [increase from default if your schema is deeply').sendCarriageReturn()
.wait('Enter the file name for the generated code').sendCarriageReturn()
.wait('Do you want to generate code for your newly created GraphQL API').sendCarriageReturn();
}

chain.run((err: Error) => {
if (!err) {
resolve();
} else {
reject(err);
}
});
});
}
11 changes: 11 additions & 0 deletions packages/amplify-codegen-e2e-tests/schemas/sdl/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type Query {
echo: String
}

type Mutation {
mymutation: String
}

type Subscription {
mysub: String
}
Loading

0 comments on commit ff4e221

Please sign in to comment.