shelfio / jest-mongodb

Jest preset for MongoDB in-memory server
MIT License
594 stars 83 forks source link

Consider using the "jsdom" test environment. #211

Open pedroresende opened 4 years ago

pedroresende commented 4 years ago

I currently have the following dependencies in packages.json file

  "dependencies": {
    "@types/bcrypt": "^3.0.0",
    "bcrypt": "^5.0.0",
    "dotenv": "^8.2.0",
    "mongodb": "^3.5.9",
    "next": "9.4.4",
    "react": "16.13.1",
    "react-dom": "16.13.1"
  },
  "devDependencies": {
    "@babel/preset-typescript": "^7.10.1",
    "@fullhuman/postcss-purgecss": "^2.3.0",
    "@shelf/jest-mongodb": "^1.1.5",
    "@testing-library/jest-dom": "^5.11.0",
    "@testing-library/react": "^10.4.3",
    "@types/jest": "^26.0.3",
    "@types/mongodb": "^3.5.25",
    "@types/node": "^14.0.14",
    "@types/react": "^16.9.41",
    "@typescript-eslint/eslint-plugin": "^3.4.0",
    "@typescript-eslint/parser": "^3.4.0",
    "autoprefixer": "^9.8.4",
    "jest": "^26.1.0",
    "mongodb-memory-server-core": "^6.6.1",
    "tailwindcss": "^1.4.6",
    "typescript": "^3.9.5"
  }

my jest.config.js has the following

module.exports = {
    preset: '@shelf/jest-mongodb',
    collectCoverageFrom: [
        'components/*.{js,jsx,ts,tsx}',
        'pages/*.{js,jsx,ts,tsx}',
    ],
    setupFilesAfterEnv: ['<rootDir>/setupTests.js'],
    testPathIgnorePatterns: ['/node_modules/', '/.next/'],
    transform: {
        '^.+\\.(js|jsx|ts|tsx)$': '<rootDir>/node_modules/babel-jest',
        '^.+\\.css$': '<rootDir>/config/jest/cssTransform.js',
    },
    transformIgnorePatterns: [
        '/node_modules/',
        '^.+\\.module\\.(css|sass|scss)$',
    ],
    moduleNameMapper: {
        '^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
    },
}

each time I attempt to run the tests I get the following error

The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/en/configuration#testenvironment-string.
    Consider using the "jsdom" test environment.

    ReferenceError: document is not defined
helloitsjoe commented 4 years ago

You should be able to solve this by adding this to the top of any test files that require document:

/**
 * @jest-environment jsdom
 */
helloitsjoe commented 4 years ago

This appears to be the same issue as #188

nextglabs commented 3 years ago

I had a simular issue and solved it by adding the testEnvironment in my jest.config.js like this:

module.exports = {
  testEnvironment: "jsdom",
  // ... other options
}

Hope it helps!

nextglabs commented 3 years ago

Had similar issue, solution here: https://github.com/shelfio/jest-mongodb/issues/211#issuecomment-878282691

ChandanaMummaneni commented 2 years ago

Can anyone help here https://github.com/kulshekhar/ts-jest/issues/3913