使用 jest 运行测试时有关嵌套“找不到模块”的查询

问题描述 投票:0回答:0

我猜我在下面遇到了关于嵌套版本的错误(请参考第一个错误部分)。 当我运行 test.spec.ts 时,它不工作。 所以我试图找到模块,但没有安装。 (使用 pnpm install 安装)

image1

不过,我在nest官方github上找到了。 如您所见,下面附上屏幕截图。 nest 官方 github

nest official github

有人可以帮我解决这个错误吗?

***** 第一个错误 *****

`测试套件运行失败 无法从“../../node_modules/.pnpm/@[email protected]_wdbvfsxfdwzlwdfiyh2gynjl4m/node_modules/@nestjs/testing/testing-module.builder”中找到模块“@nestjs/core/inspector/graph-inspector”。 js'

需要堆栈: /用户/-/node_modules/.pnpm/@[email protected]_wdbvfsxfdwzlwdfiyh2gynjl4m/node_modules/@nestjs/testing/testing-module.builder.js /用户/-/node_modules/.pnpm/@[email protected]_wdbvfsxfdwzlwdfiyh2gynjl4m/node_modules/@nestjs/testing/test.js /用户/-/node_modules/.pnpm/@[email protected]_wdbvfsxfdwzlwdfiyh2gynjl4m/node_modules/@nestjs/testing/index.js 测试/openai.spec.ts

3 | // eslint-disable-next-line @typescript-eslint/no-unused-vars 4 |来自“supertest”的导入请求

5 |从 '@nestjs/testing' 导入 { Test, TestingModule } | ^ 6 |从 '../src/filter/http-exception-filter' 导入 { HttpExceptionFilter } 7 |从 '../src/openai/openai.module' 导入 { OpenAIModule } 8 |

在 Resolver._throwModNotFoundError (../../node_modules/.pnpm/[email protected]/node_modules/jest-resolve/build/resolver.js:427:11) 在对象。 (../../node_modules/.pnpm/@[email protected]_wdbvfsxfdwzlwdfiyh2gynjl4m/node_modules/@nestjs/testing/testing-module.builder.js:7:27) 在对象。 (../../node_modules/.pnpm/@[email protected]_wdbvfsxfdwzlwdfiyh2gynjl4m/node_modules/@nestjs/testing/test.js:5:34) 在对象。 (../../node_modules/.pnpm/@[email protected]_wdbvfsxfdwzlwdfiyh2gynjl4m/node_modules/@nestjs/testing/index.js:11:22) 在对象。 (测试/openai.spec.ts:5:1`

***** 第二个错误 *****

`测试套件运行失败 Jest 遇到了意想不到的 token

Jest 解析文件失败。这发生在例如当您的代码或其依赖项使用非标准的 JavaScript 语法时,或者当 Jest 未配置为支持此类语法时。

开箱即用的 Jest 支持 Babel,它将根据您的 Babel 配置将您的文件转换为有效的 JS。

默认情况下,变压器会忽略“node_modules”文件夹。

以下是您可以执行的操作: • 如果您尝试使用ECMAScript 模块,请参阅https://jestjs.io/docs/ecmascript-modules 了解如何启用它。 • 如果您尝试使用 TypeScript,请参阅https://jestjs.io/docs/getting-started#using-typescript • 要转换一些“node_modules”文件,您可以在配置中指定自定义“transformIgnorePatterns”。 • 如果您需要自定义转换,请在您的配置中指定一个“转换”选项。 • 如果您只是想模拟您的非 JS 模块(例如二进制资产),您可以使用“moduleNameMapper”配置选项将它们存根。

您将在文档中找到这些配置选项的更多详细信息和示例: https://jestjs.io/docs/configuration 有关自定义转换的信息,请参阅: https://jestjs.io/docs/code-transformation

详情:

/用户/-/node_modules/.pnpm/@[email protected]_e6la6qvsclaae2becwjnmfvsuq/node_modules/@nestjs/core/inspector/graph-inspector.ts:1 ({"Object.":function(module,exports,require,__dirname,__filename,jest){import {UnknownDependenciesException} from '../errors/exceptions/unknown-dependencies.exception'; ^^^^^^^

SyntaxError: 不能在模块外使用 import 语句

3 | // eslint-disable-next-line @typescript-eslint/no-unused-vars 4 | // 从'supertest'导入请求

5 |从 '@nestjs/testing' 导入 { Test, TestingModule } | ^ 6 |从 '../src/filter/http-exception-filter' 导入 {HttpExceptionFilter} 7 |从 '../src/openai/openai.module' 导入 {OpenAIModule} 8 |

在 Runtime.createScriptFromCode (../../node_modules/.pnpm/[email protected]/node_modules/jest-runtime/build/index.js:1495:14) 在对象。 (../../node_modules/.pnpm/@[email protected]_wdbvfsxfdwzlwdfiyh2gynjl4m/node_modules/@nestjs/testing/testing-module.builder.js:7:27) 在对象。 (../../node_modules/.pnpm/@[email protected]_wdbvfsxfdwzlwdfiyh2gynjl4m/node_modules/@nestjs/testing/test.js:5:34) 在对象。 (../../node_modules/.pnpm/@[email protected]_wdbvfsxfdwzlwdfiyh2gynjl4m/node_modules/@nestjs/testing/index.js:11:22) 在对象。 (测试/openai.spec.ts:5:1)`

我之前尝试过的

  • 下载嵌套代码(zipfile),也将其添加到我项目的node_modules中。 -> 它正在运行,但存在一些依赖错误,我只是自己添加了所有包。从现在开始又发生了另一个错误(请参考第二个错误部分)。
  • 添加了 babel.js -> 不工作

我想知道为什么有些模块没有安装?是同步问题吗?

typescript jestjs nest pnpm
© www.soinside.com 2019 - 2024. All rights reserved.