- Getting Started
- Using Matchers
- Testing Asynchronous Code
- Setup and Teardown
- Mock Functions
- Jest Platform
- Jest Community
- More Resources
- Snapshot Testing
- An Async Example
- Timer Mocks
- Manual Mocks
- ES6 Class Mocks
- Bypassing module mocks
- Using with webpack
- Using with puppeteer
- Using with MongoDB
- Using with DynamoDB
- DOM Manipulation
- Watch Plugins
- Migrating to Jest
- Troubleshooting
- Architecture
- Testing React Apps
- Testing React Native Apps
- Testing Web Frameworks
- Expect
- Mock Functions
- The Jest Object
- Configuring Jest
- Jest CLI Options
- Globals
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
Using with MongoDB
With the Global Setup/Teardown and Async Test Environment APIs, Jest can work smoothly with MongoDB.
Use jest-mongodb Preset
Jest MongoDB provides all required configuration to run your tests using MongoDB.
- First install
@shelf/jest-mongodb
yarn add @shelf/jest-mongodb --dev
- Specify preset in your Jest configuration:
{
"preset": "@shelf/jest-mongodb"
}
- Write your test
const {MongoClient} = require('mongodb');
describe('insert', () => {
let connection;
let db;
beforeAll(async () => {
connection = await MongoClient.connect(global.__MONGO_URI__, {
useNewUrlParser: true,
});
db = await connection.db(global.__MONGO_DB_NAME__);
});
afterAll(async () => {
await connection.close();
await db.close();
});
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);
});
});
There's no need to load any dependencies.
See documentation for details (configuring MongoDB version, etc).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论