Skip to content

Commit

Permalink
Improve and refactor tests (#7180)
Browse files Browse the repository at this point in the history
* Improve and refactor tests

* Fix leak
  • Loading branch information
ardatan committed Jul 1, 2024
1 parent 8b7aa23 commit 3ec7077
Show file tree
Hide file tree
Showing 28 changed files with 267 additions and 238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { createTenv } from '@e2e/tenv';
const { serve, compose } = createTenv(__dirname);

it('should write serve logs to stderr', async () => {
const { getStd, dispose } = await serve();
await dispose();
await using serveInstance = await serve();

expect(getStd('out')).toBeFalsy();
expect(getStd('err')).toContain('Starting server on');
expect(serveInstance.getStd('out')).toBeFalsy();
expect(serveInstance.getStd('err')).toContain('Starting server on');
});

it('should write compose output to stdout and logs to stderr', async () => {
Expand Down
18 changes: 7 additions & 11 deletions e2e/utils/tenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const retries = 120,
timeout = retries * interval; // 1min
jest.setTimeout(timeout);

const leftovers = new Set<Disposable | string>();
const leftovers = new Set<AsyncDisposable | string>();
afterAll(async () => {
await Promise.allSettled(
Array.from(leftovers.values()).map(leftover => {
Expand All @@ -23,7 +23,7 @@ afterAll(async () => {
return fs.rm(leftover, { recursive: true });
}
// process
return leftover.dispose();
return leftover[Symbol.asyncDispose]();
}),
).finally(() => {
leftovers.clear();
Expand All @@ -34,10 +34,6 @@ const __project = path.resolve(__dirname, '..', '..') + '/';

const docker = new Dockerode();

export interface Disposable {
dispose(): Promise<void>;
}

export interface ProcOptions {
/**
* Pipe the logs from the spawned process to the current process.
Expand All @@ -46,7 +42,7 @@ export interface ProcOptions {
pipeLogs?: boolean;
}

export interface Proc extends Disposable {
export interface Proc extends AsyncDisposable {
getStd(o: 'out' | 'err' | 'both'): string;
getStats(): Promise<{
// Total CPU utilization (of all cores) as a percentage.
Expand Down Expand Up @@ -358,7 +354,7 @@ export function createTenv(cwd: string): Tenv {
getStats() {
throw new Error('Cannot get stats of a container.');
},
async dispose() {
async [Symbol.asyncDispose]() {
if (ctrl.signal.aborted) {
// noop if already disposed
return;
Expand Down Expand Up @@ -396,13 +392,13 @@ export function createTenv(cwd: string): Tenv {
}

if (status === 'none') {
await container.dispose();
await container[Symbol.asyncDispose]();
throw new DockerError(
'Container has "none" health status, but has a healthcheck',
container,
);
} else if (status === 'unhealthy') {
await container.dispose();
await container[Symbol.asyncDispose]();
throw new DockerError('Container is unhealthy', container);
} else if (status === 'healthy') {
break;
Expand Down Expand Up @@ -476,7 +472,7 @@ function spawn(
mem: parseFloat(mem) * 0.001, // KB to MB
};
},
dispose: () => (child.kill(), waitForExit),
[Symbol.asyncDispose]: () => (child.kill(), waitForExit),
};
leftovers.add(proc);

Expand Down
21 changes: 11 additions & 10 deletions examples/hello-world-esm/tests/hello-world.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { join } from 'path';
import { findAndParseConfig } from '@graphql-mesh/cli';
import { getMesh } from '@graphql-mesh/runtime';
import { getMesh, MeshInstance } from '@graphql-mesh/runtime';
import { printSchemaWithDirectives } from '@graphql-tools/utils';

const mesh$ = findAndParseConfig({
dir: join(__dirname, '..'),
}).then(config => getMesh(config));

describe('Hello World', () => {
let mesh: MeshInstance;
beforeAll(async () => {
const config = await findAndParseConfig({
dir: join(__dirname, '..'),
});
mesh = await getMesh(config);
});
it('should generate correct schema', async () => {
const { schema } = await mesh$;
expect(printSchemaWithDirectives(schema)).toMatchSnapshot();
expect(printSchemaWithDirectives(mesh.schema)).toMatchSnapshot();
});
it('should give correct response', async () => {
const { execute } = await mesh$;
const result = await execute(
const result = await mesh.execute(
/* GraphQL */ `
query HelloWorld {
greeting
Expand All @@ -25,5 +26,5 @@ describe('Hello World', () => {
expect(result?.errors).toBeFalsy();
expect(result).toMatchSnapshot();
});
afterAll(() => mesh$.then(mesh => mesh.destroy()));
afterAll(() => mesh?.destroy());
});
21 changes: 11 additions & 10 deletions examples/hello-world/tests/hello-world.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { join } from 'path';
import { findAndParseConfig } from '@graphql-mesh/cli';
import { getMesh } from '@graphql-mesh/runtime';
import { getMesh, MeshInstance } from '@graphql-mesh/runtime';
import { printSchemaWithDirectives } from '@graphql-tools/utils';

const mesh$ = findAndParseConfig({
dir: join(__dirname, '..'),
}).then(config => getMesh(config));

describe('Hello World', () => {
let mesh: MeshInstance;
beforeAll(async () => {
const config = await findAndParseConfig({
dir: join(__dirname, '..'),
});
mesh = await getMesh(config);
});
it('should generate correct schema', async () => {
const { schema } = await mesh$;
expect(printSchemaWithDirectives(schema)).toMatchSnapshot();
expect(printSchemaWithDirectives(mesh.schema)).toMatchSnapshot();
});
it('should give correct response', async () => {
const { execute } = await mesh$;
const result = await execute(
const result = await mesh.execute(
/* GraphQL */ `
query HelloWorld {
greeting
Expand All @@ -25,5 +26,5 @@ describe('Hello World', () => {
expect(result?.errors).toBeFalsy();
expect(result).toMatchSnapshot();
});
afterAll(() => mesh$.then(mesh => mesh.destroy()));
afterAll(() => mesh?.destroy());
});
34 changes: 15 additions & 19 deletions examples/json-schema-covid/tests/json-schema-covid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import { join } from 'path';
import { readFile } from 'fs-extra';
import { lexicographicSortSchema, printSchema } from 'graphql';
import { findAndParseConfig } from '@graphql-mesh/cli';
import { getMesh } from '@graphql-mesh/runtime';
import { getMesh, MeshInstance } from '@graphql-mesh/runtime';

const config$ = findAndParseConfig({
dir: join(__dirname, '..'),
});

const mesh$ = config$.then(config => getMesh(config));
jest.setTimeout(30000);

describe('JSON Schema Covid', () => {
let mesh: MeshInstance;
beforeAll(async () => {
const config = await findAndParseConfig({
dir: join(__dirname, '..'),
});
mesh = await getMesh(config);
});
it('should generate correct schema', async () => {
const { schema } = await mesh$;
expect(printSchema(lexicographicSortSchema(schema))).toMatchSnapshot(
expect(printSchema(lexicographicSortSchema(mesh.schema))).toMatchSnapshot(
'json-schema-covid-schema',
);
});
Expand All @@ -23,8 +24,7 @@ describe('JSON Schema Covid', () => {
join(__dirname, '../example-queries/getData_step1.graphql'),
'utf8',
);
const { execute } = await mesh$;
const result = await execute(getDataStep1Query, undefined);
const result = await mesh.execute(getDataStep1Query, undefined);
expect(result.errors).toBeFalsy();
// Check exposed response metadata
expect(result.data?.population?._response).toBeTruthy();
Expand All @@ -45,8 +45,7 @@ describe('JSON Schema Covid', () => {
join(__dirname, '../example-queries/getData_step1.graphql'),
'utf8',
);
const { execute } = await mesh$;
const result = await execute(getDataStep1Query, undefined);
const result = await mesh.execute(getDataStep1Query, undefined);
expect(result.errors?.length).toBeFalsy();
expect(typeof result?.data?.case?.confirmed).toBe('number');
expect(result?.data?.case?.countryRegion).toBe('France');
Expand All @@ -61,8 +60,7 @@ describe('JSON Schema Covid', () => {
join(__dirname, '../example-queries/getData_step2.graphql'),
'utf8',
);
const { execute } = await mesh$;
const result = await execute(getDataStep2Query, undefined);
const result = await mesh.execute(getDataStep2Query, undefined);
expect(result.errors).toBeFalsy();
expect(typeof result?.data?.case?.confirmed).toBe('number');
expect(typeof result?.data?.case?.deaths).toBe('number');
Expand All @@ -75,8 +73,7 @@ describe('JSON Schema Covid', () => {
join(__dirname, '../example-queries/getData_step3_1.graphql'),
'utf8',
);
const { execute } = await mesh$;
const result = await execute(getDataStep3_1Query, undefined);
const result = await mesh.execute(getDataStep3_1Query, undefined);
expect(result.errors).toBeFalsy();
expect(typeof result?.data?.fr?.deathRatio).toBe('number');

Expand All @@ -87,8 +84,7 @@ describe('JSON Schema Covid', () => {
join(__dirname, '../example-queries/getData_step3_2.graphql'),
'utf8',
);
const { execute } = await mesh$;
const result = await execute(getDataStep3_2Query, undefined);
const result = await mesh.execute(getDataStep3_2Query, undefined);
expect(result.errors).toBeFalsy();
expect(typeof result?.data?.fr?.deathRatio).toBe('number');
expect(typeof result?.data?.fr?.case?.deaths).toBe('number');
Expand All @@ -100,5 +96,5 @@ describe('JSON Schema Covid', () => {
expect(result?.data?.at?.population?.records?.length).toBe(1);
expect(typeof result?.data?.at?.population?.records[0]?.fields?.value).toBe('number');
});
afterAll(() => mesh$.then(mesh => mesh.destroy()));
afterAll(() => mesh?.destroy());
});
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
const { findAndParseConfig } = require('@graphql-mesh/cli');
const { getMesh } = require('@graphql-mesh/runtime');
const { readFile } = require('fs-extra');
const { join } = require('path');

const { printSchemaWithDirectives } = require('@graphql-tools/utils');

const mesh$ = findAndParseConfig({
dir: join(__dirname, '..'),
}).then(config => getMesh(config));
import { promises as fsPromises } from 'fs';
import { join } from 'path';
import { findAndParseConfig } from '@graphql-mesh/cli';
import { getMesh, MeshInstance } from '@graphql-mesh/runtime';
import { printSchemaWithDirectives } from '@graphql-tools/utils';

describe('JSON Schema Example', () => {
let mesh: MeshInstance;
beforeAll(async () => {
const config = await findAndParseConfig({
dir: join(__dirname, '..'),
});
mesh = await getMesh(config);
});
it('should generate correct schema', async () => {
const { schema } = await mesh$;
expect(printSchemaWithDirectives(schema)).toMatchSnapshot();
expect(printSchemaWithDirectives(mesh.schema)).toMatchSnapshot();
});
it('should give correct response', async () => {
const { execute } = await mesh$;
const query = await readFile(join(__dirname, '../example-query.graphql'), 'utf8');
const result = await execute(query);
const query = await fsPromises.readFile(join(__dirname, '../example-query.graphql'), 'utf8');
const result = await mesh.execute(query, {});
expect(result?.data?.me?.firstName).toBeDefined();
expect(result?.data?.me?.jobTitle).toBeDefined();
expect(result?.data?.me?.lastName).toBeDefined();
Expand All @@ -28,5 +28,5 @@ describe('JSON Schema Example', () => {
expect(result?.data?.me?.company?.employers[0]?.jobTitle).toBeDefined();
expect(result?.data?.me?.company?.employers[0]?.lastName).toBeDefined();
});
afterAll(() => mesh$.then(mesh => mesh.destroy()));
afterAll(() => mesh?.destroy());
});
18 changes: 0 additions & 18 deletions examples/json-schema-fhir/tests/fhir.test.js

This file was deleted.

19 changes: 19 additions & 0 deletions examples/json-schema-fhir/tests/fhir.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { join } from 'path';
import { findAndParseConfig } from '@graphql-mesh/cli';
import { getMesh, MeshInstance } from '@graphql-mesh/runtime';
import { printSchemaWithDirectives } from '@graphql-tools/utils';

jest.setTimeout(15000);

describe('FHIR', () => {
let mesh: MeshInstance;
beforeAll(async () => {
const config = await findAndParseConfig({
dir: join(__dirname, '..'),
});
mesh = await getMesh(config);
});
it('should generate correct schema', async () => {
expect(printSchemaWithDirectives(mesh.schema)).toMatchSnapshot();
});
});
26 changes: 14 additions & 12 deletions examples/odata-trippin/tests/odata-trippin.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
import { basename, join } from 'path';
import { introspectionFromSchema, lexicographicSortSchema } from 'graphql';
import { findAndParseConfig } from '@graphql-mesh/cli';
import { getMesh } from '@graphql-mesh/runtime';
import { getMesh, MeshInstance } from '@graphql-mesh/runtime';
import { ProcessedConfig } from '../../../packages/legacy/config/dist/typings/process';

const config$ = findAndParseConfig({
dir: join(__dirname, '..'),
});
const mesh$ = config$.then(config => getMesh(config));
jest.setTimeout(15000);

describe('OData TripPin', () => {
let config: ProcessedConfig;
let mesh: MeshInstance;
beforeAll(async () => {
config = await findAndParseConfig({
dir: join(__dirname, '..'),
});
mesh = await getMesh(config);
});
it('should generate correct schema', async () => {
const { schema } = await mesh$;
expect(
introspectionFromSchema(lexicographicSortSchema(schema), {
introspectionFromSchema(lexicographicSortSchema(mesh.schema), {
descriptions: false,
}),
).toMatchSnapshot('odata-trippin-schema');
});
it('should give correct response for example queries', async () => {
const { documents } = await config$;
const { execute } = await mesh$;
for (const source of documents) {
for (const source of config.documents) {
if (!source.document || !source.location) {
continue;
}
const result = await execute(source.document, {});
const result = await mesh.execute(source.document, {});
expect(result).toMatchSnapshot(basename(source.location) + '-query-result');
}
});
afterAll(() => mesh$.then(mesh => mesh.destroy()));
afterAll(() => mesh?.destroy());
});
Loading

0 comments on commit 3ec7077

Please sign in to comment.