cspotcode / cspotcode/tsimportlib

Can't import node-fetch in vitest

Offen
#8 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
JavaScript
Sterne
13
Forks
3
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

```
× nx run doppler:test
RUN v0.31.0 C:/Users/maxim.mazurok/transactions/packages/doppler

❯ src/lib/doppler.integration.spec.ts (1 test | 1 failed) 17ms
❯ src/lib/doppler.integration.spec.ts > should work
→ Unable to locate module "node-fetch" relative to "undefined" using the CommonJS resolver. Consider passing an absolute path to the target module.

Test Files 1 failed (1)
Tests 1 failed (1)
Start at 08:06:02
Duration 2.81s (transform 741ms, setup 404ms, collect 105ms, tests 17ms, environment 0ms, prepare 801ms)

⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯

FAIL src/lib/doppler.integration.spec.ts > should work
Error: Unable to locate module "node-fetch" relative to "undefined" using the CommonJS resolver. Consider passing an absolute path to the target module.
❯ Proxy.importEsm ../../node_modules/tsimportlib/index.js:22:15
❯ Module.setEnvironment src/lib/doppler.ts:8:46
6| export const setEnvironment = async () => {
7| // eslint-disable-next-line @typescript-eslint/consistent-type-asser…
8| const { default: fetch, Headers } = (await dynamicImport(
| ^
9| "node-fetch",
10| module,
❯ src/lib/doppler.integration.spec.ts:8:9
```

My code:
```ts
import { execSync } from "node:child_process";
import { dynamicImport } from "tsimportlib";
import { z } from "zod";

// inspired by https://docs.doppler.com/docs/sdk-javascript#async
export const setEnvironment = async () => {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const { default: fetch, Headers } = (await dynamicImport(
"node-fetch",
module,
)) as typeof import("node-fetch");

// see https://docs.doppler.com/docs/cli#fetch-cli-token-from-your-local-environment
const DOPPLER_TOKEN = execSync("doppler configure get token --plain")
.toString()
.trim();

const headers = new Headers({
Authorization: `Bearer ${DOPPLER_TOKEN}`,
});

const response = await (
await fetch(
"https://api.doppler.com/v3/configs/config/secrets/download?format=json",
{ headers },
)
).json();

const environmentVariables = z.record(z.string()).parse(response);

for (const [key, value] of Object.entries(environmentVariables)) {
process.env[key] = value;
}
};
```

My test:
```ts
import { setEnvironment } from "./doppler";

it("should work", async () => {
// Arrange
const oldEnvironmentKeys = Object.keys(process.env);

// Act
await setEnvironment();

// Assert
const newEnvironmentKeys = Object.keys(process.env);
const newKeys = newEnvironmentKeys.filter(
(x) => !oldEnvironmentKeys.includes(x),
);
expect(oldEnvironmentKeys).not.toEqual(newEnvironmentKeys);
expect(newKeys.sort()).toStrictEqual(
[
"BLA",
].sort(),
);
});
```

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Rechercherichtung

Beginne mit dem fehlschlagenden Befehl nx run doppler:test und untersuche den Aufruf dynamicImport in src/lib/doppler.ts. Lies anschließend src/lib/doppler.integration.spec.ts und reproduziere den Fehler bei der Auflösung von node-fetch. Als erledigt gilt die Aufgabe, wenn der Integrationstest node-fetch importieren und seine Assertions zu den Umgebungsvariablen abschließen kann.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
node.js, typescript
Bereich
testing-qa
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.