Skip to content

Commit 0b37cb7

Browse files
authored
Remove inline require() and enable @ts-check in tests (#55)
1 parent 9abc924 commit 0b37cb7

File tree

9 files changed

+37
-20
lines changed

9 files changed

+37
-20
lines changed

.eslintrc.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ module.exports = {
1616
rules: {
1717
"jest/expect-expect": ["off"],
1818
"@typescript-eslint/no-namespace": ["off"],
19-
20-
// Rules to disable in V5 port
21-
"@typescript-eslint/no-var-requires": ["off"],
2219
},
20+
overrides: [
21+
{
22+
files: "__tests__/**/*.js",
23+
rules: {
24+
// Rules to disable in V5 port
25+
"@typescript-eslint/no-var-requires": ["off"],
26+
},
27+
},
28+
],
2329
};

__tests__/helpers.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
// @ts-check
12
const pg = require("pg");
23
const { readFile } = require("fs");
3-
const pgConnectionString = require("pg-connection-string");
44

55
// This test suite can be flaky. Increase it’s timeout.
66
jest.setTimeout(1000 * 20);
@@ -19,7 +19,7 @@ const withPgClient = async (url, fn) => {
1919
fn = url;
2020
url = process.env.TEST_DATABASE_URL;
2121
}
22-
const pgPool = new pg.Pool(pgConnectionString.parse(url));
22+
const pgPool = new pg.Pool({ connectionString: url });
2323
let client;
2424
try {
2525
client = await pgPool.connect();
@@ -51,7 +51,8 @@ const withDbFromUrl = async (url, fn) => {
5151

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

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

5657
const populateDatabase = async (client) => {
5758
await client.query(await readFilePromise(`${__dirname}/p-data.sql`, "utf8"));
@@ -89,14 +90,14 @@ withPrepopulatedDb.setup = (done) => {
8990
}
9091
let res;
9192
let rej;
92-
prepopulatedDBKeepalive = new Promise((resolve, reject) => {
93-
res = resolve;
94-
rej = reject;
95-
});
96-
prepopulatedDBKeepalive.resolve = res;
97-
prepopulatedDBKeepalive.reject = rej;
9893
withRootDb(async (client) => {
99-
prepopulatedDBKeepalive.client = client;
94+
prepopulatedDBKeepalive = Object.assign(
95+
new Promise((resolve, reject) => {
96+
res = resolve;
97+
rej = reject;
98+
}),
99+
{ resolve: res, reject: rej, client, vars: undefined }
100+
);
100101
try {
101102
prepopulatedDBKeepalive.vars = await populateDatabase(client);
102103
} catch (e) {

__tests__/integration/queries.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
// @ts-check
12
const { graphql } = require("graphql");
23
const { withPgClient } = require("../helpers");
34
const { createPostGraphileSchema } = require("postgraphile-core");
45
const { readdirSync, readFile: rawReadFile } = require("fs");
56
const { resolve: resolvePath } = require("path");
67
const { printSchema } = require("graphql/utilities");
78
const debug = require("debug")("graphile-build:schema");
9+
const { default: PgOrderByRelatedPlugin } = require("../../dist/index.js");
810

911
function readFile(filename, encoding) {
1012
return new Promise((resolve, reject) => {
@@ -29,10 +31,10 @@ beforeAll(() => {
2931
// need and wait for them to be created in parallel.
3032
const [normal, columnAggregates] = await Promise.all([
3133
createPostGraphileSchema(pgClient, ["p"], {
32-
appendPlugins: [require("../../dist/index.js")],
34+
appendPlugins: [PgOrderByRelatedPlugin],
3335
}),
3436
createPostGraphileSchema(pgClient, ["p"], {
35-
appendPlugins: [require("../../dist/index.js")],
37+
appendPlugins: [PgOrderByRelatedPlugin],
3638
graphileBuildOptions: {
3739
orderByRelatedColumnAggregates: true,
3840
},

__tests__/integration/schema/columnAggregates.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// @ts-check
12
const core = require("./core");
3+
const { default: PgOrderByRelatedPlugin } = require("../../../dist/index.js");
24

35
test(
46
"prints a schema with the order-by-related plugin",
57
core.test(["p"], {
6-
appendPlugins: [require("../../../dist/index.js")],
8+
appendPlugins: [PgOrderByRelatedPlugin],
79
disableDefaultMutations: true,
810
legacyRelations: "omit",
911
graphileBuildOptions: {

__tests__/integration/schema/core.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-check
12
const { withPgClient } = require("../../helpers");
23
const { createPostGraphileSchema } = require("postgraphile-core");
34
const { parse, buildASTSchema } = require("graphql");

__tests__/integration/schema/ignoreIndexes.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// @ts-check
12
const core = require("./core");
3+
const { default: PgOrderByRelatedPlugin } = require("../../../dist/index.js");
24

35
test(
46
"prints a schema with `ignoreIndexes: false`",
57
core.test(["p"], {
6-
appendPlugins: [require("../../../dist/index.js")],
8+
appendPlugins: [PgOrderByRelatedPlugin],
79
disableDefaultMutations: true,
810
legacyRelations: "omit",
911
ignoreIndexes: false,

__tests__/integration/schema/orderByRelated.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// @ts-check
12
const core = require("./core");
3+
const { default: PgOrderByRelatedPlugin } = require("../../../dist/index.js");
24

35
test(
46
"prints a schema with the order-by-related plugin",
57
core.test(["p"], {
6-
appendPlugins: [require("../../../dist/index.js")],
8+
appendPlugins: [PgOrderByRelatedPlugin],
79
disableDefaultMutations: true,
810
legacyRelations: "omit",
911
})

src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import type { Inflection, Plugin } from "graphile-build";
2+
import * as pkg from "../package.json";
23

34
const PgOrderByRelatedPlugin: Plugin = (
45
builder,
56
{ orderByRelatedColumnAggregates }
67
) => {
78
builder.hook("build", (build) => {
8-
const pkg = require("../package.json");
9-
109
// Check dependencies
1110
if (!build.versions) {
1211
throw new Error(

tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
2+
"$schema": "https://json.schemastore.org/tsconfig",
23
"extends": "@tsconfig/node18/tsconfig.json",
34
"compilerOptions": {
45
"noImplicitAny": false,
6+
"resolveJsonModule": true,
57
"rootDir": "./src",
68
"outDir": "./dist",
79
"declarationDir": "dist",

0 commit comments

Comments
 (0)