Skip to content

Commit

Permalink
test(cmd-api-server): fix package.json needs an import assertion - json
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
petermetz committed Jan 30, 2024
1 parent db3fe87 commit 3f36d58
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from "path";
import test, { Test } from "tape-promise/tape";
import { v4 as uuidv4 } from "uuid";
import { readFile } from "fs/promises";
import { LogLevelDesc } from "@hyperledger/cactus-common";
import {
PluginImportAction,
Expand Down Expand Up @@ -68,7 +69,10 @@ test("can install plugins at runtime with specified version based on imports", a
`${apiServerOptions.plugins[0].packageName}`,
"package.json",
);
const { name, version } = await import(packageFilePath);

const pkgJsonStr = await readFile(packageFilePath, "utf-8");
const { name, version } = JSON.parse(pkgJsonStr);

t.comment(name);
t.comment(version);
t.strictEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from "../../../main/typescript/public-api";
import lmify from "lmify";
import fs from "fs-extra";
import { readFile } from "fs/promises";

const logLevel: LogLevelDesc = "TRACE";

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

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

t2.strictEquals(
version,
Expand Down Expand Up @@ -219,7 +221,8 @@ test("can instantiate plugins at runtime without install them", async (t: Test)
"package.json",
);

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

t2.strictEquals(
version,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import path from "path";
import test, { Test } from "tape-promise/tape";
import { v4 as uuidv4 } from "uuid";
import { readFile } from "fs/promises";

import { LogLevelDesc } from "@hyperledger/cactus-common";
import {
PluginImportAction,
Expand Down Expand Up @@ -67,7 +69,9 @@ test("can install plugins at runtime with specified version based on imports", a
`${apiServerOptions.plugins[0].packageName}`,
"package.json",
);
const { version } = await import(packageFilePath);
const pkgJsonStr = await readFile(packageFilePath, "utf-8");
const { version } = JSON.parse(pkgJsonStr);

t.strictEquals(
version,
apiServerOptions.plugins[0].options.version,
Expand Down

0 comments on commit 3f36d58

Please sign in to comment.