Comments (6)
Yea. You should be able to, I believe. mongoSetup
is just the variable name I assigned to require'@shelf/jest-mongodb/setup')
which is the default export of https://github.com/shelfio/jest-mongodb/blob/master/setup.js#L10
Given your initial function, it would look like:
const mongoSetup = require('@shelf/jest-mongodb/setup');
module.exports = async function globalSetup() {
await mongoSetup();
// process.env.MONGO_URL is now set by
// https://github.com/shelfio/jest-mongodb/blob/fcda766bfa433644a7383993467ccaa06dfea28b/setup.js#L26
await mongoose.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
});
};
from jest-mongodb.
I did as follows, but I am having trouble connecting to the database from my test files.
Here are the relevant files with their code.
jest.setup.js
import mongoose from "mongoose";
const mongoSetup = require("@shelf/jest-mongodb/setup");
module.exports = async () => {
await mongoSetup();
global.__DB__await = mongoose
.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.then(null, (err) => {
console.log(
`In-memory mongodb instance failed to initialize. Error: ${err}`
);
});
};
jest.teadown.js
import mongoose from "mongoose";
module.exports = async () => {
await mongoose.connection.close();
};
jest.config.js
module.exports = {
preset: "@shelf/jest-mongodb",
globalSetup: "<rootDir>/server/__tests__/__utils__/jest.setup.js",
globalTeardown: "<rootDir>/server/__tests__/__utils__/jest.teardown.js",
}
None of my tests that require a connection to mongodb to run successfully and pass.
Running npm run test -- --detectOpenHandles
reveals the following errors.
β TCPSERVERWRAP
710 | };
711 | let response = await request(app)
> 712 | .post("/api/users")
| ^
713 | .send(payload)
714 | .set("Accept", "application/json");
715 | expect(response.statusCode).toBe(200);
at Test.serverAddress (node_modules/supertest/lib/test.js:48:35)
at new Test (node_modules/supertest/lib/test.js:34:14)
at Object.post (node_modules/supertest/index.js:28:14)
at server/__tests__/controllers/user.controller.test.js:712:10
All tests exit with TCPSERVERWRAP
. npm run test
is test: "jest"
in the package.json file.
I have tried the block of async code in the jest.setup.js
and jest.teadown.js
files in a beforeAll()
and afterAll()
embedded into each the test files and it works. All tests pass when the db connection originates from the test files themselves.
Any guidance is highly appreciated.
from jest-mongodb.
This is achievable by calling setup
directly:
const mongoSetup = require('@shelf/jest-mongodb/setup');
module.exports = async config => {
await mongoSetup();
const url = process.env.MONGO_URL;
if(url == null) {
throw new Error("MONGO_URL was not set");
}
// connect
}
from jest-mongodb.
Confused @chanced. You're saying you can do this in jestβs globalSetup
? Where do you use the mongoSetup()
you described? Like I said in the original post, I'm able to connect just fine for each individual test suite, just not once globally which would be really nice.
from jest-mongodb.
Yea. You should be able to, I believe.
mongoSetup
is just the variable name I assigned torequire'@shelf/jest-mongodb/setup')
which is the default export of https://github.com/shelfio/jest-mongodb/blob/master/setup.js#L10Given your initial function, it would look like:
const mongoSetup = require('@shelf/jest-mongodb/setup'); module.exports = async function globalSetup() { await mongoSetup(); // process.env.MONGO_URL is now set by // https://github.com/shelfio/jest-mongodb/blob/fcda766bfa433644a7383993467ccaa06dfea28b/setup.js#L26 await mongoose.connect(process.env.MONGO_URL, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false, }); };
i still have the same error; can't connect to the database and then timeout error
from jest-mongodb.
Not clear if this will work due to https://mongoosejs.com/docs/jest.html#globalsetup-and-globalteardown
from jest-mongodb.
Related Issues (20)
- Jest: Got error running globalSetup - First run works - After Monorepo + Babel
- $unwind $lookup problems HOT 1
- Error in 4.1.3 - Cannot read properties of undefined (reading 'rootDir') HOT 2
- Atlas search directives are not supported HOT 1
- TypeError: Class extends value undefined is not a constructor or null HOT 8
- StdoutInstanceError: Instance failed to start because a library is missing or cannot be opened: "libcrypto.so.1.1" HOT 6
- ERESOLVE: Found: [email protected], peer mongodb@"3.x.x || 4.x" from @shelf/[email protected] HOT 1
- Need to update the documentation to reflect current 'mongodb' library HOT 2
- [Question] How to use views in tests?
- jest-environment-node peer dependency
- useSharedDBForAllJestWorkers: false is not working and all jest workers are getting the same database URI HOT 2
- Preset conflicting with jest --watch
- Upgrade dependencies for mongodb@6 and higher HOT 4
- jest --watchAll fails when having test suites without mongodb mocking
- Improvements Suggestions
- Improvements Suggestions
- Not working on Ubuntu 23.04 HOT 3
- Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature. HOT 2
- MongoWriteConcernError: operation was interrupted
- Jest runner never exits after completing tests HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jest-mongodb.