' rxjs'在运行开玩笑测试套件时找不到模块

发布于 2025-02-05 03:22:52 字数 1546 浏览 3 评论 0原文

我有一个Nest JS应用程序,我正在为某些提供商类编写测试。 beforeall fn。在我的 *.spec.ts文件中定义的开玩笑的运行良好,但开玩笑()fn。失败带有以下错误:


    Cannot find module 'rxjs' from 'node_modules/@nestjs/common/cache/interceptors/cache.interceptor.js'

    Require stack:
      node_modules/@nestjs/common/cache/interceptors/cache.interceptor.js
      node_modules/@nestjs/common/cache/interceptors/index.js
      node_modules/@nestjs/common/cache/index.js
      node_modules/@nestjs/common/index.js
      node_modules/@nestjs/testing/testing-module.builder.js
      node_modules/@nestjs/testing/test.js
      node_modules/@nestjs/testing/index.js
      src/bookmark/test/integration/bkm.service.int-spec.ts

      at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:491:11)
      at Object.<anonymous> (node_modules/@nestjs/common/cache/interceptors/cache.interceptor.js:5:16) 

我的package.json在“依赖关系”部分中定义了带有 ^7.2.0版本的RXJ。

我在 *.spec.ts测试类中的测试看起来像这样:

describe('BookmarkService Integration', () => {
    let prisma: PrismaService;
    let bookmarkService: BookmarkService
    beforeAll(async () => {
        const moduleRef = await Test.createTestingModule({
          imports: [AppModule],
        }).compile();
    
        prisma = moduleRef.get(PrismaService);
        bookmarkService = moduleRef.get(BookmarkService);
        await prisma.cleanDatabase();
      });
    it.todo('Default test pass')
})

我没有尝试明确执行RXJ的NPM安装,因为当我初始化嵌套时,当它已经作为默认的依赖性时,我不理解安装它的后果应用。任何帮助将不胜感激。

谢谢

I have a Nest JS application for which I am writing tests for some of the Provider classes. The beforeAll fn. of jest defined in my *.spec.ts file runs fine but the jest todo() fn. fails with the following error:


    Cannot find module 'rxjs' from 'node_modules/@nestjs/common/cache/interceptors/cache.interceptor.js'

    Require stack:
      node_modules/@nestjs/common/cache/interceptors/cache.interceptor.js
      node_modules/@nestjs/common/cache/interceptors/index.js
      node_modules/@nestjs/common/cache/index.js
      node_modules/@nestjs/common/index.js
      node_modules/@nestjs/testing/testing-module.builder.js
      node_modules/@nestjs/testing/test.js
      node_modules/@nestjs/testing/index.js
      src/bookmark/test/integration/bkm.service.int-spec.ts

      at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:491:11)
      at Object.<anonymous> (node_modules/@nestjs/common/cache/interceptors/cache.interceptor.js:5:16) 

My package.json has rxjs defined in the "dependencies" section with a version of ^7.2.0.

My test in the *.spec.ts testing class looks like this:

describe('BookmarkService Integration', () => {
    let prisma: PrismaService;
    let bookmarkService: BookmarkService
    beforeAll(async () => {
        const moduleRef = await Test.createTestingModule({
          imports: [AppModule],
        }).compile();
    
        prisma = moduleRef.get(PrismaService);
        bookmarkService = moduleRef.get(BookmarkService);
        await prisma.cleanDatabase();
      });
    it.todo('Default test pass')
})

I have not tried explicitly doing a npm install of rxjs since I don't understand the consequence of installing it when it already came as a default dependency when I initialized my Nest application. Any help would be greatly appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜无邪 2025-02-12 03:22:52

找不到模块是一个常见的错误,当您的项目代码请求或导入的模块之一由一个导入的模块所请求的依赖项无法找到node_module目录中的目标模块。

这个问题可能是由多种情况引起的,但是根据您对我的评论的回应,这可能是由于npm已缓存package> package> package-lock.json 属性以及重新启动npm安装仍然会将错误的模块添加到node_modules Directory。因此,分辨率node_modules目录的简单删除,package> package> package-lock.json文件和npm install的重读命令。

要回答您关于这仍然是一个问题的问题,即使package-lock.json确实包含rxjs库参考,我可能会有很多我会这样必须推测;一方面,您正在检查的内容可能是 transive rxjs依赖项,而不是另一个库使用,而不是rxjs库实际上是在错误中要求的。另外,即使rxjspackage> package-lock.json文件中定义,但实际上可能没有在node_modules 目录可能无法安装。这实际上取决于您在看到问题之前所做的事情的确切顺序,但我希望我的解释阐明了您面临的问题。

Cannot find module is a common error that is thrown when a dependency requested by your project code or by one of the imported modules cannot find the target module inside the node_module directory.

This issue can be caused by a multitude of scenarios, but going by your response to my comment, it was likely caused by the fact that npm had cached the package-lock.json properties and upon rerunning npm install would still add incorrect modules to the node_modules directory. Hence the resolution is a simple deletion of the node_modules directory, the package-lock.json file and the rerunning of the npm install command.

To answer your question on how this was still a problem even when package-lock.json did contain the rxjs library reference, there could have been a number of scenarios that I would have to speculate; for one, what you were inspecting could have been a transitive rxjs dependency which is being used by another library and is not the rxjs library that is actually being requested for in the error. Alternatively, there is a chance that even though rxjs was defined in the package-lock.json file, it may not have actually been present in the node_modules directory as it may have failed to install. This really depends on the exact sequence of things that you have done prior to seeing the issue, but I hope my explanation clarifies the problem you were facing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文