Git Product home page Git Product logo

jest-mongodb's Introduction

jest-mongodb CircleCI npm (scoped)

Jest preset to run MongoDB memory server

Usage

0. Install

$ yarn add @shelf/jest-mongodb --dev

Make sure mongodb is installed in the project as well, as it's required as a peer dependency.

1. Create jest.config.js

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

If you have a custom jest.config.js make sure you remove testEnvironment property, otherwise it will conflict with the preset.

2. Create jest-mongodb-config.js

See mongodb-memory-server

module.exports = {
  mongodbMemoryServerOptions: {
    binary: {
      version: '4.0.3',
      skipMD5: true,
    },
    autoStart: false,
    instance: {},
  },
};

To use the same database for all tests pass the config like this:

module.exports = {
  mongodbMemoryServerOptions: {
    binary: {
      version: '4.0.3',
      skipMD5: true,
    },
    instance: {
      dbName: 'jest',
    },
    autoStart: false,
  },
};

To use separate database for each jest worker pass the useSharedDBForAllJestWorkers: false (doesn't create process.env variable when using this option):

module.exports = {
  mongodbMemoryServerOptions: {
    binary: {
      skipMD5: true,
    },
    autoStart: false,
    instance: {},
  },

  useSharedDBForAllJestWorkers: false,
};

To use dynamic database name you must pass empty object for instance field:

module.exports = {
  mongodbMemoryServerOptions: {
    binary: {
      version: '4.0.3',
      skipMD5: true,
    },
    instance: {},
    autoStart: false,
  },
};

To use another uri environment variable name you must set mongoURLEnvName field:

module.exports = {
  mongodbMemoryServerOptions: {
    binary: {
      version: '4.0.3',
      skipMD5: true,
    },
    instance: {},
    autoStart: false,
  },
  mongoURLEnvName: 'MONGODB_URI',
};

To use mongo as a replica set you must add the replSet config object and set count and storageEngine fields:

module.exports = {
  mongodbMemoryServerOptions: {
    binary: {
      skipMD5: true,
    },
    autoStart: false,
    instance: {},
    replSet: {
      count: 3,
      storageEngine: 'wiredTiger',
    },
  },
};

3. Configure MongoDB client

Library sets the process.env.MONGO_URL for your convenience, but using of global.__MONGO_URI__ is preferable as it works with useSharedDBForAllJestWorkers: false

const {MongoClient} = require('mongodb');

describe('insert', () => {
  let connection;
  let db;

  beforeAll(async () => {
    connection = await MongoClient.connect(global.__MONGO_URI__, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    });
    db = await connection.db();
  });

  afterAll(async () => {
    await connection.close();
  });
});

4. PROFIT! Write tests

it('should insert a doc into collection', async () => {
  const users = db.collection('users');

  const mockUser = {_id: 'some-user-id', name: 'John'};
  await users.insertOne(mockUser);

  const insertedUser = await users.findOne({_id: 'some-user-id'});
  expect(insertedUser).toEqual(mockUser);
});

Cache MongoDB binary in CI by putting this folder to the list of cached paths: ./node_modules/.cache/mongodb-memory-server/mongodb-binaries

You can enable debug logs by setting environment variable DEBUG=jest-mongodb:*

5. Clean collections before each test (optional)

beforeEach(async () => {
  await db.collection('COLLECTION_NAME').deleteMany({});
});

See this issue for discussion

6. Jest watch mode gotcha

This package creates the file globalConfig.json in the project root, when using jest --watch flag, changes to globalConfig.json can cause an infinite loop

In order to avoid this unwanted behaviour, add globalConfig to ignored files in watch mode in the Jest configuation

// jest.config.js
module.exports = {
  watchPathIgnorePatterns: ['globalConfig'],
};

See Also

Publish

$ git checkout master
$ yarn version
$ yarn publish
$ git push origin master --tags

License

MIT © Shelf

jest-mongodb's People

Contributors

