Skip to content

Commit

Permalink
Remove inline require() and enable @ts-check in tests (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie authored Nov 23, 2024
1 parent 9abc924 commit 0b37cb7
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 20 deletions.
12 changes: 9 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ module.exports = {
rules: {
"jest/expect-expect": ["off"],
"@typescript-eslint/no-namespace": ["off"],

// Rules to disable in V5 port
"@typescript-eslint/no-var-requires": ["off"],
},
overrides: [
{
files: "__tests__/**/*.js",
rules: {
// Rules to disable in V5 port
"@typescript-eslint/no-var-requires": ["off"],
},
},
],
};
21 changes: 11 additions & 10 deletions __tests__/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check
const pg = require("pg");
const { readFile } = require("fs");
const pgConnectionString = require("pg-connection-string");

// This test suite can be flaky. Increase it’s timeout.
jest.setTimeout(1000 * 20);
Expand All @@ -19,7 +19,7 @@ const withPgClient = async (url, fn) => {
fn = url;
url = process.env.TEST_DATABASE_URL;
}
const pgPool = new pg.Pool(pgConnectionString.parse(url));
const pgPool = new pg.Pool({ connectionString: url });
let client;
try {
client = await pgPool.connect();
Expand Down Expand Up @@ -51,7 +51,8 @@ const withDbFromUrl = async (url, fn) => {

const withRootDb = (fn) => withDbFromUrl(process.env.TEST_DATABASE_URL, fn);

let prepopulatedDBKeepalive;
/** @type {(Promise<void> & {resolve: () => void, reject: () => void, client: import('pg').PoolClient, vars: any}) | null} */
let prepopulatedDBKeepalive = null;

const populateDatabase = async (client) => {
await client.query(await readFilePromise(`${__dirname}/p-data.sql`, "utf8"));
Expand Down Expand Up @@ -89,14 +90,14 @@ withPrepopulatedDb.setup = (done) => {
}
let res;
let rej;
prepopulatedDBKeepalive = new Promise((resolve, reject) => {
res = resolve;
rej = reject;
});
prepopulatedDBKeepalive.resolve = res;
prepopulatedDBKeepalive.reject = rej;
withRootDb(async (client) => {
prepopulatedDBKeepalive.client = client;
prepopulatedDBKeepalive = Object.assign(
new Promise((resolve, reject) => {
res = resolve;
rej = reject;
}),
{ resolve: res, reject: rej, client, vars: undefined }
);
try {
prepopulatedDBKeepalive.vars = await populateDatabase(client);
} catch (e) {
Expand Down
6 changes: 4 additions & 2 deletions __tests__/integration/queries.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// @ts-check
const { graphql } = require("graphql");
const { withPgClient } = require("../helpers");
const { createPostGraphileSchema } = require("postgraphile-core");
const { readdirSync, readFile: rawReadFile } = require("fs");
const { resolve: resolvePath } = require("path");
const { printSchema } = require("graphql/utilities");
const debug = require("debug")("graphile-build:schema");
const { default: PgOrderByRelatedPlugin } = require("../../dist/index.js");

function readFile(filename, encoding) {
return new Promise((resolve, reject) => {
Expand All @@ -29,10 +31,10 @@ beforeAll(() => {
// need and wait for them to be created in parallel.
const [normal, columnAggregates] = await Promise.all([
createPostGraphileSchema(pgClient, ["p"], {
appendPlugins: [require("../../dist/index.js")],
appendPlugins: [PgOrderByRelatedPlugin],
}),
createPostGraphileSchema(pgClient, ["p"], {
appendPlugins: [require("../../dist/index.js")],
appendPlugins: [PgOrderByRelatedPlugin],
graphileBuildOptions: {
orderByRelatedColumnAggregates: true,
},
Expand Down
4 changes: 3 additions & 1 deletion __tests__/integration/schema/columnAggregates.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// @ts-check
const core = require("./core");
const { default: PgOrderByRelatedPlugin } = require("../../../dist/index.js");

test(
"prints a schema with the order-by-related plugin",
core.test(["p"], {
appendPlugins: [require("../../../dist/index.js")],
appendPlugins: [PgOrderByRelatedPlugin],
disableDefaultMutations: true,
legacyRelations: "omit",
graphileBuildOptions: {
Expand Down
1 change: 1 addition & 0 deletions __tests__/integration/schema/core.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
const { withPgClient } = require("../../helpers");
const { createPostGraphileSchema } = require("postgraphile-core");
const { parse, buildASTSchema } = require("graphql");
Expand Down
4 changes: 3 additions & 1 deletion __tests__/integration/schema/ignoreIndexes.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// @ts-check
const core = require("./core");
const { default: PgOrderByRelatedPlugin } = require("../../../dist/index.js");

test(
"prints a schema with `ignoreIndexes: false`",
core.test(["p"], {
appendPlugins: [require("../../../dist/index.js")],
appendPlugins: [PgOrderByRelatedPlugin],
disableDefaultMutations: true,
legacyRelations: "omit",
ignoreIndexes: false,
Expand Down
4 changes: 3 additions & 1 deletion __tests__/integration/schema/orderByRelated.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// @ts-check
const core = require("./core");
const { default: PgOrderByRelatedPlugin } = require("../../../dist/index.js");

test(
"prints a schema with the order-by-related plugin",
core.test(["p"], {
appendPlugins: [require("../../../dist/index.js")],
appendPlugins: [PgOrderByRelatedPlugin],
disableDefaultMutations: true,
legacyRelations: "omit",
})
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { Inflection, Plugin } from "graphile-build";
import * as pkg from "../package.json";

const PgOrderByRelatedPlugin: Plugin = (
builder,
{ orderByRelatedColumnAggregates }
) => {
builder.hook("build", (build) => {
const pkg = require("../package.json");

// Check dependencies
if (!build.versions) {
throw new Error(
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@tsconfig/node18/tsconfig.json",
"compilerOptions": {
"noImplicitAny": false,
"resolveJsonModule": true,
"rootDir": "./src",
"outDir": "./dist",
"declarationDir": "dist",
Expand Down

0 comments on commit 0b37cb7

Please sign in to comment.