javascript - 开 Jest 配置错误 : "Preset @shelf/jest-mongodb not found."

标签 javascript node.js mongodb testing jestjs

我有一个使用 jest 的项目,我可以用 npm test 运行 jest如果我不设置预设,它就可以工作。
我需要预设@shelf/jest-mongodb ,我得到了这篇文章标题中的错误。
这是我的 jest.config.js:

const path = require('path')

module.exports = {
    preset: '@shelf/jest-mongodb',
    rootDir: path.resolve(__dirname),
    testMatch: ['<rootDir>\\test\\unit\\specs\\**.js'],
    moduleFileExtensions: [
        'js',
        'json',
        'vue'
    ],
    moduleNameMapper: {
        '^@/(.*)$': '<rootDir>/src/$1'
    },
    transform: {
        '^.+\\.js$': '<rootDir>/node_modules/babel-jest',
        '.*\\.(vue)$': '<rootDir>/node_modules/vue-jest'
    },
    testPathIgnorePatterns: [
        '<rootDir>/test/e2e',
        '<rootDir>/test/unit/specs/bundle'
    ],
    snapshotSerializers: ['<rootDir>/node_modules/jest-serializer-vue'],
    setupFiles: ['<rootDir>/test/unit/setup'],
    coverageDirectory: '<rootDir>/test/unit/coverage',
    collectCoverageFrom: [
        'src/**/*.{js,vue}',
        '!src/main.js',
        '!src/router/index.js',
        '!**/node_modules/**'
    ],
    verbose: true,
    testURL: 'http://localhost/'
}
我想重申,如果我删除“预设”选项,测试将按预期运行。
我已经尝试了在“预设”字段中我能想到的所有字符组合,以使其找到 node_module,但它没有找到它。
如果有帮助,这里是预设的文档:
@shelf/jest-mongodb
这还没有解决。
我将添加文件夹结构以查看是否有帮助:
├───.vscode
├───build
├───config
├───node_modules
├───@shelf
│       └───jest-mongodb
│           ├───.circleci
│           └───node_modules
│               ├───.bin
│               ├───.cache
│               │   └───mongodb-memory-server
│               │       └───mongodb-binaries
│               ├───agent-base
│               │   ├───dist
│               │   │   └───src
│               │   ├───node_modules
│               │   │   ├───debug
│               │   │   │   └───src
│               │   │   └───ms
│               │   └───src
│               ├───camelcase
│               ├───cross-spawn
│               │   ├───lib
│               │   │   └───util
│               │   └───node_modules
│               │       └───.bin
│               ├───debug
│               │   ├───dist
│               │   └───src
│               ├───find-cache-dir
│               ├───find-up
│               ├───https-proxy-agent
│               │   ├───dist
│               │   └───node_modules
│               │       ├───debug
│               │       │   └───src
│               │       └───ms
│               ├───locate-path
│               ├───lru-cache
│               ├───mkdirp
│               │   ├───bin
│               │   └───lib
│               ├───mongodb-memory-server
│               ├───mongodb-memory-server-core
│               │   ├───lib
│               │   │   ├───util
│               │   │   │   ├───getos
│               │   │   │   └───__tests__
│               │   │   └───__tests__
│               │   ├───node_modules
│               │   │   ├───.bin
│               │   │   ├───debug
│               │   │   │   └───src
│               │   │   ├───ms
│               │   │   └───uuid
│               │   │       └───dist
│               │   │           ├───bin
│               │   │           ├───esm-browser
│               │   │           ├───esm-node
│               │   │           └───umd
│               │   └───scripts
│               ├───ms
│               ├───p-limit
│               ├───p-locate
│               ├───p-try
│               ├───path-exists
│               ├───path-key
│               ├───pkg-dir
│               ├───semver
│               │   ├───bin
│               │   ├───classes
│               │   ├───functions
│               │   ├───internal
│               │   └───ranges
│               ├───shebang-command
│               ├───shebang-regex
│               ├───uuid
│               │   └───dist
│               │       ├───bin
│               │       ├───esm-browser
│               │       ├───esm-node
│               │       └───umd
│               ├───which
│               │   └───bin
│               └───yallist
├───src
│   ├───assets
│   ├───backend
│   │   ├───models
│   │   └───routes
│   ├───components
│   └───router
└───test
    ├───e2e
    │   ├───custom-assertions
    │   └───specs
    └───unit
        ├───coverage
        │   └───lcov-report
        │       ├───backend
        │       │   ├───models
        │       │   └───routes
        │       └───components
        └───specs
package.json、jest-mongodb-config.js 和 jest.config.js 都位于目录的根目录下。测试位于 test/unit/specs
我尝试过的事情:
  • 删除 package-lock.json 和 node_modules 文件夹,然后再次安装包
  • 从 jest.config.js 文件
  • 中删除除预设之外的所有内容
  • 将预设名称更改为 node_module 的相对路径 (./node_modules/@shelf/jest-mongodb)
  • 最佳答案

    我变了:

    "unit": "jest --config test/unit/jest.conf.js --coverage"
    

    "unit": "jest"
    
    我不知道为什么会解决任何问题,但我很高兴它确实如此(请评论为什么会解决它)

    关于javascript - 开 Jest 配置错误 : "Preset @shelf/jest-mongodb not found.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66833681/

    相关文章:

    javascript - 使用复选框过滤多个类

    javascript - iframe 如何删除自己的沙箱?

    node.js - Azure 未使用正确版本的 Node

    node.js - 使用 Forever 运行的 NodeJS 应用程序在浏览器中出现 502 错误

    javascript - 更新嵌套对象的多个字段

    ruby-on-rails - 使用 Mongo 运行 'bootstrap:themed' 时出现 Twitter Bootstrap 错误

    MongoDB-CR 身份验证失败

    javascript - 大型SVG渲染期间的DOM操作

    javascript - 无法使用 "class"方法在 JavaScript 中进行回调

    node.js - 'ObjectId' 仅指一种类型,但在此处用作值。ts(2693)