addwot avatar ajwootto avatar arisjulio avatar danez avatar diegomais avatar drewsabuse avatar harazdovskiy avatar icco avatar ilyamore88 avatar ixartz avatar jenlky avatar kandros avatar knupman avatar kohtala avatar madefortv avatar natac13 avatar noseworthy avatar oezguen-yalcin avatar randomhash avatar renovate-bot avatar renovate[bot] avatar robin-hoodie avatar rodolfosilva avatar sibelius avatar siloss avatar simenb avatar simonbrunel avatar tim-hoffmann avatar vladgolubev avatar vladholubiev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jest-mongodb's Issues

Support random database names

If I understood the the code correctly the configuration must include the mongodbMemoryServerOptions.instance.dbName key in jest-mongodb-config.js file and fill it with a valid name.

But if you check mongodb-memory-server options here you'll see that not supplying dbName will generate a random db name:

dbName?: string, // by default generate random dbName

Is this supported here as well? and if not can it be added so that tests can run in parallel with several databases at once?

[Question] Avoid downloading MongDB for every package

Hi,

I'm facing the following problem, I've got several microservices structured in a way that every microservice is a project. Every microservice inherits from the base package which holds the commons dependencies used for every project. One of these dependencies is jest-mongodb and mongodb for unit testing;

The problem I am facing is that every microservice is downloading a mongoDB instead of downloading just once for the base package.json where it's defined. Any suggestion on how to solve this?

Validation Error: Preset @shelf/jest-mongodb not found.

I'm getting the following validation error:

● Validation Error:

  Preset @shelf/jest-mongodb not found.

  Configuration Documentation:
  https://facebook.github.io/jest/docs/configuration.html

This is what my jest.config.js looks like:

const path = require('path')

module.exports = {
	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/',
	preset: '@shelf/jest-mongodb'
}

I installed @shelf/jest-mongodb with the following command:

npm install @shelf/jest-mongodb --also=dev

Any ideas?

Edit:
I have tried deleting node_modules and package-lock.json, this didn't work.

mongoose

It would be cool to add a new section explaining the usage with mongoose

Option to rename MONGO_URL

My setup requires that I load all constants from a single file - src/constants/index.ts and I have .env.test for testing. So, my preference is that I do use MONGO_URI instead of MONGO_URL.

I have a proposal that we add another variable to jest-mongodb-config.js:

interface JestMongoDBConfig {
  mongodbMemoryServerOptions: MongoMemoryServerOptsT
  mongoURLEnvName?: string
}

mongoURLEnvName defaults back to MONGO_URL. I can submit a PR if the idea is appealing.

Incorrect cache folder in readme.md

The readme states that the binaries are downloaded to ~/.mongodb-binaries but I don't think that's right.
According to nodkz/mongodb-memory-server the default folder is node_modules/.cache/mongodb-memory-server/mongodb-binaries

A quick run seems to confirm that to be the right folder.

Conflict with tests that require window/document

Thanks a lot for creating this package. I have installed it and it works perfectly for interracting with mongo in my tests.

However it comes with a regression which is tests that require the window/dom (e.g. from react testing library document.createElement) require the default testEnvironment: jsdom which this overwrites.

Is there a way to prevent this overwriting such functionality?

Any interest in a TypeScript port and/or definitions?

I want to write a Jest environment that includes both MongoDB and some extra unique requirements. I'd like to extend the good work already done in this repo rather than reinvent the wheel.

We're using TypeScript and naturally it complains that there are no type defs. I'm ready to put in some work to create them.

I'd prefer not to provide manual type definitions and publish them separately because they're a pain to write and maintain. But I'd be happy to provide a complete port instead if that's interesting. I thought I'd check if there is any interest or objection before doing so.

Allow silencing the logs

The logs creates a fair amount of noise to the test output. Using debug might be a good way to keep the logs but exclude them for regular runs.

Screen Shot 2019-05-01 at 1 16 28 PM

Operator $unset is not recognized

Hi,

I'm trying to test my function that uses the $ unset operator in your aggregation query, but it is not recognized, returning the error:

MongoError: Unrecognized pipeline stage name: '$unset'

