Skip to content

Commit fd7dd3b

Browse files
authored
Merge pull request #300 from aws-amplify/e2e-testing
Fix E2e tests by including the API category walkthrough changes
2 parents 471fd5b + f72a9b7 commit fd7dd3b

File tree

8 files changed

+44
-87
lines changed

8 files changed

+44
-87
lines changed

packages/amplify-codegen-e2e-core/src/categories/api.ts

Lines changed: 6 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ const defaultOptions: AddApiOptions = {
3232
testingWithLatestCodebase: false
3333
};
3434

35-
export function addApiWithoutSchema(cwd: string, opts: Partial<AddApiOptions> = {}) {
35+
export function addApiWithoutSchema(cwd: string, opts: Partial<AddApiOptions & { apiKeyExpirationDays: number }> = {}) {
3636
const options = _.assign(defaultOptions, opts);
3737
return new Promise<void>((resolve, reject) => {
38-
spawn(getCLIPath(), ['add', 'api'], { cwd, stripColors: true })
38+
spawn(getCLIPath(options.testingWithLatestCodebase), ['add', 'api'], { cwd, stripColors: true })
3939
.wait('Please select from one of the below mentioned services:')
4040
.sendCarriageReturn()
4141
.wait(/.*Here is the GraphQL API that we will create. Select a setting to edit or continue.*/)
4242
.sendKeyUp(3)
43+
.sendCarriageReturn()
4344
.wait('Provide API name:')
4445
.sendLine(options.apiName)
4546
.wait(/.*Here is the GraphQL API that we will create. Select a setting to edit or continue.*/)
@@ -48,10 +49,9 @@ export function addApiWithoutSchema(cwd: string, opts: Partial<AddApiOptions> =
4849
.sendCarriageReturn()
4950
.wait('Do you want to edit the schema now?')
5051
.sendConfirmNo()
51-
.wait('Successfully added resource collectionapi locally')
52-
// .wait(
53-
// '"amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloud',
54-
// )
52+
.wait(
53+
'"amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloud',
54+
)
5555
.run((err: Error) => {
5656
if (!err) {
5757
resolve();
@@ -126,45 +126,6 @@ export function addApiWithBlankSchemaAndConflictDetection(cwd: string) {
126126
});
127127
}
128128

129-
export function addApiWithSchemaAndConflictDetection(cwd: string, schemaFile: string) {
130-
const schemaPath = getSchemaPath(schemaFile);
131-
return new Promise<void>((resolve, reject) => {
132-
spawn(getCLIPath(), ['add', 'api'], { cwd, stripColors: true })
133-
.wait('Please select from one of the below mentioned services:')
134-
.sendCarriageReturn()
135-
.wait('Provide API name:')
136-
.sendCarriageReturn()
137-
.wait(/.*Choose the default authorization type for the API.*/)
138-
.sendCarriageReturn()
139-
.wait(/.*Enter a description for the API key.*/)
140-
.sendCarriageReturn()
141-
.wait(/.*After how many days from now the API key should expire.*/)
142-
.sendCarriageReturn()
143-
.wait(/.*Do you want to configure advanced settings for the GraphQL API.*/)
144-
.sendLine(KEY_DOWN_ARROW) // Down
145-
.wait(/.*Configure additional auth types.*/)
146-
.sendLine('n')
147-
.wait(/.*Enable conflict detection.*/)
148-
.sendLine('y')
149-
.wait(/.*Select the default resolution strategy.*/)
150-
.sendCarriageReturn()
151-
.wait(/.*Do you have an annotated GraphQL schema.*/)
152-
.sendLine('y')
153-
.wait('Provide your schema file path:')
154-
.sendLine(schemaPath)
155-
.wait(
156-
'"amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloud',
157-
)
158-
.run((err: Error) => {
159-
if (!err) {
160-
resolve();
161-
} else {
162-
reject(err);
163-
}
164-
});
165-
});
166-
}
167-
168129
export function updateApiSchema(cwd: string, projectName: string, schemaName: string, forceUpdate: boolean = false) {
169130
const testSchemaPath = getSchemaPath(schemaName);
170131
let schemaText = fs.readFileSync(testSchemaPath).toString();
@@ -265,26 +226,6 @@ export function apiDisableDataStore(cwd: string, settings: any) {
265226
});
266227
}
267228

268-
export function apiUpdateToggleDataStore(cwd: string, settings: any = {}) {
269-
return new Promise<void>((resolve, reject) => {
270-
spawn(getCLIPath(settings.testingWithLatestCodebase), ['update', 'api'], { cwd, stripColors: true })
271-
.wait('Please select from one of the below mentioned services:')
272-
.sendCarriageReturn()
273-
.wait('Select from the options below')
274-
.send(KEY_DOWN_ARROW)
275-
.sendLine(KEY_DOWN_ARROW) // select enable datastore for the api
276-
.wait(/.*Successfully updated resource.*/)
277-
.sendEof()
278-
.run((err: Error) => {
279-
if (!err) {
280-
resolve();
281-
} else {
282-
reject(err);
283-
}
284-
});
285-
});
286-
}
287-
288229
export function updateAPIWithResolutionStrategyWithoutModels(cwd: string, settings: any) {
289230
return new Promise<void>((resolve, reject) => {
290231
spawn(getCLIPath(settings.testingWithLatestCodebase), ['update', 'api'], { cwd, stripColors: true })

packages/amplify-codegen-e2e-tests/src/__tests__/add-codegen-js.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {
22
createNewProjectDir,
33
initProjectWithProfile,
4-
addApiWithSchema,
54
addCodegen,
6-
DEFAULT_JS_CONFIG
5+
DEFAULT_JS_CONFIG,
6+
createRandomName,
7+
addApiWithoutSchema,
8+
updateApiSchema
79
} from "amplify-codegen-e2e-core";
810
import { existsSync } from "fs";
911
import path from 'path';
@@ -46,7 +48,9 @@ describe('codegen add tests - JS', () => {
4648
it(`Adding codegen twice gives appropriate message`, async () => {
4749
// init project and add API category
4850
await initProjectWithProfile(projectRoot, { ...config });
49-
await addApiWithSchema(projectRoot, schema);
51+
const projectName = createRandomName();
52+
await addApiWithoutSchema(projectRoot, { apiName: projectName });
53+
await updateApiSchema(projectRoot, projectName, schema);
5054

5155
const userSourceCodePath = testSetupBeforeAddCodegen(projectRoot, config);
5256

packages/amplify-codegen-e2e-tests/src/__tests__/env-codegen.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import {
22
createNewProjectDir,
33
deleteProjectDir,
44
deleteProject,
5-
addApiWithSchema,
65
initJSProjectWithProfile,
76
amplifyPush,
87
generateModels,
8+
addApiWithoutSchema,
99
updateApiSchema
1010
} from "amplify-codegen-e2e-core";
1111
import { addEnvironment, checkoutEnvironment } from "../environment/env";
@@ -27,7 +27,9 @@ describe('env codegen tests', () => {
2727
it('should generate models in different environments', async () => {
2828
//create amplify project with enva
2929
await initJSProjectWithProfile(projectRoot, { envName: 'enva' });
30-
await addApiWithSchema(projectRoot, schema, { apiName });
30+
await addApiWithoutSchema(projectRoot, { apiName: apiName });
31+
await updateApiSchema(projectRoot, apiName, schema);
32+
3133
await amplifyPush(projectRoot);
3234
//create new envb
3335
await addEnvironment(projectRoot, { envName: 'envb' });

packages/amplify-codegen-e2e-tests/src/__tests__/pull-codegen.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import {
33
deleteProjectDir,
44
deleteProject,
55
initJSProjectWithProfile,
6-
addApiWithSchemaAndConflictDetection,
6+
addApiWithBlankSchemaAndConflictDetection,
7+
updateApiSchema,
8+
createRandomName,
79
amplifyPush,
810
amplifyPull,
911
getAppId,
@@ -37,9 +39,11 @@ describe('Amplify pull in amplify app with codegen tests', () => {
3739
let projectRoot: string;
3840
let appId: string;
3941
beforeAll(async () => {
42+
const name = createRandomName();
4043
projectRoot = await createNewProjectDir('pullCodegen');
41-
await initJSProjectWithProfile(projectRoot, { envName, disableAmplifyAppCreation: false });
42-
await addApiWithSchemaAndConflictDetection(projectRoot, schema);
44+
await initJSProjectWithProfile(projectRoot, { name, envName, disableAmplifyAppCreation: false });
45+
await addApiWithBlankSchemaAndConflictDetection(projectRoot);
46+
await updateApiSchema(projectRoot, name, schema);
4347
await amplifyPush(projectRoot);
4448
appId = getAppId(projectRoot);
4549
expect(appId).toBeDefined();

packages/amplify-codegen-e2e-tests/src/__tests__/remove-codegen-js.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import {
33
DEFAULT_JS_CONFIG,
44
removeCodegen,
55
initProjectWithProfile,
6-
addApiWithSchema
6+
addApiWithoutSchema,
7+
updateApiSchema,
8+
createRandomName
79
} from "amplify-codegen-e2e-core";
810
import { deleteAmplifyProject, testRemoveCodegen } from '../codegen-tests-base';
911

@@ -23,7 +25,9 @@ describe('codegen remove tests - JS', () => {
2325
it(`Give appropriate message during remove when codegen is not added in JS project`, async () => {
2426
// init project and add API category
2527
await initProjectWithProfile(projectRoot, DEFAULT_JS_CONFIG);
26-
await addApiWithSchema(projectRoot, schema);
28+
const projectName = createRandomName();
29+
await addApiWithoutSchema(projectRoot, { apiName: projectName });
30+
await updateApiSchema(projectRoot, projectName, schema);
2731

2832
// remove command should give expected message
2933
await expect(removeCodegen(projectRoot, false)).resolves.not.toThrow();

packages/amplify-codegen-e2e-tests/src/codegen-tests-base/datastore-modelgen.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import {
22
initProjectWithProfile,
33
addApiWithBlankSchemaAndConflictDetection,
4-
generateModels,
5-
AmplifyFrontendConfig,
4+
updateApiSchema,
65
createRandomName,
7-
updateApiSchema
6+
generateModels,
7+
AmplifyFrontendConfig
88
} from "amplify-codegen-e2e-core";
99
import { existsSync } from "fs";
1010
import path from 'path';
1111
import { isNotEmptyDir, generateSourceCode } from '../utils';
1212

1313
export async function testCodegenModels(config: AmplifyFrontendConfig, projectRoot: string, schema: string) {
14+
const name = createRandomName();
15+
1416
// init project and add API category
15-
await initProjectWithProfile(projectRoot, { ...config });
17+
await initProjectWithProfile(projectRoot, { name, ...config });
1618

1719
//enable datastore
18-
const projectName = createRandomName();
1920
await addApiWithBlankSchemaAndConflictDetection(projectRoot);
20-
await updateApiSchema(projectRoot, projectName, schema);
21+
await updateApiSchema(projectRoot, name, schema);
2122

2223
//generate pre existing user file
2324
const userSourceCodePath = generateSourceCode(projectRoot, config.srcDir);

packages/amplify-codegen-e2e-tests/src/codegen-tests-base/push-codegen.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {
55
createRandomName,
66
amplifyPushWithCodegenAdd,
77
AmplifyFrontendConfig,
8-
apiUpdateToggleDataStore,
9-
amplifyPushWithCodegenUpdate
8+
amplifyPushWithCodegenUpdate,
9+
updateAPIWithResolutionStrategyWithModels
1010
} from "amplify-codegen-e2e-core";
1111
import { existsSync } from "fs";
1212
import path from 'path';
@@ -32,7 +32,7 @@ export async function testPushCodegen(config: AmplifyFrontendConfig, projectRoot
3232
expect(isNotEmptyDir(path.join(projectRoot, config.graphqlCodegenDir))).toBe(true);
3333

3434
//enable datastore
35-
await apiUpdateToggleDataStore(projectRoot);
35+
await updateAPIWithResolutionStrategyWithModels(projectRoot, {});
3636
//push with codegen update
3737
await amplifyPushWithCodegenUpdate(projectRoot);
3838
expect(existsSync(userSourceCodePath)).toBe(true);

packages/amplify-codegen-e2e-tests/src/schema-api-directives/tests/key-howTo4.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import _ from 'lodash';
2-
import { addApiWithSchemaAndConflictDetection, amplifyPush } from 'amplify-codegen-e2e-core';
2+
import { addApiWithBlankSchemaAndConflictDetection, updateApiSchema, amplifyPush } from 'amplify-codegen-e2e-core';
33
import { getApiKey, configureAmplify, getConfiguredAppsyncClientAPIKeyAuth } from '../authHelper';
44
import { testQueries, testMutations } from '../common';
55

@@ -269,8 +269,9 @@ export const expected_result_query5 = {
269269
},
270270
};
271271

272-
export async function runTest(projectDir: string, testModule: any) {
273-
await addApiWithSchemaAndConflictDetection(projectDir, testModule.schemaName);
272+
export async function runTest(projectDir: string, testModule: any, appName: string) {
273+
await addApiWithBlankSchemaAndConflictDetection(projectDir);
274+
await updateApiSchema(projectDir, appName, testModule.schemaName);
274275
await amplifyPush(projectDir);
275276

276277
const awsconfig = configureAmplify(projectDir);

0 commit comments

Comments
 (0)