项目作者: lirbank

项目描述 :
Jest environment with a running in-memory MongoDB server
高级语言: TypeScript
项目地址: git://github.com/lirbank/jest-environment-mongodb.git
创建时间: 2019-03-22T19:27:01Z
项目社区:https://github.com/lirbank/jest-environment-mongodb

开源协议:MIT License

下载


jest-environment-mongodb

npm version
lerna

Easily run Jest integration tests that require a running
MongoDB server

Features

  • Superfast in watch mode (use --runInBand to prevent MongoDB from rebooting
    between test-runs)
  • Easy to set the storage engine per test file (use docblocks to override the
    default storage engine on a per file basis)
  • Includes TypeScript type annotations

Types

This package is written in TypeScript, type declarations (and source maps) are
included with the package so no need to install types separately. It also works
great with regular JavaScript.

Installation

NPM

  1. npm install --save-dev mongodb-memory-server jest-environment-mongodb

Yarn

  1. yarn add --dev mongodb-memory-server jest-environment-mongodb

Configuration - configuration file

To use jest-environment-mongodb as the default test environment, update your
Jest configuration as follows:

  1. {
  2. "testEnvironment": "mongodb"
  3. }

To set the test environment on a per file or file pattern basis, consider using
a docblock (see the next section) or configure
Jest projects.

Configuration - docblock

By adding a @jest-environment
docblock at
the top of a test file, you can specify mongodb to be used for all tests in
that file:

  1. /**
  2. * @jest-environment mongodb
  3. */

This overrides any environment set by testEnvironment in the Jest
configuration file.

For docblock usage,
jest-environment-mongodb-wiredtiger
and
jest-environment-mongodb-ephemeral
may come in handy.

Default mode

The default behavior of jest-environment-mongodb is to run one MongoDB server
per Jest worker and to set up a new database for each test suite. This means you
have complete isolation between test suites and parallel test runs (workers).
While great for running many tests (in for example CI), it is also slow,
especially if you need to apply indexes to your collections. This becomes
particularly painful if you use watch mode when developing. However, you can
change this behavior with --runInBand, which will keep a single server running
between test runs, see the next section.

TDD mode - keeping the MongoDB server alive between test suites

When starting Jest with the --runInBand (or the alias -i) CLI flag, a single
MongoDB server will be started and kept alive between test runs. This is useful
when running Jest in watch mode, as it prevents MongoDB from restarting between
every code change.

If you need to apply indexes to your DB before running tests, --runInBand will
give you an extra performance boost since you don’t need to recreate the indexes
for every code-change/test-run.

The MongoDB server is also kept alive and shared between test suites.

Just remember to clean up your database between each test and this should give a
nice performance boost when developing.

Example:

  1. jest --watch --runInBand mytests.test.js

MongoDB server options

Configure the MongoDB server by passing options to testEnvironmentOptions of
your Jest configuration file.

The available testEnvironmentOptions are the same as the
mongodb-memory-server
options.

Example:

  1. {
  2. "testEnvironment": "mongodb",
  3. "testEnvironmentOptions": {
  4. "binary": {
  5. "version": "3.6.5"
  6. },
  7. "instance": {
  8. "storageEngine": "wiredTiger"
  9. }
  10. }
  11. }

MongoDB options provided via testEnvironmentOptions also applies to docblock
environments.

Globals

The jest-environment-mongodb environment exposes three global variables:

  1. global.MONGO_URI // The server connection URI
  2. global.MONGO_DB_NAME // The database name
  3. global.MONGOD // The mongod instance from `mongodb-memory-server`

Usage

  1. /**
  2. * @jest-environment mongodb
  3. */
  4. import { MongoClient } from "mongodb";
  5. let client;
  6. let db;
  7. beforeAll(async () => {
  8. client = await MongoClient.connect(global.MONGO_URI);
  9. db = await client.db(global.MONGO_DB_NAME);
  10. });
  11. afterAll(async () => {
  12. await client.close();
  13. });
  14. beforeEach(async () => {
  15. // Reset the database before each test
  16. await db.dropDatabase();
  17. });
  18. it("should aggregate docs from collection", async () => {
  19. const files = db.collection("files");
  20. await files.insertMany([
  21. { type: "Document" },
  22. { type: "Video" },
  23. { type: "Image" },
  24. { type: "Document" },
  25. { type: "Image" },
  26. { type: "Document" },
  27. ]);
  28. const topFiles = await files
  29. .aggregate([
  30. { $group: { _id: "$type", count: { $sum: 1 } } },
  31. { $sort: { count: -1 } },
  32. ])
  33. .toArray();
  34. expect(topFiles).toEqual([
  35. { _id: "Document", count: 3 },
  36. { _id: "Image", count: 2 },
  37. { _id: "Video", count: 1 },
  38. ]);
  39. });

See also