My jest-mogodb-config.js:

module.exports = {
  mongodbMemoryServerOptions: {
    instance: {
      dbName: 'jest',
    },
    binary: {
      version: '4.0.2',
      skipMD5: true,
    },
    autoStart: false,
  },
};

Another issue has already been opened for this problem but has not yet been resolved:
#200

This is a part of my query:

        $unset: [
          'accountMemberId',
          'externalPaymentId',
          'person.phoneNumbers.accountMemberId',
          'accountMember',
        ],
      },

How to use both jest-mongodb and jest-dynamodb

Thank you for great support for testing with MongoDB and DynamoDB with Jest.

In one repo I have multiple services connecting to either MongoDB and DynamoDB, is it possible to combine both presets to use them together?

Db name different for each test file

Hi,
from Readme I was under impression that if I pass empty object to instance property I will get random db per file.

If I simply print out the global variables in every test file I am getting:
One file:

process.env.MONGO_URL: mongodb://127.0.0.1:56925/3b97e130-dd09-440d-9937-a7442a421da7?
global.__MONGO_URI__: mongodb://127.0.0.1:56925/3b97e130-dd09-440d-9937-a7442a421da7?
global.__MONGO_DB_NAME__: 1ed20263-b27c-42c8-a800-805368f8fdb3

Second file:

process.env.MONGO_URL: mongodb://127.0.0.1:56925/3b97e130-dd09-440d-9937-a7442a421da7?
global.__MONGO_URI__: mongodb://127.0.0.1:56925/3b97e130-dd09-440d-9937-a7442a421da7?
global.__MONGO_DB_NAME__: e2eb11c0-c836-4849-a7fb-568a642e02bf

So its true that db name is random, but its same for all test files, which is not really that much useful. My expectation was that it would be unique db name per test file - interestingly thats what we get in MONGO_DB_NAME, but we still need to create sensible MONGO_URL from it, it seems.

Am I missing something?

[Question] Why globalConfig.json?

Hi everyone!

Thank you very much for this utility!

I have a question that I can't answer no matter how hard I try to dive into the code. Why are global variables written to a globalConfig.json file?

Thank you very much for the reply! 😄
Best regards!

How can I provide the collection to the file under test.

I used this repo to successfully setup mongodb in my test environment. It works great when I create and populate a collection within the test file, but when I import a method from another file that uses a collection it doesn't recognize it. Which isn't surprising.

Is there an example of how to set up a test mongodb with collections that are available to files that import them?

Cannot find module '@shelf/jest-mongodb'

I must be doing something stupid, but I can't seem to resolve @shelf/jest-mongodb. Here's a minimal repro:

$ npm i -D @shelf/jest-mongodb

$ echo "require('@shelf/jest-mongodb')" > test.js

$ node test.js

internal/modules/cjs/loader.js:626
    throw err;
    ^

Error: Cannot find module '@shelf/jest-mongodb'

At the same time,

$ cat node_modules/@shelf/jest-mongodb/package.json | grep name

    "name": "@shelf/jest-mongodb",

Any other package (incl. scoped) works. Originally ran into this with jest. I needs to use both ts-jest and @shelf/jest-mongodb, so I require both and then merge them with my own overwrites.

Env:

  • node -v: v12.3.1
  • npm -v: 6.9.0

Error in Readme.md

I notice that in the Readme example you use:

  beforeAll(async () => {
    connection = await MongoClient.connect(global.__MONGO_URI__, {useNewUrlParser: true});
    db = await connection.db(global.__MONGO_DB_NAME__);
  });

But in your code you are not using that global anymore

  // Set reference to mongod in order to close the server during teardown.
  global.__MONGOD__ = mongod;
  process.env.MONGO_URL = mongoConfig.mongoUri;

'@shelf/jest-mongodb@latest' is not in the npm registry.

npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/@shelf%2fjest-mongodb - Not found
npm ERR! 404 
npm ERR! 404  '@shelf/jest-mongodb@latest' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! A complete log of this run can be found in:

