cspotcode / cspotcode/tsimportlib

Can't import node-fetch in vitest

Đang mở
#8 3 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
JavaScript
Star
13
Fork
3
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Hướng nghiên cứu

Bắt đầu với lệnh bị lỗi nx run doppler:test và kiểm tra lời gọi dynamicImport trong src/lib/doppler.ts. Sau đó đọc src/lib/doppler.integration.spec.ts và tái hiện lỗi phân giải node-fetch. Hoàn tất khi bài kiểm thử tích hợp có thể import node-fetch và hoàn thành các assertion về biến môi trường.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
node.js, typescript
Lĩnh vực
testing-qa
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
30/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.