Skip to content

Commit

Permalink
test: add additional cases
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronZyLee committed May 3, 2024
1 parent 51fefab commit e5d33db
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path';
import { createNewProjectDir, deleteProjectDir } from "@aws-amplify/amplify-codegen-e2e-core";
import { deleteSandbox, generateForms, generateGraphqlClientCode, generateOutputs, initGen2Project, sandboxDeploy } from "../gen2-codegen-tests-base/commands";
import { ClientCodegenConfig, GraphqlCodegenConfig, IntrospectionCodegenConfig, ModelgenConfig, deleteSandbox, generateForms, generateGraphqlClientCode, generateOutputs, initGen2Project, sandboxDeploy, testGraphqlClientCodegen } from "../gen2-codegen-tests-base/";

describe('GraphQL generator for Gen2 e2e tests', () => {
let projRoot: string;
Expand All @@ -26,8 +26,41 @@ describe('GraphQL generator for Gen2 e2e tests', () => {
it('should not throw error when generating forms', async () => {
await expect(generateForms(projRoot)).resolves.not.toThrow();
});

it('should not throw error when generating GraphQL client code', async () => {
await expect(generateGraphqlClientCode(projRoot, { outDir: 'codegen', format: 'introspection'})).resolves.not.toThrow();
});
describe('Graphql client codegen', () => {
// introspection
const introspectionCodegenConfigs: IntrospectionCodegenConfig[] = [
{ outDir: 'codegen', format: 'introspection' }
];
introspectionCodegenConfigs.forEach(config => {
it(`should not throw error when generating GraphQL client code in format ${config.format}`, async () => {
await testGraphqlClientCodegen(projRoot, config);
});
});
// modelgen
const modelTargets = ['java', 'swift', 'javascript', 'typescript', 'dart']
const modelgenConfigs: ModelgenConfig[] = modelTargets.map(target => {
return { outDir: 'codegen', format: 'modelgen', modelTarget: target };
})
modelgenConfigs.forEach(config => {
it(`should not throw error when generating GraphQL client code in format ${config.format} with target ${config.modelTarget}`, async () => {
await testGraphqlClientCodegen(projRoot, config);
});
});
// graphql codegen
const statementTargets = ['javascript', 'graphql', 'flow', 'typescript', 'angular'];
const typeTargets = ['json', 'swift', 'typescript', 'flow', 'scala', 'flow-modern', 'angular'];
const typeTargetConfigs = typeTargets.map(tt => { return { outDir: 'codegen', format: 'graphql-codegen', typeTarget: tt }});
const graphqlCodegenConfigs: GraphqlCodegenConfig[] = statementTargets.map(st => {
return typeTargetConfigs.map(config => {
return { ...config, statementTarget: st } as GraphqlCodegenConfig
});
}).flat();
console.log(graphqlCodegenConfigs)
graphqlCodegenConfigs.forEach(config => {
// TODO: fix the operation source not being parsed issue and enable the tests
it.skip(`should not throw error when generating GraphQL client code in format ${config.format} with type ${config.typeTarget} and statement ${config.statementTarget}`, async () => {
await testGraphqlClientCodegen(projRoot, config);
});
})
})
});
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,24 @@ export const deleteSandbox = async (cwd: string): Promise<void> => {
/**
* Commands for ampx generate
*/
type ClientCodegenConfigBase = {
export type ClientCodegenConfigBase = {
format: string
outDir: string
}

type IntrospectionCodegenConfig = ClientCodegenConfigBase & {
export type IntrospectionCodegenConfig = ClientCodegenConfigBase & {
format: 'introspection'
}
type ModelgenConfig = ClientCodegenConfigBase & {
export type ModelgenConfig = ClientCodegenConfigBase & {
format: 'modelgen'
modelTarget: string
}
type GraphqlCodegenConfig = ClientCodegenConfigBase & {
export type GraphqlCodegenConfig = ClientCodegenConfigBase & {
format: 'graphql-codegen'
typeTarget: string
statementTarget: string
}
type ClientCodegenConfig = IntrospectionCodegenConfig | ModelgenConfig | GraphqlCodegenConfig
export type ClientCodegenConfig = IntrospectionCodegenConfig | ModelgenConfig | GraphqlCodegenConfig

const getClientCodegenParams = (props: ClientCodegenConfig): string[] => {
const params = [ '--out', props.outDir, '--format', props.format ]
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './commands';
export * from './commands';
export * from './test-graphql-client-codegen';
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import path from 'path';
import { isNotEmptyDir } from "../utils";
import { ClientCodegenConfig, generateGraphqlClientCode } from "./commands";
import { existsSync } from 'fs-extra';
import { deleteProjectDir } from '@aws-amplify/amplify-codegen-e2e-core';

export const testGraphqlClientCodegen = async (projectRoot: string, config: ClientCodegenConfig) => {
const outputPath = path.join(projectRoot, config.outDir)
if (existsSync(outputPath)) {
deleteProjectDir(outputPath);
}
await expect(generateGraphqlClientCode(projectRoot, config)).resolves.not.toThrow();

expect(isNotEmptyDir(outputPath)).toBe(true);
}

0 comments on commit e5d33db

Please sign in to comment.