1 | # jest-mongodb [![CircleCI](https://circleci.com/gh/shelfio/jest-mongodb/tree/master.svg?style=svg)](https://circleci.com/gh/shelfio/jest-mongodb/tree/master) ![](https://img.shields.io/badge/code_style-prettier-ff69b4.svg) [![npm (scoped)](https://img.shields.io/npm/v/@shelf/jest-mongodb.svg)](https://www.npmjs.com/package/@shelf/jest-mongodb)
|
2 |
|
3 | > Jest preset to run MongoDB memory server
|
4 |
|
5 | ## Usage
|
6 |
|
7 | ### 0. Install
|
8 |
|
9 | ```
|
10 | $ yarn add @shelf/jest-mongodb --dev
|
11 | ```
|
12 |
|
13 | Make sure `mongodb` is installed in the project as well, as it's required as a peer dependency.
|
14 |
|
15 | ### 1. Create `jest.config.js`
|
16 |
|
17 | ```js
|
18 | module.exports = {
|
19 | preset: '@shelf/jest-mongodb',
|
20 | };
|
21 | ```
|
22 |
|
23 | If you have a custom `jest.config.js` make sure you remove `testEnvironment` property, otherwise it will conflict with the preset.
|
24 |
|
25 | ### 2. Create `jest-mongodb-config.js`
|
26 |
|
27 | See [mongodb-memory-server](https://github.com/nodkz/mongodb-memory-server#available-options)
|
28 |
|
29 | ```js
|
30 | module.exports = {
|
31 | mongodbMemoryServerOptions: {
|
32 | binary: {
|
33 | version: '4.0.3',
|
34 | skipMD5: true,
|
35 | },
|
36 | autoStart: false,
|
37 | instance: {},
|
38 | },
|
39 | };
|
40 | ```
|
41 |
|
42 | To use the same database for all tests pass the config like this:
|
43 |
|
44 | ```js
|
45 | module.exports = {
|
46 | mongodbMemoryServerOptions: {
|
47 | binary: {
|
48 | version: '4.0.3',
|
49 | skipMD5: true,
|
50 | },
|
51 | instance: {
|
52 | dbName: 'jest',
|
53 | },
|
54 | autoStart: false,
|
55 | },
|
56 | };
|
57 | ```
|
58 |
|
59 | To use separate database for each jest worker pass the `useSharedDBForAllJestWorkers: false` (doesn't create `process.env` variable when using this option):
|
60 |
|
61 | ```js
|
62 | module.exports = {
|
63 | mongodbMemoryServerOptions: {
|
64 | binary: {
|
65 | skipMD5: true,
|
66 | },
|
67 | autoStart: false,
|
68 | instance: {},
|
69 | },
|
70 |
|
71 | useSharedDBForAllJestWorkers: false,
|
72 | };
|
73 | ```
|
74 |
|
75 | To use dynamic database name you must pass empty object for instance field:
|
76 |
|
77 | ```js
|
78 | module.exports = {
|
79 | mongodbMemoryServerOptions: {
|
80 | binary: {
|
81 | version: '4.0.3',
|
82 | skipMD5: true,
|
83 | },
|
84 | instance: {},
|
85 | autoStart: false,
|
86 | },
|
87 | };
|
88 | ```
|
89 |
|
90 | To use another uri environment variable name you must set mongoURLEnvName field:
|
91 |
|
92 | ```js
|
93 | module.exports = {
|
94 | mongodbMemoryServerOptions: {
|
95 | binary: {
|
96 | version: '4.0.3',
|
97 | skipMD5: true,
|
98 | },
|
99 | instance: {},
|
100 | autoStart: false,
|
101 | },
|
102 | mongoURLEnvName: 'MONGODB_URI',
|
103 | };
|
104 | ```
|
105 |
|
106 | To use mongo as a replica set you must add the `replSet` config object and set
|
107 | `count` and `storageEngine` fields:
|
108 |
|
109 | ```js
|
110 | module.exports = {
|
111 | mongodbMemoryServerOptions: {
|
112 | binary: {
|
113 | skipMD5: true,
|
114 | },
|
115 | autoStart: false,
|
116 | instance: {},
|
117 | replSet: {
|
118 | count: 3,
|
119 | storageEngine: 'wiredTiger',
|
120 | },
|
121 | },
|
122 | };
|
123 | ```
|
124 |
|
125 | ### 3. Configure MongoDB client
|
126 |
|
127 | Library sets the `process.env.MONGO_URL` for your convenience, but using of `global.__MONGO_URI__` is preferable as it works with ` useSharedDBForAllJestWorkers: false`
|
128 |
|
129 | ```js
|
130 | const {MongoClient} = require('mongodb');
|
131 |
|
132 | describe('insert', () => {
|
133 | let connection;
|
134 | let db;
|
135 |
|
136 | beforeAll(async () => {
|
137 | connection = await MongoClient.connect(global.__MONGO_URI__, {
|
138 | useNewUrlParser: true,
|
139 | useUnifiedTopology: true,
|
140 | });
|
141 | db = await connection.db();
|
142 | });
|
143 |
|
144 | afterAll(async () => {
|
145 | await connection.close();
|
146 | });
|
147 | });
|
148 | ```
|
149 |
|
150 | ### 4. PROFIT! Write tests
|
151 |
|
152 | ```js
|
153 | it('should insert a doc into collection', async () => {
|
154 | const users = db.collection('users');
|
155 |
|
156 | const mockUser = {_id: 'some-user-id', name: 'John'};
|
157 | await users.insertOne(mockUser);
|
158 |
|
159 | const insertedUser = await users.findOne({_id: 'some-user-id'});
|
160 | expect(insertedUser).toEqual(mockUser);
|
161 | });
|
162 | ```
|
163 |
|
164 | Cache MongoDB binary in CI by putting this folder to the list of cached paths: `./node_modules/.cache/mongodb-memory-server/mongodb-binaries`
|
165 |
|
166 | You can enable debug logs by setting environment variable `DEBUG=jest-mongodb:*`
|
167 |
|
168 | #### 5. Clean collections before each test (optional)
|
169 |
|
170 | ```js
|
171 | beforeEach(async () => {
|
172 | await db.collection('COLLECTION_NAME').deleteMany({});
|
173 | });
|
174 | ```
|
175 |
|
176 | <sub>See [this issue](https://github.com/shelfio/jest-mongodb/issues/173) for discussion</sub>
|
177 |
|
178 | #### 6. Jest watch mode gotcha
|
179 |
|
180 | 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
|
181 |
|
182 | In order to avoid this unwanted behaviour, add `globalConfig` to ignored files in watch mode in the Jest configuation
|
183 |
|
184 | ```js
|
185 | // jest.config.js
|
186 | module.exports = {
|
187 | watchPathIgnorePatterns: ['globalConfig'],
|
188 | };
|
189 | ```
|
190 |
|
191 | ## See Also
|
192 |
|
193 | - [jest-dynamodb](https://github.com/shelfio/jest-dynamodb)
|
194 |
|
195 | ## Publish
|
196 |
|
197 | ```sh
|
198 | $ git checkout master
|
199 | $ yarn version
|
200 | $ yarn publish
|
201 | $ git push origin master --tags
|
202 | ```
|
203 |
|
204 | ## License
|
205 |
|
206 | MIT © [Shelf](https://shelf.io)
|