jest 配置错误:“未找到预设 @shelf/jest-mongodb。”

Beb*_*bet 6 javascript testing mongodb node.js jestjs

我有一个使用 jest 的项目,我可以运行 jest,npm test如果我不设置预设,它就可以工作。

我需要预设@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/'
}
Run Code Online (Sandbox Code Playgroud)

我想重申,如果我删除“预设”选项,测试将按预期运行。

我已经在“预设”字段中尝试了我能想到的所有字符组合来让它找到 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
Run Code Online (Sandbox Code Playgroud)

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) 的相对路径

Beb*_*bet 2

我变了:

"unit": "jest --config test/unit/jest.conf.js --coverage"
Run Code Online (Sandbox Code Playgroud)

"unit": "jest"
Run Code Online (Sandbox Code Playgroud)

我不知道为什么这可以解决任何问题,但我很高兴它做到了(请评论为什么这已经解决了它)