-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration.test.js
More file actions
76 lines (66 loc) · 2.58 KB
/
integration.test.js
File metadata and controls
76 lines (66 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// tests/integration.test.js
const request = require('supertest');
const { MongoMemoryServer } = require('mongodb-memory-server');
const mongoose = require('mongoose');
let mongod;
let app; // will hold our Express app
beforeAll(async () => {
// 1️⃣ Start in‑memory MongoDB
mongod = await MongoMemoryServer.create();
process.env.MONGODB_URI = mongod.getUri() + 'hackai2025_test';
// 2️⃣ Now that MONGODB_URI is pointing at our in‑memory instance,
// import & connect your app (which uses operations.connectDB())
app = require('./index.js'); // import your Express app here
// Note: index.js must export the `app` instance (not just listen)
// Wait for Mongoose to connect
await mongoose.connection.asPromise();
});
afterAll(async () => {
// 1️⃣ Disconnect mongoose
await mongoose.disconnect();
// 2️⃣ Stop the in‑memory server
await mongod.stop();
});
describe('Full API integration (Mongoose)', () => {
let userId;
it('POST /api/users → 201 & returns new user', async () => {
const res = await request(app)
.post('/api/users')
.send({
name: { first: 'Test', last: 'User' },
address: '123 Main St',
age: 25,
gender: 'Other',
relationshipStatus:'Single'
});
expect(res.statusCode).toBe(201);
expect(res.body).toHaveProperty('_id');
userId = res.body._id;
});
it('POST /api/journal-entries → 201 & returns entry', async () => {
const res = await request(app)
.post('/api/journal-entries')
.send({
userId,
date: new Date().toISOString(),
keywords: ['mongo','jest'],
keywordFrequency: { mongo: 1, jest: 1 },
summary: 'Testing journal entry'
});
expect(res.statusCode).toBe(201);
expect(res.body).toMatchObject({ summary: 'Testing journal entry' });
});
it('POST /api/persons-of-interest → 201 & returns POI', async () => {
const res = await request(app)
.post('/api/persons-of-interest')
.send({
userId,
name: 'Alice POI',
age: 30,
relationship: 'Friend',
hostilityFactor: 2
});
expect(res.statusCode).toBe(201);
expect(res.body).toMatchObject({ name: 'Alice POI', hostilityFactor: 2 });
});
});