cspotcode / cspotcode/tsimportlib

Can't import node-fetch in vitest

Open
#8 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
13
Forks
3
PR merge metrics
No merged PRs in 30d

Description

```
× 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(),
);
});
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.