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

Posted

技术标签:

【中文标题】玩笑配置错误:“未找到预设 @shelf/jest-mongodb。”【英文标题】:Error with jest config: "Preset @shelf/jest-mongodb not found." 【发布时间】:2021-06-24 06:29:41 【问题描述】:

我有一个使用 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)

【问题讨论】:

【参考方案1】:

我变了:

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

"unit": "jest"

我不知道为什么会修复任何问题,但我很高兴它确实如此(请评论为什么会修复它)

【讨论】:

你记得用 yarn 或 npm 下载预设吧?看起来您已将其作为代码库中的子目录

以上是关于玩笑配置错误:“未找到预设 @shelf/jest-mongodb。”的主要内容,如果未能解决你的问题,请参考以下文章

TypeError:开玩笑:转换必须导出某些东西

开玩笑:测试套件无法运行,意外令牌 =

用于 react-native 的 Code Push 打破玩笑测试

Gitlab - 开玩笑测试 - 分段错误(核心转储)

赛普拉斯在开玩笑断言中导致类型错误

开玩笑错误找不到模块....映射为: