Skip to content

Commit

Permalink
fix: Use .default to access CJS module for dynamic imports (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie authored Jan 26, 2024
2 parents c00e4d9 + 7587133 commit 24eb4f4
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ env:
TEST_ROOT_DATABASE_URL: postgres://postgres:postgres@127.0.0.1:5432/postgres
TEST_DATABASE_URL: postgres://someone:something@127.0.0.1:5432/graphile_migrate_test
PGVERSION: 10
NODE_OPTIONS: "--experimental-vm-modules"

jobs:
test:
Expand Down
52 changes: 51 additions & 1 deletion __tests__/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import "./helpers"; // Side effects - must come first
import mockFs from "mock-fs";
import * as path from "path";

import { DEFAULT_GMRC_PATH, getSettings } from "../src/commands/_common";
import {
DEFAULT_GMRC_COMMONJS_PATH,
DEFAULT_GMRC_PATH,
DEFAULT_GMRCJS_PATH,
getSettings,
} from "../src/commands/_common";
import {
makeRootDatabaseConnectionString,
ParsedSettings,
Expand Down Expand Up @@ -342,3 +347,48 @@ describe("gmrc path", () => {
mockFs.restore();
});
});

describe("gmrc from JS", () => {
const nodeMajor = parseInt(process.versions.node.split(".")[0], 10);
// Only test these on Node20+
const node20PlusIt = nodeMajor >= 20 ? it : it.skip.bind(it);
node20PlusIt("supports .gmrc.js", async () => {
mockFs.restore();
// The warmups force all the parts of Node import to be exercised before we
// try and import something from our mocked filesystem.
// @ts-ignore
await Promise.all([import("./warmup.js"), import("./warmup.cjs")]);

mockFs({
[DEFAULT_GMRCJS_PATH]: /* JavaScript */ `\
module.exports = {
connectionString: "postgres://appuser:apppassword@host:5432/gmrcjs_test",
};`,
});
const settings = await getSettings();
expect(settings.connectionString).toEqual(
"postgres://appuser:apppassword@host:5432/gmrcjs_test",
);
mockFs.restore();
});

node20PlusIt("supports .gmrc.cjs", async () => {
mockFs.restore();
// The warmups force all the parts of Node import to be exercised before we
// try and import something from our mocked filesystem.
// @ts-ignore
await Promise.all([import("./warmup.js"), import("./warmup.cjs")]);

mockFs({
[DEFAULT_GMRC_COMMONJS_PATH]: /* JavaScript */ `\
module.exports = {
connectionString: "postgres://appuser:apppassword@host:5432/gmrc_commonjs_test",
};`,
});
const settings = await getSettings();
expect(settings.connectionString).toEqual(
"postgres://appuser:apppassword@host:5432/gmrc_commonjs_test",
);
mockFs.restore();
});
});
1 change: 1 addition & 0 deletions __tests__/warmup.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
1 change: 1 addition & 0 deletions __tests__/warmup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"prepack": "npm run tsc && chmod +x dist/cli.js",
"clean": "rm -Rf dist",
"test": "yarn lint && yarn run lint:deps && FORCE_COLOR=1 yarn run test:only --ci",
"test:only": "jest -i",
"test:only": "NODE_OPTIONS=\"--experimental-vm-modules\" jest -i",
"version": "yarn prepack && ./scripts/update-docs.js && node ./scripts/version.mjs && git add README.md src/version.ts",
"watch": "mkdir -p dist && touch dist/cli.js && chmod +x dist/cli.js && npm run tsc --watch"
},
Expand Down
3 changes: 2 additions & 1 deletion src/commands/_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ export async function getSettings(options: Options = {}): Promise<Settings> {
const relativePath = pathToFileURL(resolve(process.cwd(), path)).href;

try {
return (await import(relativePath)) as Settings;
const module = (await import(relativePath)) as Record<string, unknown>;
return (module.default ?? module) as Settings;
} catch (e) {
throw new Error(
`Failed to import '${relativePath}'; error:\n ${
Expand Down

0 comments on commit 24eb4f4

Please sign in to comment.