shelfio / jest-mongodb Goto Github PK
View Code? Open in Web Editor NEWJest preset for MongoDB in-memory server
License: MIT License
Jest preset for MongoDB in-memory server
License: MIT License
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?
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
When starting jest using the command "jest" the warn is displayed:
jest
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.
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?
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.
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.
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
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?
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.
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.
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?
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:
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?
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?
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?
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)
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?
How can I prevent/cache the mongodb 70MB download in a CI environment where the tests are run in on-demand docker containers?
mongo --version // MongoDB shell version v4.2.6
package.json
"dependencies": {
"mongodb": "^3.3.0",
}
jest-mongodb-config.json
module.exports = {
mongodbMemoryServerOptions: {
binary: {
version: "latest",
skipMD5: true,
},
autoStart: false,
instance: {},
},
};
npm test jest
, it starts downloading mongodbDetermining test suites to run...Downloading MongoDownloading MongoDB latest: 0.1 % (0.2mb / 310.2mb)
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.
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)
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
Line 11 in ea02ede
Line 3 in ea02ede
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?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"
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.
It would be cool to add a new section explaining the usage with mongoose
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!
I got Unknown linux distro elementary
message.
Elementary is based on Ubuntu. It doesn't difference to you
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?
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?
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!
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",
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__);
});
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
(node:7357) [MDEP001] DeprecationWarning: "MongoMemoryReplSet.getConnectionString" is deprecated, use ".getUri"
We should fix this warning
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.
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?
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.
Is it necessary to call the close()
for the second time?
https://github.com/vladgolubev/jest-mongodb/blob/master/mongo-insert.test.js#L14
I see in mongo example the call close()
only on mongoClient
object
https://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html#open
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;
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)
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
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',
],
},
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!
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.
Following readme, but got global.__MONGO_URI__
undefined.
Do we need other steps to setup global env?
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).
Hello ๐
this repository seems to have been removed from NPM, https://www.npmjs.com/package/@shelf/jest-mongodb, is that a mistake ?
I'd like to have a different database for each of my test
this setup make this possible?
Is it possible to launch a replica set for transaction testing?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.