Skip to content

Commit

Permalink
feat: add e2e tests for codegen add when in a non-amplify project
Browse files Browse the repository at this point in the history
  • Loading branch information
alharris-at committed Sep 18, 2023
1 parent a9d22c3 commit e9f98db
Show file tree
Hide file tree
Showing 7 changed files with 1,483 additions and 59 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
95 changes: 80 additions & 15 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 Down Expand Up @@ -166,22 +197,27 @@ 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, 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();

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 +228,32 @@ export function addCodegenNonAmplifyJS(cwd: string): Promise<void> {
});
});
}

export function addCodegenNonAmplifyTS(cwd: string, initialFailureMessage?: string): Promise<void> {
return new Promise((resolve, reject) => {
const cmdOptions = ['codegen', 'add'];
const chain = spawn(getCLIPath(), cmdOptions, { 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 e9f98db

Please sign in to comment.