Error with jest config: “Preset @shelf/jest-mongodb not found.”

i have a project that uses jest, i can run jest with npm test and it works if i dont set a preset.

I need the preset @shelf/jest-mongodb, and i get the error that is in the title of this post.

Here is my 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/'
}

I want to reiterate that the tests are run as expected if i remove the ‘preset’ option.

I have tried every combination of characters i can think of in the ‘preset’ field to get it to find the node_module, and it isn’t finding it.

Here are the docs for the preset if that will help:

@shelf/jest-mongodb

This still hasn’t been resolved.

I will add the folder structure to see if that will help:

├───.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, and jest.config.js are all located in the root of directory. Tests are located in test/unit/specs

Things i’ve tried:

  • Deleting the package-lock.json, and node_modules folder, then installing packages again
  • Removing everything but the preset from the jest.config.js file
  • Changing preset name to the relative path to the node_module (./node_modules/@shelf/jest-mongodb)

Answer

I changed:

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

to

"unit": "jest"

I don’t know why that fixes anything, but im happy it does (please comment why this has fixed it)