Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 3f36d58

Browse files
author
Peter Somogyvari
committed
test(cmd-api-server): fix package.json needs an import assertion - json
Refactored the test cases so that they use the build-in file system libraries of NodeJS to read the file contents of the dynamically pulled in package.json The dynamic import with import type assertion cannot work because we do not yet have the migration to ESM completed and therefore it won't compile if you try to do it like it's supposed to be: ```typescript const { default: data } = await import("./foo.json", { assert: { type: "json" } }); ``` When running the test cases in question it would produce the failure pasted below: ```sh not ok 3 TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module "file:///home/runner/work/cacti/cacti/.tmp/test/test-cmd-api-server/ plugin-import-from-github_test/5c711023-7573-4272-aee9-c743f5346ce7/ fad4ca80-c93a-4611-9a68-207c9ad2085e/node_modules/@hyperledger/ cactus-dummy-package/package.json" needs an import assertion of type "json" --- operator: error at: bound call (/home/runner/work/cacti/cacti/node_modules/ tape-promise/node_modules/onetime/index.js:30:12) stack: |- TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module "file:///home/runner/work/cacti/cacti/.tmp/test/test-cmd-api-server/ plugin-import-from-github_test/5c711023-7573-4272-aee9-c743f5346ce7/ fad4ca80-c93a-4611-9a68-207c9ad2085e/node_modules/@hyperledger/ cactus-dummy-package/package.json" needs an import assertion of type "json" at new NodeError (node:internal/errors:405:5) at validateAssertions (node:internal/modules/esm/assert:95:15) at defaultLoad (node:internal/modules/esm/load:91:3) at nextLoad (node:internal/modules/esm/loader:163:28) at ESMLoader.load (node:internal/modules/esm/loader:603:26) at ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:22) at new ModuleJob (node:internal/modules/esm/module_job:64:26) at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:480:17) at ESMLoader.getModuleJob (node:internal/modules/esm/loader:434:34) at processTicksAndRejections (node:internal/process/task_queues:95:5) ... ``` Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
1 parent db3fe87 commit 3f36d58

3 files changed

Lines changed: 15 additions & 4 deletions

File tree

packages/cactus-cmd-api-server/src/test/typescript/integration/plugin-import-from-github.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from "path";
22
import test, { Test } from "tape-promise/tape";
33
import { v4 as uuidv4 } from "uuid";
4+
import { readFile } from "fs/promises";
45
import { LogLevelDesc } from "@hyperledger/cactus-common";
56
import {
67
PluginImportAction,
@@ -68,7 +69,10 @@ test("can install plugins at runtime with specified version based on imports", a
6869
`${apiServerOptions.plugins[0].packageName}`,
6970
"package.json",
7071
);
71-
const { name, version } = await import(packageFilePath);
72+
73+
const pkgJsonStr = await readFile(packageFilePath, "utf-8");
74+
const { name, version } = JSON.parse(pkgJsonStr);
75+
7276
t.comment(name);
7377
t.comment(version);
7478
t.strictEquals(

packages/cactus-cmd-api-server/src/test/typescript/integration/plugin-import-without-install.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from "../../../main/typescript/public-api";
1414
import lmify from "lmify";
1515
import fs from "fs-extra";
16+
import { readFile } from "fs/promises";
1617

1718
const logLevel: LogLevelDesc = "TRACE";
1819

@@ -143,7 +144,8 @@ test("can instantiate plugins at runtime without install them", async (t: Test)
143144
"package.json",
144145
);
145146

146-
const { version } = await import(packageFilePath);
147+
const pkgJsonStr = await readFile(packageFilePath, "utf-8");
148+
const { version } = JSON.parse(pkgJsonStr);
147149

148150
t2.strictEquals(
149151
version,
@@ -219,7 +221,8 @@ test("can instantiate plugins at runtime without install them", async (t: Test)
219221
"package.json",
220222
);
221223

222-
const { version } = await import(packageFilePath);
224+
const pkgJsonStr = await readFile(packageFilePath, "utf-8");
225+
const { version } = JSON.parse(pkgJsonStr);
223226

224227
t2.strictEquals(
225228
version,

packages/cactus-test-cmd-api-server/src/test/typescript/integration/plugin-import-with-npm-install-version-selection.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import path from "path";
22
import test, { Test } from "tape-promise/tape";
33
import { v4 as uuidv4 } from "uuid";
4+
import { readFile } from "fs/promises";
5+
46
import { LogLevelDesc } from "@hyperledger/cactus-common";
57
import {
68
PluginImportAction,
@@ -67,7 +69,9 @@ test("can install plugins at runtime with specified version based on imports", a
6769
`${apiServerOptions.plugins[0].packageName}`,
6870
"package.json",
6971
);
70-
const { version } = await import(packageFilePath);
72+
const pkgJsonStr = await readFile(packageFilePath, "utf-8");
73+
const { version } = JSON.parse(pkgJsonStr);
74+
7175
t.strictEquals(
7276
version,
7377
apiServerOptions.plugins[0].options.version,

0 commit comments

Comments
 (0)