Skip to content

Commit c5195f3

Browse files
committed
use uvu and c8 for testing
1 parent 4026d16 commit c5195f3

7 files changed

Lines changed: 108 additions & 113 deletions

File tree

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
"devDependencies": {
1010
"@hapi/code": "~8.0.4",
1111
"@hapi/lab": "~24.4.0",
12+
"c8": "~7.10.0",
1213
"eslint": "~7.27.0",
1314
"eslint-config-standard": "~16.0.3",
1415
"eslint-plugin-import": "~2.25.3",
1516
"eslint-plugin-node": "~11.1.0",
1617
"eslint-plugin-promise": "~5.1.1",
1718
"eslint-plugin-standard": "~4.1.0",
18-
"mongoose": "~6.0.13"
19+
"expect": "~27.3.1",
20+
"mongoose": "~6.0.13",
21+
"uvu": "~0.5.2"
1922
},
2023
"engines": {
2124
"node": ">=8"
@@ -34,7 +37,7 @@
3437
},
3538
"scripts": {
3639
"list-tests": "lab --assert @hapi/code --dry --verbose",
37-
"test": "lab --assert @hapi/code --leaks --coverage --lint --reporter console --output stdout --reporter html --output ./coverage/coverage.html",
38-
"test-single": "lab --assert @hapi/code --leaks --lint --id"
40+
"test": "c8 --include=dist uvu --ignore fixtures",
41+
"posttest": "c8 report --reporter=html"
3942
}
4043
}

test/custom-port.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

test/custom-port/custom-port.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
const { test } = require('uvu')
4+
const Mongoose = require('mongoose')
5+
6+
test('connects to MongoDB on custom port 12345', async () => {
7+
const connection = await Mongoose.createConnection('mongodb://localhost:12345').asPromise()
8+
await connection.close()
9+
})
10+
11+
test.run()

test/replica-set.js

Lines changed: 0 additions & 66 deletions
This file was deleted.

test/replica-set/replica-set.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict'
2+
3+
const { test } = require('uvu')
4+
const expect = require('expect')
5+
const Mongoose = require('mongoose')
6+
7+
const { MONGODB_PORT = 27017, MONGODB_REPLICA_SET = 'mongodb-test-rs' } = process.env
8+
9+
test.before(async () => {
10+
const connectionString = `mongodb://localhost:${MONGODB_PORT}/test?replicaSet=${MONGODB_REPLICA_SET}`
11+
12+
console.log('---------------------------------------------------------------------')
13+
console.log('connecting to MongoDB using connection string -> ' + connectionString)
14+
console.log('---------------------------------------------------------------------')
15+
16+
await Mongoose.connect(connectionString, {
17+
serverSelectionTimeoutMS: 1500
18+
})
19+
})
20+
21+
test.after(async () => {
22+
await Mongoose.connection.db.dropDatabase()
23+
await Mongoose.disconnect()
24+
})
25+
26+
test('queries the replica set status', async () => {
27+
const db = Mongoose.connection.db.admin()
28+
const { ok, set } = await db.command({ replSetGetStatus: 1 })
29+
30+
expect(ok).toBe(1)
31+
expect(set).toEqual(MONGODB_REPLICA_SET)
32+
})
33+
34+
test('saves a document', async () => {
35+
const Dog = Mongoose.model('Dog', { name: String })
36+
const albert = await new Dog({ name: 'Albert' }).save()
37+
expect(albert.name).toEqual('Albert')
38+
})
39+
40+
test('uses transactions', async () => {
41+
const Customer = Mongoose.model('Customer', new Mongoose.Schema({ name: String }))
42+
await Customer.createCollection()
43+
44+
const session = await Mongoose.startSession()
45+
session.startTransaction()
46+
47+
await Customer.create([{ name: 'test-customer' }], { session })
48+
49+
expect(
50+
await Customer.findOne({ name: 'test-customer' })
51+
).toBeNull()
52+
53+
expect(
54+
await Customer.findOne({ name: 'test-customer' }).session(session)
55+
).not.toBeNull()
56+
57+
await session.commitTransaction()
58+
59+
expect(
60+
await Customer.findOne({ name: 'test-customer' })
61+
).not.toBeNull()
62+
})
63+
64+
test.run()

test/single-instance.js

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict'
2+
3+
const { test } = require('uvu')
4+
const expect = require('expect')
5+
const Mongoose = require('mongoose')
6+
7+
test('connects to MongoDB', async () => {
8+
const connection = await Mongoose.createConnection('mongodb://localhost', {
9+
user: process.env.MONGODB_USERNAME,
10+
pass: process.env.MONGODB_PASSWORD,
11+
dbName: process.env.MONGODB_DB,
12+
authSource: 'admin'
13+
}).asPromise()
14+
15+
await connection.close()
16+
})
17+
18+
test('fails to connect to non-existent MongoDB instance', async () => {
19+
await expect(
20+
Mongoose.connect('mongodb://localhost:27018', {
21+
connectTimeoutMS: 1000,
22+
serverSelectionTimeoutMS: 1000
23+
})
24+
).rejects.toThrow()
25+
})
26+
27+
test.run()

0 commit comments

Comments
 (0)