All Projects → shelfio → Jest Mongodb

shelfio / Jest Mongodb

Licence: mit
Jest preset for MongoDB in-memory server

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Jest Mongodb

Express React Boilerplate
🚀🚀🚀 This is a tool that helps programmers create Express & React projects easily base on react-cool-starter.
Stars: ✭ 32 (-90.09%)
Mutual labels:  mongodb, npm-package, jest
Fullstack Shopping Cart
MERN stack shopping cart, written in TypeScript
Stars: ✭ 82 (-74.61%)
Mutual labels:  mongodb, jest
Moveit
🚀 NLW #4 | React+ TypeScript + NextJS + StyledComponents + Firebase + MongoDb +Axios
Stars: ✭ 39 (-87.93%)
Mutual labels:  mongodb, jest
Rest
REST API generator with Node.js, Express and Mongoose
Stars: ✭ 1,663 (+414.86%)
Mutual labels:  mongodb, jest
Mevn Cli
Light speed setup for MEVN(Mongo Express Vue Node) Apps
Stars: ✭ 696 (+115.48%)
Mutual labels:  mongodb, npm-package
Node Express Boilerplate
A boilerplate for building production-ready RESTful APIs using Node.js, Express, and Mongoose
Stars: ✭ 890 (+175.54%)
Mutual labels:  mongodb, jest
Mongodb Memory Server
Spinning up mongod in memory for fast tests. If you run tests in parallel this lib helps to spin up dedicated mongodb servers for every test file in MacOS, *nix, Windows or CI environments (in most cases with zero-config).
Stars: ✭ 1,376 (+326.01%)
Mutual labels:  mongodb, tests
Nodejs Backend Architecture Typescript
Node.js Backend Architecture Typescript - Learn to build a backend server for Blogging platform like Medium, FreeCodeCamp, MindOrks, AfterAcademy - Learn to write unit and integration tests - Learn to use Docker image - Open-Source Project By AfterAcademy
Stars: ✭ 1,292 (+300%)
Mutual labels:  mongodb, jest
Nest User Auth
A starter build for a back end which implements managing users with MongoDB, Mongoose, NestJS, Passport-JWT, and GraphQL.
Stars: ✭ 145 (-55.11%)
Mutual labels:  mongodb, jest
Mongoose Typescript Example
Stars: ✭ 156 (-51.7%)
Mutual labels:  mongodb, jest
Blog Service
blog service @nestjs
Stars: ✭ 188 (-41.8%)
Mutual labels:  mongodb, jest
Clean Ts Api
API em NodeJs usando Typescript, TDD, Clean Architecture, Design Patterns e SOLID principles
Stars: ✭ 619 (+91.64%)
Mutual labels:  mongodb, jest
Jest Allure
Generate Allure Report for jest. Allure Report, a flexible lightweight multi-language test report tool with the possibility to add steps, attachments, parameters and so on.
Stars: ✭ 90 (-72.14%)
Mutual labels:  jest, tests
Typescript Express Starter
🚀 TypeScript Express Starter
Stars: ✭ 238 (-26.32%)
Mutual labels:  mongodb, jest
Monitaure
🔔 A server uptime monitoring progressive web application - NO LONGER MAINTAINED
Stars: ✭ 135 (-58.2%)
Mutual labels:  mongodb, jest
Node Typescript Api
🚀Complete Node.js API built using 👉Typescript | Jest | MongoDB | Express
Stars: ✭ 234 (-27.55%)
Mutual labels:  mongodb, jest
react-multi-context
Manage multiple React 16 contexts with a single component.
Stars: ✭ 19 (-94.12%)
Mutual labels:  jest, npm-package
Coderplanets web
the most sexiest community for developers, build with React, Mobx/MST, GraphQL, Styled-Components, Rxjs, Ramda ... and ❤️
Stars: ✭ 314 (-2.79%)
Mutual labels:  jest
Springy Store Microservices
Springy Store is a conceptual simple μServices-based project using the latest cutting-edge technologies, to demonstrate how the Store services are created to be a cloud-native and 12-factor app agnostic. Those μServices are developed based on Spring Boot & Cloud framework that implements cloud-native intuitive, design patterns, and best practices.
Stars: ✭ 318 (-1.55%)
Mutual labels:  mongodb
Saturday Night Works
Takip ettiğim kaynaklardaki örneklere ait çalışmalar yer alır.
Stars: ✭ 312 (-3.41%)
Mutual labels:  mongodb

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 dynamic database name you must pass empty object for instance field

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

3. Configure MongoDB client

Library sets the process.env.MONGO_URL for your convenience

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

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

  beforeAll(async () => {
    connection = await MongoClient.connect(process.env.MONGO_URL, {
      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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].