Exception when using change streams

If I subscribe to a change stream, using mongoose's watch method, I encounter the following exception.

Is there a way to configure the mongo memory server to support this?

Exception has occurred: Error [ERR_UNHANDLED_ERROR]: Unhandled error. (MongoError: Storage engine does not support read concern: { readConcern: { level: "majority" } }
  at MessageStream.messageHandler (/Users/jim/development/.../node_modules/mongodb/lib/cmap/connection.js:263:20)
    at MessageStream.emit (events.js:315:20)
    at processIncomingData (/Users/jim/development/.../node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/Users/jim/development/.../node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at writeOrBuffer (internal/streams/writable.js:358:12)
    at MessageStream.Writable.write (internal/streams/writable.js:303:10)
    at Socket.ondata (internal/streams/readable.js:719:22)
    at Socket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at Socket.Readable.push (internal/streams/readable.js:223:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
    at TCP.callbackTrampoline (internal/async_hooks.js:131:14) {
  ok: 0,
  code: 148,
  codeName: 'ReadConcernMajorityNotEnabled'
})
    at ChangeStream.emit (events.js:304:17)
    at ChangeStream.<anonymous> (/Users/jim/development/.../node_modules/mongoose/lib/cursor/ChangeStream.js:41:51)
    at ChangeStream.emit (events.js:315:20)
    at processError (/Users/jim/development/.../node_modules/mongodb/lib/change_stream.js:572:38)
    at ChangeStreamCursor.<anonymous> (/Users/jim/development/.../node_modules/mongodb/lib/change_stream.js:436:5)
    at ChangeStreamCursor.emit (events.js:315:20)
    at /Users/jim/development/.../node_modules/mongodb/lib/core/cursor.js:343:16
    at /Users/jim/development/.../node_modules/mongodb/lib/core/cursor.js:736:9
    at /Users/jim/development/.../node_modules/mongodb/lib/change_stream.js:330:9
    at done (/Users/jim/development/.../node_modules/mongodb/lib/core/cursor.js:458:7)

Option to disable `jest-mongodb` and use real database

Add and option to disable jest-mongodb and use real database, so it's possible to use the same tests both in local and/or unit tests, and for a real deployment like integration tests.

Implementation would be easy, just a matter creating the MongodbMemoryServer instance inside

module.exports = async () => {
after checking for a condition, and later check on
module.exports = async function () {
if global.__MONGOD__ has been set... Problem is what condition to use. Obvious one would be an environment variable, like if process.env.MONGO_URL is already set, but maybe overwritting it by jest-mongodb could be a valid use case, for example if it's already set in a .env file... Maybe a DISABLE_JEST_MONGODB environment variable?

Blank DB for every single test

I would like to have a clean DB for every single unit test.

I'm using more or less the code from your README. However I'm using beforeEach and afterEach instead of *All. I would have expected the database to be clear after closing the connection in one unit test and creating a new one in the next, but apparently this is not the case.

When rerunning the tests, the first test always starts from a "clean" database. How can I achieve this for every test? At the moment, to achieve what I want, I drop the tables in afterEach, but I'm sure there must be a better approach? You can view me code here.

How can we spin up the mongodb-memory-server only for the tests that depend on DB?

First of all, thanks so much for creating this repo example - I had been trying for days and it is really helpful and appreciated.

However, I wonder if there is any way to configure it so that the mongodb-memory-server is only started up when we are running the tests / test suites that would depend on DB connection (e.g. usually the slower tests such as integration tests), but not when only unit tests are run?

FYI, currently I am grouping my tests into the unit tests (run more frequently for faster feedback) and integration tests (slower and thus are run less frequently)

Consider using the "jsdom" test environment.

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

Error in watch mode

When I use the config file in watch mode. It report error below:

Error: Database instance is not running. You should start database by calling start() method. BTW it should start automatically if opts.autoStart!=false. Also you may provide opts.debug=true for more info.
    at MongoMemoryServer._callee6$ (/Users/arvin/Desktop/koa-app/node_modules/mongodb-memory-server/lib/MongoMemoryServer.js:304:23)
    at tryCatch (/Users/arvin/Desktop/koa-app/node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:62:40)
    at Generator.invoke [as _invoke] (/Users/arvin/Desktop/koa-app/node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:296:22)
    at Generator.prototype.(anonymous function) [as next] (/Users/arvin/Desktop/koa-app/node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:114:21)
    at step (/Users/arvin/Desktop/koa-app/node_modules/mongodb-memory-server/lib/MongoMemoryServer.js:79:191)
    at /Users/arvin/Desktop/koa-app/node_modules/mongodb-memory-server/lib/MongoMemoryServer.js:79:437
    at new Promise (<anonymous>)
    at MongoMemoryServer.<anonymous> (/Users/arvin/Desktop/koa-app/node_modules/mongodb-memory-server/lib/MongoMemoryServer.js:79:99)
    at MongoMemoryServer.getInstanceData (/Users/arvin/Desktop/koa-app/node_modules/mongodb-memory-server/lib/MongoMemoryServer.js:315:22)
    at MongoMemoryServer._callee5$ (/Users/arvin/Desktop/koa-app/node_modules/mongodb-memory-server/lib/MongoMemoryServer.js:251:29)
    at tryCatch (/Users/arvin/Desktop/koa-app/node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:62:40)
    at Generator.invoke [as _invoke] (/Users/arvin/Desktop/koa-app/node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:296:22)
    at Generator.prototype.(anonymous function) [as next] (/Users/arvin/Desktop/koa-app/node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:114:21)
    at step (/Users/arvin/Desktop/koa-app/node_modules/mongodb-memory-server/lib/MongoMemoryServer.js:79:191)
    at /Users/arvin/Desktop/koa-app/node_modules/mongodb-memory-server/lib/MongoMemoryServer.js:79:437
    at new Promise (<anonymous>)
    at MongoMemoryServer.<anonymous> (/Users/arvin/Desktop/koa-app/node_modules/mongodb-memory-server/lib/MongoMemoryServer.js:79:99)
    at MongoMemoryServer.stop (/Users/arvin/Desktop/koa-app/node_modules/mongodb-memory-server/lib/MongoMemoryServer.js:283:22)
    **at module.exports (/Users/arvin/Desktop/koa-app/test/config/teardown.js:3:27)**
    at /Users/arvin/Desktop/koa-app/node_modules/jest-cli/build/run_jest.js:242:49
    at Generator.next (<anonymous>)
    at step (/Users/arvin/Desktop/koa-app/node_modules/jest-cli/build/run_jest.js:81:191)

In error message I find may teardown.js file go wrong.And after I comment line 3.

   await global.__MONGOD__.stop();

It works well.
So maybe this code should be refactored to suit watch mode?
Now I use it with that line commented.

Support $search aggregate

Hi

Been using jest-mongodb for a while now, and it suits most test cases. Thanks for putting it together!

We recently added $search functionality to some of our aggregations and would like to test them, and the library sends back errors each time we try.

Does the library currently support $search, and I am just using it wrong, or does it need adding?

If it needs adding, is it in the pipeline, or do you want me to have a crack at adding it in?

npm audit reports nested dependency decompress as vulnerable

The npm audit output is:

  High            Arbitrary File Write
  Package         decompress
  Patched in      No patch available
  Dependency of   @shelf/jest-mongodb [dev]
  Path            @shelf/jest-mongodb > mongodb-memory-server >
                  mongodb-memory-server-core > decompress
  More info       https://npmjs.com/advisories/1217

found with @shelf/jest-mongodb: version 1.1.3

Mongo instance on Bitbucket pipelines

Tried exactly as given in https://jestjs.io/docs/en/mongodb. The local integration tests work fine but the tests fail on the bitbucket pipeline (uses alpine image).

This is the error generated:

Starting the instance failed, please enable debug for more infomation
TypeError: Cannot read property 'toString' of undefined
    at MongoInstance._launchKiller (/opt/atlassian/pipelines/agent/build/node_modules/mongodb-memory-server-core/lib/util/MongoInstance.js:259:22)
    at MongoInstance.<anonymous> (/opt/atlassian/pipelines/agent/build/node_modules/mongodb-memory-server-core/lib/util/MongoInstance.js:148:51)
    at step (/opt/atlassian/pipelines/agent/build/node_modules/mongodb-memory-server-core/lib/util/MongoInstance.js:44:23)
    at Object.next (/opt/atlassian/pipelines/agent/build/node_modules/mongodb-memory-server-core/lib/util/MongoInstance.js:25:53)
    at fulfilled (/opt/atlassian/pipelines/agent/build/node_modules/mongodb-memory-server-core/lib/util/MongoInstance.js:16:58)
error Command failed with exit code 1. 

Not able to understand from the documentation if I should do something different for this to work on pipelines.
what am I missing?

$pull or $pullAll are not working

Hi,

I can't believe I'm actually posting this issue but It's my last chance to fix my problem.
I can't use $pullAll update operator with jest-mongo. It's working well with my actual database. But when I ran my units tests (jest), they failed because this operation is doing nothing on a mongodb memory server.

 await AccountModel.updateOne(
        { uid: userId },
        { $pullAll: { fcmTokens: fcmTokens }, $set: { nickname: "ooo" } }
      ).exec();

Here is my code sample, the $set operation is done but not the $pullAll.

node v14.15.1

    "@shelf/jest-mongodb": "^1.2.3",
   "mongoose": "^5.11.15",

TypeError: Cannot read property 'getConnectionString' of undefined

Hey there!

If I pull down your repo and npm install like so:

git clone [email protected]:vladgolubev/jest-mongodb.git && cd jest-mongodb
npm install

Running Jest will fail like so:

▶ jest

 RUNS  ./mongo-insert.test.js
 FAIL  ./mongo-aggregate.test.js
  ● Test suite failed to run

    TypeError: Cannot read property 'getConnectionString' of undefined

       9 |     console.log('Setup MongoDB Test Environment');
      10 |
    > 11 |     this.global.__MONGO_URI__ = await global.__MONGOD__.getConnectionString();
      12 |     this.global.__MONGO_DB_NAME__ = global.__MONGO_DB_NAME__;
      13 |
      14 |     await super.setup();

      at MongoEnvironment.setup (mongo-environment.js:11:57)

 FAIL  ./mongo-insert.test.js
  ● Test suite failed to run

    TypeError: Cannot read property 'getConnectionString' of undefined

       9 |     console.log('Setup MongoDB Test Environment');
      10 |
    > 11 |     this.global.__MONGO_URI__ = await global.__MONGOD__.getConnectionString();
      12 |     this.global.__MONGO_DB_NAME__ = global.__MONGO_DB_NAME__;
      13 |
      14 |     await super.setup();

      at MongoEnvironment.setup (mongo-environment.js:11:57)

Test Suites: 2 failed, 2 total
Tests:       0 total
Snapshots:   0 total
Time:        0.409s
Ran all test suites.
Teardown mongod

If I run with --runInBand will succeed:

▶ jest --runInBand
Setup MongoDB Test Environment
 PASS  ./mongo-insert.test.js
Teardown MongoDB Test Environment
Setup MongoDB Test Environment
 PASS  ./mongo-aggregate.test.js

Test Suites: 2 passed, 2 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        1.504s
Ran all test suites.
Teardown mongod
Teardown MongoDB Test Environment

And Jest runs that use the cache will succeed to unless you either manually remove the cache or pass --no-cache, ie:

▶ jest
Setup MongoDB Test Environment
 PASS  ./mongo-insert.test.js
Teardown MongoDB Test Environment
Setup MongoDB Test Environment
 PASS  ./mongo-aggregate.test.js

Test Suites: 2 passed, 2 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        1.089s
Ran all test suites.
Teardown mongod
Teardown MongoDB Test Environment

~/jest-mongodb  master ✗                                                                                        68d ⚑
▶ jest --no-cache

 RUNS  ./mongo-insert.test.js
 FAIL  ./mongo-insert.test.js.js
  ● Test suite failed to run

    TypeError: Cannot read property 'getConnectionString' of undefined

       9 |     console.log('Setup MongoDB Test Environment');
      10 |
    > 11 |     this.global.__MONGO_URI__ = await global.__MONGOD__.getConnectionString();
      12 |     this.global.__MONGO_DB_NAME__ = global.__MONGO_DB_NAME__;
      13 |
      14 |     await super.setup();

      at MongoEnvironment.setup (mongo-environment.js:11:57)

 FAIL  ./mongo-aggregate.test.js
  ● Test suite failed to run

    TypeError: Cannot read property 'getConnectionString' of undefined

       9 |     console.log('Setup MongoDB Test Environment');
      10 |
    > 11 |     this.global.__MONGO_URI__ = await global.__MONGOD__.getConnectionString();
      12 |     this.global.__MONGO_DB_NAME__ = global.__MONGO_DB_NAME__;
      13 |
      14 |     await super.setup();

      at MongoEnvironment.setup (mongo-environment.js:11:57)

Test Suites: 2 failed, 2 total
Tests:       0 total
Snapshots:   0 total
Time:        0.44s
Ran all test suites.
Teardown mongod

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (github>shelfio/renovate-config)

Usage with jest globalSetup and globalTeardown

Awesome module!

We're running fine with the current implementation, however, one small sticking point is that we have to perform a new connection and disconnection within every jest block. It would be awesome if we were able to use a globalSetup and globalTeardown to connect/disconnect our client (mongoose) to the database once.

Unfortunately, when I do this, the process.env.MONGO_URL is undefined:

module.exports = async function globalSetup() {
  await mongoose.connect(process.env.MONGO_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true,
    useFindAndModify: false,
  });
};

I suspect this is because the setup and teardown might run before the preset? Any thoughts as to a way around this limitation?

Why is it downloading MongoDB if I already have it installed ?

I have mongodb installed on my machine,

mongo --version // MongoDB shell version v4.2.6

In package.json

"dependencies": {
    "mongodb": "^3.3.0",
}

In jest-mongodb-config.json

module.exports = {
  mongodbMemoryServerOptions: {
    binary: {
      version: "latest",
      skipMD5: true,
    },
    autoStart: false,
    instance: {},
  },
};

Still, when I run npm test jest, it starts downloading mongodb

Determining test suites to run...Downloading MongoDownloading MongoDB latest: 0.1 % (0.2mb / 310.2mb)

globalConfig.json file location change

Whether on purpose or not, the location of the globalConfig.json changed to my projects root directory instead of @sheldfio in node_modules jest-mongodb root.

I have my own custom environment and have to deal with this file manually. This broke my test environment until I put in a check for the new location.

Using a cwd module before initialization

I tried to create MongoDB instance with jest-mongodb-config.js, but I always got random mongoUri in globalConfig.json like this:

"mongoUri":"mongodb://127.0.0.1:62212/6062f011-b0af-4bd3-89be-cd1ed47c4c37?"

When I added console.log(e) in getMongodbMemoryOptions() function, I got this:

ReferenceError: Cannot access 'cwd' before initialization
    at getMongodbMemoryOptions (D:\WebStormProjects\hawk.mono\api\node_modules\@shelf\jest-mongodb\setup.js:34:58)
    at Object.<anonymous> (D:\WebStormProjects\hawk.mono\api\node_modules\@shelf\jest-mongodb\setup.js:6:48)
    at Module._compile (internal/modules/cjs/loader.js:1201:30)
    at Module._extensions..js (internal/modules/cjs/loader.js:1221:10)
    at Object.newLoader [as .js] (D:\WebStormProjects\hawk.mono\api\node_modules\pirates\lib\index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:1050:32)
    at Function.Module._load (internal/modules/cjs/loader.js:938:14)
    at Module.require (internal/modules/cjs/loader.js:1090:19)
    at require (internal/modules/cjs/helpers.js:75:18)
    at ScriptTransformer.requireAndTranspileModule (D:\WebStormProjects\hawk.mono\api\node_modules\@jest\transform\build\ScriptTransformer.js:676:20)

You need to init cwd before creating MongodbMemoryServer.

TypeError: Cannot read property 'dbName' of undefined

How it can be that updating a 'patch' version brings a "breaking change".
Updating from 1.1.0 to 1.1.1 now i get the error in the title. Maybe add a default?

Also i would like to know the benefit that this change brings. What was the reason for it?

Config dbName does not change global.__MONGO_DB_NAME__

Hi, I have been using this library for a while. I have noticed global.__MONGO_DB_NAME__ does not return the same dbname at the end of global.__MONGO_URI__.

The dbName testing in jest-mongodb-config is reflected in global.__MONGO_URI__ as mongodb://127.0.0.1:52302/testing. However, global.__MONGO_DB_NAME__ shows jest.

I have forked and managed to fix it with one line. Going to make a PR. Please take a look.

jest-mongodb-config

module.exports = {
  mongodbMemoryServerOptions: {
    instance: {
      dbName: 'testing'
    },
    binary: {
      skipMD5: true
    },
    autoStart: false
  }
};

mongo-aggregate.test.js

  beforeAll(async () => {
    connection = await MongoClient.connect(global.__MONGO_URI__, {useNewUrlParser: true});
    
    console.log('global.__MONGO_URI__', global.__MONGO_URI__) 
    // returns mongodb://127.0.0.1:52302/testing
    
    console.log('global.__MONGO_DB_NAME__', global.__MONGO_DB_NAME__) 
    // returns jest

    db = await connection.db(global.__MONGO_DB_NAME__);
  });

image

How to use with ts-jest?

Has anybody gotten jest-mongodb to work with ts-jest?

They both require a preset, which I'm not sure how to get around 🤔

preset for ts-jest is ts-jest VS @shelf/jest-mongodb

Thanks!

The tests stop running after installing jest-mongodb

Tried to install jest-mongodb on a working project with tests that were already passing, but after installing it all the tests stops working, it just displays "Determining test suites to run..." and after a few seconds it shows something like "Done in 2.77s.". Tried to carefully follow all the instructions on README but still was not able to make it work. When I comment th preset: '@shelf/jest-mongodb' line on my jest.config.js it works again.

I have a simple Express API (4.17.1) with Mongoose (5.7.12) running on Debian 10 Buster, Node v12.13.1.

Unrecognized pipeline stage name: '$unset'

Hi,

I'm trying to test some of my code, where I'm using $unset stage when aggregating data and I'm getting this error:

MongoError: Unrecognized pipeline stage name: '$unset'

This is my jest-mogodb-config.js

module.exports = {
  mongodbMemoryServerOptions: {
    instance: {
      dbName: 'jest'
    },
    binary: {
      skipMD5: true
    },
    autoStart: false
  }
};

What am I doing wrong?

Thanks!

@shelf/jest-mongodb does not work with TypeScript

Here the config I am using for jest in jest.config.js:

module.exports = {
  transform: {
    '^.+\\.jsx?$': 'babel-jest',
    '^.+\\.ts?$': 'ts-jest'
  },
  rootDir: './lib',
  roots: ['<rootDir>'],
  moduleFileExtensions: ['ts', 'js', 'json', 'node'],
  preset: "@shelf/jest-mongodb"
};

Jest fails to run tests just saying "Determining test suites to run..."

Works if I replace preset: "@shelf/jest-mongodb" with preset: "ts-jest"

[Question] about process.env

Dear vladgolubev,

I'm new into Jest

I would like to ask any reason behind to use a persistent json file to get mongo db_name mongo_url, etc.

As I saw this line of code. Why don't we just return process.env.something instead of save into a new json file?

Docs should point out parallel test issues

Running parallel tests has issues with this because one MongoDB instance is used for all tests.

If you have another test running concurrently that hits the the same collection it causes race conditions (if you clear the collection in between tests).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.