cspotcode / cspotcode/tsimportlib

Can't import node-fetch in vitest

未关闭
#8 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
JavaScript
星标
13
派生
3
PR 合并指标
30 天内没有已合并 PR

描述

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

贡献指南

这个仓库没有索引到贡献指南

调研方向

从失败的 nx run doppler:test 命令开始,检查 src/lib/doppler.ts 中的 dynamicImport 调用。然后阅读 src/lib/doppler.integration.spec.ts,并重现 node-fetch 解析失败。集成测试能够导入 node-fetch 并完成其环境变量断言,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
node.js, typescript
领域
testing-qa
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
30/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。