skip to Main Content

I’m following the documentation on jestjs.io. When I try to run a test I get this error:

Error: Jest: Got error running globalSetup - /home/.../node_modules/@shelf/jest-mongodb/setup.js, reason: Instance Exited before being ready and without throwing an error!

This happened both when I’ve used typescript and also when I created a simple app using javascript instead:

package.json:

{
  "name": "mongojest",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "devDependencies": {
    "@shelf/jest-mongodb": "^3.0.1"
  },
  "scripts": {
    "test": "jest"
  },
  "dependencies": {
    "jest": "^28.1.0"
  }
}

jest.config.js:

module.exports = {
  coverageProvider: "v8",
  "preset": "@shelf/jest-mongodb"
};

I haven’t made any changes other than the minimum installs to get it running.

2

Answers


  1. Chosen as BEST ANSWER

    I got around this issue by installing Ubuntu 20.04. I was using the newer Ubuntu 22.04 and it seem to have caused some issues.


  2. I installed typescript in my project. That was enough to run. Follow my configuration files

    //package.json
    
    {
      "name": "research-with-programmers-api",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "jest --passWithNoTests --silent --noStackTrace --runInBand",
        "test:verbose": "jest --passWithNoTests --runInBand",
        "test:unit": "yarn test -- --watch -c jest-unit-config.ts",
        "test:integration": "yarn test -- --watch -c jest-integration-config.ts",
        "test:staged": "yarn test -- --findRelatedTest",
        "test:ci": "yarn test -- --coverage"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "devDependencies": {
        "@shelf/jest-mongodb": "^4.1.0",
        "@types/bcrypt": "^5.0.0",
        "@types/jest": "^28.1.8",
        "@types/mongodb": "^4.0.7",
        "@types/node": "^18.7.13",
        "@types/validator": "^13.7.6",
        "@typescript-eslint/eslint-plugin": "^5.0.0",
        "eslint": "^8.0.1",
        "eslint-config-standard-with-typescript": "^22.0.0",
        "eslint-plugin-import": "^2.25.2",
        "eslint-plugin-n": "^15.0.0",
        "eslint-plugin-promise": "^6.0.0",
        "git-commit-msg-linter": "^4.1.3",
        "husky": "^8.0.1",
        "jest": "^29.0.1",
        "lint-staged": "^13.0.3",
        "ts-jest": "^28.0.8",
        "ts-node": "^10.9.1",
        "typescript": "^4.8.2"
      },
      "dependencies": {
        "bcrypt": "^5.0.1",
        "mongodb": "^4.10.0",
        "validator": "^13.7.0"
      }
    }
    
    
    //jest-config.ts
    
    export default {
      roots: ['<rootDir>/src'],
      collectCoverageFrom: [
        '<rootDir>/src/**/*.ts',
        '!<rootDir>/src/**/protocols/*.ts',
        '!<rootDir>/src/**/*protocols.ts',
        '!<rootDir>/src/**/models/*.ts',
        '!<rootDir>/src/**/usecases/*.ts',
        '!<rootDir>/src/**/index.ts'
      ],
      collectCoverage: true,
      coverageDirectory: 'coverage',
      coverageProvider: 'v8',
      testEnvironment: 'node',
      preset: '@shelf/jest-mongodb',
      transform: {
        '.+\.ts$': 'ts-jest'
      }
    }
    
    // jest-mongodb-config.ts
    
    export default {
      mongodbMemoryServerOptions: {
        instance: {
          dbName: 'jest'
        },
        binary: {
          version: '4.0.3',
          skipMD5: true
        },
        autoStart: false
      }
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search