ts-patch-mongoose 2.6.6 → 2.6.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -2
- package/dist/cjs/hooks/delete-hooks.js.map +1 -1
- package/dist/cjs/types/models/History.d.ts +3 -3
- package/dist/esm/hooks/delete-hooks.js.map +1 -1
- package/dist/esm/types/models/History.d.ts +3 -3
- package/package.json +11 -13
- package/src/hooks/delete-hooks.ts +2 -2
- package/tests/em.test.ts +4 -2
- package/tests/mongo/.gitignore +3 -0
- package/tests/mongo/server.ts +32 -0
- package/tests/patch.test.ts +14 -9
- package/tests/plugin-event-created.test.ts +14 -9
- package/tests/plugin-event-deleted.test.ts +19 -9
- package/tests/plugin-event-updated.test.ts +14 -9
- package/tests/plugin-global.test.ts +13 -7
- package/tests/plugin-omit-all.test.ts +13 -6
- package/tests/plugin-patch-history-disabled.test.ts +13 -6
- package/tests/plugin-pre-delete.test.ts +13 -7
- package/tests/plugin-pre-save.test.ts +12 -6
- package/tests/plugin.test.ts +15 -7
- package/vite.config.mts +12 -0
- package/jest-mongodb-config.ts +0 -10
- package/jest.config.ts +0 -18
package/README.md
CHANGED
|
@@ -13,8 +13,6 @@ Patch history (audit log) & events plugin for mongoose
|
|
|
13
13
|
[](https://sonarcloud.io/summary/new_code?id=ilovepixelart_ts-patch-mongoose)
|
|
14
14
|
[](https://sonarcloud.io/summary/new_code?id=ilovepixelart_ts-patch-mongoose)
|
|
15
15
|
|
|
16
|
-
[](https://www.npmjs.com/package/ts-patch-mongoose)
|
|
17
|
-
|
|
18
16
|
## Motivation
|
|
19
17
|
|
|
20
18
|
ts-patch-mongoose is a plugin for mongoose
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"delete-hooks.js","sourceRoot":"","sources":["../../../src/hooks/delete-hooks.ts"],"names":[],"mappings":";;;;AAAA,4DAAsB;AAEtB,wCAA0C;AAC1C,oCAAsC;AAMtC,MAAM,aAAa,G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AAC,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAA;YACxD,IAAI,CAAC,gBAAC,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;gBACpB,IAAI,CAAC,QAAQ,CAAC,WAAW,GAAG,CAAC,GAAG,CAA0B,CAAA;YAC5D,CAAC;QACH,CAAC;QAED,IAAI,IAAI,CAAC,SAAS,IAAI,gBAAC,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,gBAAC,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC;YACpG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAA;QACjD,CAAC;IACH,CAAC,CAAC,CAAA;IAEF,MAAM,CAAC,IAAI,CAAC,aAA0C,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,KAAK;QAC7F,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,EAAE,CAAA;QACjC,IAAI,IAAA,uBAAa,EAAC,OAAO,CAAC;YAAE,OAAM;QAElC,MAAM,IAAA,mBAAW,EAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;IACxC,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA;AAvCY,QAAA,qBAAqB,yBAuCjC"}
|
|
@@ -3,15 +3,15 @@ import type IHistory from '../interfaces/IHistory';
|
|
|
3
3
|
declare const History: import("mongoose").Model<IHistory, {}, {}, {}, import("mongoose").Document<unknown, {}, IHistory> & IHistory & {
|
|
4
4
|
_id: import("mongoose").Types.ObjectId;
|
|
5
5
|
} & {
|
|
6
|
-
__v
|
|
6
|
+
__v: number;
|
|
7
7
|
}, Schema<IHistory, import("mongoose").Model<IHistory, any, any, any, import("mongoose").Document<unknown, any, IHistory> & IHistory & {
|
|
8
8
|
_id: import("mongoose").Types.ObjectId;
|
|
9
9
|
} & {
|
|
10
|
-
__v
|
|
10
|
+
__v: number;
|
|
11
11
|
}, any>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, IHistory, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<IHistory>> & import("mongoose").FlatRecord<IHistory> & {
|
|
12
12
|
_id: import("mongoose").Types.ObjectId;
|
|
13
13
|
} & {
|
|
14
|
-
__v
|
|
14
|
+
__v: number;
|
|
15
15
|
}>>;
|
|
16
16
|
export default History;
|
|
17
17
|
//# sourceMappingURL=History.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"delete-hooks.js","sourceRoot":"","sources":["../../../src/hooks/delete-hooks.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,QAAQ,CAAA;AAEtB,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AAMtC,MAAM,aAAa,GAAG,CAAC,QAAQ,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,WAAW,EAAE,YAAY,CAAC,CAAA;AAE7I,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAI,MAAiB,EAAE,IAAuB,EAAQ,EAAE;IAC3F,MAAM,CAAC,GAAG,CAAC,aAA0C,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,KAAK;QAC5F,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,EAAE,CAAA;QACjC,IAAI,aAAa,CAAC,OAAO,CAAC;YAAE,OAAM;QAElC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAiB,CAAA;QACpC,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,EAAE,CAAA;QAE/B,IAAI,CAAC,QAAQ,GAAG;YACd,EAAE,EAAE,IAAI,CAAC,EAAE;YACX,SAAS,EAAE,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,K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0C,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,KAAK;QAC7F,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,EAAE,CAAA;QACjC,IAAI,aAAa,CAAC,OAAO,CAAC;YAAE,OAAM;QAElC,MAAM,WAAW,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;IACxC,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA"}
|
|
@@ -3,15 +3,15 @@ import type IHistory from '../interfaces/IHistory';
|
|
|
3
3
|
declare const History: import("mongoose").Model<IHistory, {}, {}, {}, import("mongoose").Document<unknown, {}, IHistory> & IHistory & {
|
|
4
4
|
_id: import("mongoose").Types.ObjectId;
|
|
5
5
|
} & {
|
|
6
|
-
__v
|
|
6
|
+
__v: number;
|
|
7
7
|
}, Schema<IHistory, import("mongoose").Model<IHistory, any, any, any, import("mongoose").Document<unknown, any, IHistory> & IHistory & {
|
|
8
8
|
_id: import("mongoose").Types.ObjectId;
|
|
9
9
|
} & {
|
|
10
|
-
__v
|
|
10
|
+
__v: number;
|
|
11
11
|
}, any>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, IHistory, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<IHistory>> & import("mongoose").FlatRecord<IHistory> & {
|
|
12
12
|
_id: import("mongoose").Types.ObjectId;
|
|
13
13
|
} & {
|
|
14
|
-
__v
|
|
14
|
+
__v: number;
|
|
15
15
|
}>>;
|
|
16
16
|
export default History;
|
|
17
17
|
//# sourceMappingURL=History.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-patch-mongoose",
|
|
3
|
-
"version": "2.6.
|
|
3
|
+
"version": "2.6.7",
|
|
4
4
|
"description": "Patch history & events for mongoose models",
|
|
5
5
|
"author": "Alex Eagle",
|
|
6
6
|
"license": "MIT",
|
|
@@ -45,8 +45,7 @@
|
|
|
45
45
|
"tests",
|
|
46
46
|
".swcrc",
|
|
47
47
|
"tsconfig.json",
|
|
48
|
-
"
|
|
49
|
-
"jest-mongodb-config.ts",
|
|
48
|
+
"vite.config.mts",
|
|
50
49
|
"biome.json"
|
|
51
50
|
],
|
|
52
51
|
"exports": {
|
|
@@ -67,8 +66,8 @@
|
|
|
67
66
|
"scripts": {
|
|
68
67
|
"biome": "npx @biomejs/biome check",
|
|
69
68
|
"biome:fix": "npx @biomejs/biome check --write .",
|
|
70
|
-
"test": "
|
|
71
|
-
"test:open": "
|
|
69
|
+
"test": "vitest run --coverage",
|
|
70
|
+
"test:open": "vitest run --coverage && open-cli coverage/lcov-report/index.html",
|
|
72
71
|
"clean": "rm -rf ./dist",
|
|
73
72
|
"build": "npm run clean && npm run build:cjs && npm run build:esm",
|
|
74
73
|
"build:cjs": "tsc -p config/tsconfig.cjs.json",
|
|
@@ -84,17 +83,16 @@
|
|
|
84
83
|
},
|
|
85
84
|
"devDependencies": {
|
|
86
85
|
"@biomejs/biome": "1.9.4",
|
|
87
|
-
"@
|
|
88
|
-
"@
|
|
89
|
-
"@types/jest": "29.5.14",
|
|
90
|
-
"@types/lodash": "4.17.12",
|
|
91
|
-
"@types/node": "22.8.1",
|
|
86
|
+
"@types/lodash": "4.17.13",
|
|
87
|
+
"@types/node": "22.10.1",
|
|
92
88
|
"@types/semver": "7.5.8",
|
|
93
|
-
"
|
|
89
|
+
"@vitest/coverage-v8": "2.1.8",
|
|
94
90
|
"merge": "2.1.1",
|
|
95
|
-
"
|
|
91
|
+
"mongodb-memory-server": "10.1.2",
|
|
92
|
+
"mongoose": "8.8.3",
|
|
96
93
|
"open-cli": "8.0.0",
|
|
97
|
-
"typescript": "5.
|
|
94
|
+
"typescript": "5.7.2",
|
|
95
|
+
"vitest": "2.1.8"
|
|
98
96
|
},
|
|
99
97
|
"peerDependencies": {
|
|
100
98
|
"mongoose": ">=6.6.0 < 9"
|
|
@@ -26,12 +26,12 @@ export const deleteHooksInitialize = <T>(schema: Schema<T>, opts: IPluginOptions
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
if (['remove', 'deleteMany'].includes(this._context.op) && !options.single) {
|
|
29
|
-
const docs = await model.find(filter).lean().exec()
|
|
29
|
+
const docs = await model.find<T>(filter).lean().exec()
|
|
30
30
|
if (!_.isEmpty(docs)) {
|
|
31
31
|
this._context.deletedDocs = docs as HydratedDocument<T>[]
|
|
32
32
|
}
|
|
33
33
|
} else {
|
|
34
|
-
const doc = await model.findOne(filter).lean().exec()
|
|
34
|
+
const doc = await model.findOne<T>(filter).lean().exec()
|
|
35
35
|
if (!_.isEmpty(doc)) {
|
|
36
36
|
this._context.deletedDocs = [doc] as HydratedDocument<T>[]
|
|
37
37
|
}
|
package/tests/em.test.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
2
|
+
|
|
1
3
|
import { emitEvent } from '../src/patch'
|
|
2
4
|
import { patchEventEmitter } from '../src/plugin'
|
|
3
5
|
|
|
@@ -16,7 +18,7 @@ describe('em', () => {
|
|
|
16
18
|
})
|
|
17
19
|
|
|
18
20
|
it('emitEvent', async () => {
|
|
19
|
-
const fn =
|
|
21
|
+
const fn = vi.fn()
|
|
20
22
|
patchEventEmitter.on('test', fn)
|
|
21
23
|
|
|
22
24
|
const context = {
|
|
@@ -32,7 +34,7 @@ describe('em', () => {
|
|
|
32
34
|
})
|
|
33
35
|
|
|
34
36
|
it('emitEvent ignore', async () => {
|
|
35
|
-
const fn =
|
|
37
|
+
const fn = vi.fn()
|
|
36
38
|
patchEventEmitter.on('test', fn)
|
|
37
39
|
|
|
38
40
|
const context = {
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import mongoose from 'mongoose'
|
|
3
|
+
|
|
4
|
+
import { MongoMemoryServer } from 'mongodb-memory-server'
|
|
5
|
+
|
|
6
|
+
const server = (dbName: string) => {
|
|
7
|
+
let mongo: MongoMemoryServer
|
|
8
|
+
const dbPath = `./tests/mongo/${dbName}`
|
|
9
|
+
|
|
10
|
+
const create = async () => {
|
|
11
|
+
fs.mkdirSync(dbPath, { recursive: true })
|
|
12
|
+
mongo = await MongoMemoryServer.create({
|
|
13
|
+
instance: {
|
|
14
|
+
dbName,
|
|
15
|
+
dbPath,
|
|
16
|
+
},
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
const uri = mongo.getUri()
|
|
20
|
+
await mongoose.connect(uri)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const destroy = async () => {
|
|
24
|
+
await mongoose.connection.dropDatabase()
|
|
25
|
+
await mongoose.connection.close()
|
|
26
|
+
await mongo.stop({ doCleanup: true, force: true })
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return { create, destroy }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export default server
|
package/tests/patch.test.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
|
|
1
3
|
import mongoose, { model } from 'mongoose'
|
|
2
4
|
|
|
3
5
|
import { bulkPatch, getData, getJsonOmit, getMetadata, getReason, getUser, getValue, updatePatch } from '../src/patch'
|
|
@@ -13,16 +15,15 @@ import type { User } from '../src/interfaces/IPluginOptions'
|
|
|
13
15
|
import type IPluginOptions from '../src/interfaces/IPluginOptions'
|
|
14
16
|
import type IUser from './interfaces/IUser'
|
|
15
17
|
|
|
18
|
+
import { afterEach } from 'node:test'
|
|
19
|
+
import { update } from 'lodash'
|
|
16
20
|
import em from '../src/em'
|
|
21
|
+
import server from './mongo/server'
|
|
17
22
|
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
emit: jest.fn(),
|
|
21
|
-
}
|
|
22
|
-
})
|
|
23
|
+
vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
|
|
23
24
|
|
|
24
25
|
describe('patch tests', () => {
|
|
25
|
-
const
|
|
26
|
+
const instance = server('patch')
|
|
26
27
|
|
|
27
28
|
UserSchema.plugin(patchHistoryPlugin, {
|
|
28
29
|
eventDeleted: USER_DELETED,
|
|
@@ -32,11 +33,11 @@ describe('patch tests', () => {
|
|
|
32
33
|
const User = model('User', UserSchema)
|
|
33
34
|
|
|
34
35
|
beforeAll(async () => {
|
|
35
|
-
await
|
|
36
|
+
await instance.create()
|
|
36
37
|
})
|
|
37
38
|
|
|
38
39
|
afterAll(async () => {
|
|
39
|
-
await
|
|
40
|
+
await instance.destroy()
|
|
40
41
|
})
|
|
41
42
|
|
|
42
43
|
beforeEach(async () => {
|
|
@@ -44,6 +45,10 @@ describe('patch tests', () => {
|
|
|
44
45
|
await mongoose.connection.collection('patches').deleteMany({})
|
|
45
46
|
})
|
|
46
47
|
|
|
48
|
+
afterEach(async () => {
|
|
49
|
+
vi.restoreAllMocks()
|
|
50
|
+
})
|
|
51
|
+
|
|
47
52
|
describe('getObjects', () => {
|
|
48
53
|
it('should omit properties from currentObject and originalObject based on the opts', async () => {
|
|
49
54
|
const original = await User.create({ name: 'John', role: 'user' })
|
|
@@ -140,7 +145,7 @@ describe('patch tests', () => {
|
|
|
140
145
|
}
|
|
141
146
|
|
|
142
147
|
await updatePatch(pluginOptions, context, current, {} as HydratedDocument<IUser>)
|
|
143
|
-
expect(em.emit).
|
|
148
|
+
expect(em.emit).toHaveBeenCalled()
|
|
144
149
|
})
|
|
145
150
|
})
|
|
146
151
|
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
|
|
1
3
|
import mongoose, { Types, model } from 'mongoose'
|
|
2
4
|
import { isMongooseLessThan7 } from '../src/version'
|
|
3
5
|
|
|
@@ -5,17 +7,15 @@ import History from '../src/models/History'
|
|
|
5
7
|
import { patchHistoryPlugin } from '../src/plugin'
|
|
6
8
|
import UserSchema from './schemas/UserSchema'
|
|
7
9
|
|
|
8
|
-
import em from '../src/em'
|
|
9
10
|
import { USER_CREATED } from './constants/events'
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
})
|
|
12
|
+
import em from '../src/em'
|
|
13
|
+
import server from './mongo/server'
|
|
14
|
+
|
|
15
|
+
vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
|
|
16
16
|
|
|
17
17
|
describe('plugin - event created & patch history disabled', () => {
|
|
18
|
-
const
|
|
18
|
+
const instance = server('plugin-event-created')
|
|
19
19
|
|
|
20
20
|
UserSchema.plugin(patchHistoryPlugin, {
|
|
21
21
|
eventCreated: USER_CREATED,
|
|
@@ -25,11 +25,11 @@ describe('plugin - event created & patch history disabled', () => {
|
|
|
25
25
|
const User = model('User', UserSchema)
|
|
26
26
|
|
|
27
27
|
beforeAll(async () => {
|
|
28
|
-
await
|
|
28
|
+
await instance.create()
|
|
29
29
|
})
|
|
30
30
|
|
|
31
31
|
afterAll(async () => {
|
|
32
|
-
await
|
|
32
|
+
await instance.destroy()
|
|
33
33
|
})
|
|
34
34
|
|
|
35
35
|
beforeEach(async () => {
|
|
@@ -37,6 +37,10 @@ describe('plugin - event created & patch history disabled', () => {
|
|
|
37
37
|
await mongoose.connection.collection('history').deleteMany({})
|
|
38
38
|
})
|
|
39
39
|
|
|
40
|
+
afterEach(() => {
|
|
41
|
+
vi.restoreAllMocks()
|
|
42
|
+
})
|
|
43
|
+
|
|
40
44
|
describe('normal cases', () => {
|
|
41
45
|
it('should save() and emit one create event', async () => {
|
|
42
46
|
const john = new User({ name: 'John', role: 'user' })
|
|
@@ -151,6 +155,7 @@ describe('plugin - event created & patch history disabled', () => {
|
|
|
151
155
|
describe('upsert cases', () => {
|
|
152
156
|
it('should update() + upsert and emit one create event', async () => {
|
|
153
157
|
if (isMongooseLessThan7) {
|
|
158
|
+
// @ts-expect-error update() not available in Mongoose v6 and below
|
|
154
159
|
await User.update({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
|
|
155
160
|
} else {
|
|
156
161
|
await User.findOneAndUpdate({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
|
|
1
3
|
import mongoose, { model } from 'mongoose'
|
|
2
4
|
import { isMongooseLessThan7 } from '../src/version'
|
|
3
5
|
|
|
@@ -5,18 +7,16 @@ import History from '../src/models/History'
|
|
|
5
7
|
import { patchHistoryPlugin } from '../src/plugin'
|
|
6
8
|
import UserSchema from './schemas/UserSchema'
|
|
7
9
|
|
|
8
|
-
import em from '../src/em'
|
|
9
10
|
import { toObjectOptions } from '../src/helpers'
|
|
10
11
|
import { USER_DELETED } from './constants/events'
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
})
|
|
13
|
+
import em from '../src/em'
|
|
14
|
+
import server from './mongo/server'
|
|
15
|
+
|
|
16
|
+
vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
|
|
17
17
|
|
|
18
18
|
describe('plugin - event delete & patch history disabled', () => {
|
|
19
|
-
const
|
|
19
|
+
const instance = server('plugin-event-deleted')
|
|
20
20
|
|
|
21
21
|
UserSchema.plugin(patchHistoryPlugin, {
|
|
22
22
|
eventDeleted: USER_DELETED,
|
|
@@ -26,11 +26,11 @@ describe('plugin - event delete & patch history disabled', () => {
|
|
|
26
26
|
const User = model('User', UserSchema)
|
|
27
27
|
|
|
28
28
|
beforeAll(async () => {
|
|
29
|
-
await
|
|
29
|
+
await instance.create()
|
|
30
30
|
})
|
|
31
31
|
|
|
32
32
|
afterAll(async () => {
|
|
33
|
-
await
|
|
33
|
+
await instance.destroy()
|
|
34
34
|
})
|
|
35
35
|
|
|
36
36
|
beforeEach(async () => {
|
|
@@ -38,10 +38,15 @@ describe('plugin - event delete & patch history disabled', () => {
|
|
|
38
38
|
await mongoose.connection.collection('history').deleteMany({})
|
|
39
39
|
})
|
|
40
40
|
|
|
41
|
+
afterEach(async () => {
|
|
42
|
+
vi.resetAllMocks()
|
|
43
|
+
})
|
|
44
|
+
|
|
41
45
|
it('should remove() and emit one delete event', async () => {
|
|
42
46
|
const john = await User.create({ name: 'John', role: 'user' })
|
|
43
47
|
|
|
44
48
|
if (isMongooseLessThan7) {
|
|
49
|
+
// @ts-expect-error not available in Mongoose 6 and below
|
|
45
50
|
await john.remove()
|
|
46
51
|
} else {
|
|
47
52
|
await john.deleteOne()
|
|
@@ -70,6 +75,7 @@ describe('plugin - event delete & patch history disabled', () => {
|
|
|
70
75
|
const [john, alice] = users
|
|
71
76
|
|
|
72
77
|
if (isMongooseLessThan7) {
|
|
78
|
+
// @ts-expect-error not available in Mongoose 6 and below
|
|
73
79
|
await User.remove({ role: 'user' }).exec()
|
|
74
80
|
} else {
|
|
75
81
|
await User.deleteMany({ role: 'user' }).exec()
|
|
@@ -107,6 +113,7 @@ describe('plugin - event delete & patch history disabled', () => {
|
|
|
107
113
|
const [john] = users
|
|
108
114
|
|
|
109
115
|
if (isMongooseLessThan7) {
|
|
116
|
+
// @ts-expect-error not available in Mongoose 6 and below
|
|
110
117
|
await User.remove({ role: 'user', name: 'John' }, { single: true }).exec()
|
|
111
118
|
} else {
|
|
112
119
|
await User.deleteOne({ role: 'user', name: 'John' }).exec()
|
|
@@ -165,6 +172,7 @@ describe('plugin - event delete & patch history disabled', () => {
|
|
|
165
172
|
const [john] = users
|
|
166
173
|
|
|
167
174
|
if (isMongooseLessThan7) {
|
|
175
|
+
// @ts-expect-error not available in Mongoose 6 and below
|
|
168
176
|
await User.findOneAndRemove({ role: 'user' }).exec()
|
|
169
177
|
} else {
|
|
170
178
|
await User.findOneAndDelete({ role: 'user' }).exec()
|
|
@@ -223,6 +231,7 @@ describe('plugin - event delete & patch history disabled', () => {
|
|
|
223
231
|
const [john] = users
|
|
224
232
|
|
|
225
233
|
if (isMongooseLessThan7) {
|
|
234
|
+
// @ts-expect-error not available in Mongoose 6 and below
|
|
226
235
|
await User.findByIdAndRemove(john._id).exec()
|
|
227
236
|
} else {
|
|
228
237
|
await User.findByIdAndDelete(john._id).exec()
|
|
@@ -339,6 +348,7 @@ describe('plugin - event delete & patch history disabled', () => {
|
|
|
339
348
|
const john = await User.create({ name: 'John', role: 'user' })
|
|
340
349
|
|
|
341
350
|
if (isMongooseLessThan7) {
|
|
351
|
+
// @ts-expect-error not available in Mongoose 6 and below
|
|
342
352
|
await john.delete()
|
|
343
353
|
} else {
|
|
344
354
|
await john.deleteOne()
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
|
|
1
3
|
import { isMongooseLessThan7 } from '../src/version'
|
|
2
4
|
|
|
3
5
|
import mongoose, { Types, model } from 'mongoose'
|
|
@@ -6,17 +8,15 @@ import History from '../src/models/History'
|
|
|
6
8
|
import { patchHistoryPlugin } from '../src/plugin'
|
|
7
9
|
import UserSchema from './schemas/UserSchema'
|
|
8
10
|
|
|
9
|
-
import em from '../src/em'
|
|
10
11
|
import { USER_UPDATED } from './constants/events'
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
})
|
|
13
|
+
import em from '../src/em'
|
|
14
|
+
import server from './mongo/server'
|
|
15
|
+
|
|
16
|
+
vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
|
|
17
17
|
|
|
18
18
|
describe('plugin - event updated & patch history disabled', () => {
|
|
19
|
-
const
|
|
19
|
+
const instance = server('plugin-event-updated')
|
|
20
20
|
|
|
21
21
|
UserSchema.plugin(patchHistoryPlugin, {
|
|
22
22
|
eventUpdated: USER_UPDATED,
|
|
@@ -27,11 +27,11 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
27
27
|
const User = model('User', UserSchema)
|
|
28
28
|
|
|
29
29
|
beforeAll(async () => {
|
|
30
|
-
await
|
|
30
|
+
await instance.create()
|
|
31
31
|
})
|
|
32
32
|
|
|
33
33
|
afterAll(async () => {
|
|
34
|
-
await
|
|
34
|
+
await instance.destroy()
|
|
35
35
|
})
|
|
36
36
|
|
|
37
37
|
beforeEach(async () => {
|
|
@@ -39,6 +39,10 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
39
39
|
await mongoose.connection.collection('history').deleteMany({})
|
|
40
40
|
})
|
|
41
41
|
|
|
42
|
+
afterEach(async () => {
|
|
43
|
+
vi.resetAllMocks()
|
|
44
|
+
})
|
|
45
|
+
|
|
42
46
|
it('should save() and emit one update event', async () => {
|
|
43
47
|
await User.create({ name: 'Bob', role: 'user' })
|
|
44
48
|
const user = new User({ name: 'John', role: 'user' })
|
|
@@ -94,6 +98,7 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
94
98
|
)
|
|
95
99
|
|
|
96
100
|
if (isMongooseLessThan7) {
|
|
101
|
+
// @ts-expect-error not available in Mongoose 6 and below
|
|
97
102
|
await User.update({ role: 'user' }, { role: 'manager' })
|
|
98
103
|
} else {
|
|
99
104
|
await User.updateMany({ role: 'user' }, { role: 'manager' })
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
|
|
1
3
|
import mongoose from 'mongoose'
|
|
2
4
|
|
|
3
5
|
import History from '../src/models/History'
|
|
@@ -5,15 +7,15 @@ import { patchHistoryPlugin } from '../src/plugin'
|
|
|
5
7
|
import ProductSchema from './schemas/ProductSchema'
|
|
6
8
|
import UserSchema from './schemas/UserSchema'
|
|
7
9
|
|
|
8
|
-
import em from '../src/em'
|
|
9
10
|
import { GLOBAL_CREATED, GLOBAL_DELETED, GLOBAL_UPDATED } from './constants/events'
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
import em from '../src/em'
|
|
13
|
+
import server from './mongo/server'
|
|
14
|
+
|
|
15
|
+
vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
|
|
14
16
|
|
|
15
17
|
describe('plugin - global', () => {
|
|
16
|
-
const
|
|
18
|
+
const instance = server('plugin-global')
|
|
17
19
|
|
|
18
20
|
mongoose.plugin(patchHistoryPlugin, {
|
|
19
21
|
eventCreated: GLOBAL_CREATED,
|
|
@@ -26,11 +28,11 @@ describe('plugin - global', () => {
|
|
|
26
28
|
const Product = mongoose.model('Product', ProductSchema)
|
|
27
29
|
|
|
28
30
|
beforeAll(async () => {
|
|
29
|
-
await
|
|
31
|
+
await instance.create()
|
|
30
32
|
})
|
|
31
33
|
|
|
32
34
|
afterAll(async () => {
|
|
33
|
-
await
|
|
35
|
+
await instance.destroy()
|
|
34
36
|
})
|
|
35
37
|
|
|
36
38
|
beforeEach(async () => {
|
|
@@ -39,6 +41,10 @@ describe('plugin - global', () => {
|
|
|
39
41
|
await mongoose.connection.collection('history').deleteMany({})
|
|
40
42
|
})
|
|
41
43
|
|
|
44
|
+
afterEach(async () => {
|
|
45
|
+
vi.restoreAllMocks()
|
|
46
|
+
})
|
|
47
|
+
|
|
42
48
|
it('should save array', async () => {
|
|
43
49
|
const product = await Product.create({ name: 'paper', groups: [] })
|
|
44
50
|
expect(product.name).toBe('paper')
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
|
|
1
3
|
import { isMongooseLessThan7 } from '../src/version'
|
|
2
4
|
|
|
3
5
|
import mongoose, { Types, model } from 'mongoose'
|
|
@@ -7,13 +9,12 @@ import { patchHistoryPlugin } from '../src/plugin'
|
|
|
7
9
|
import UserSchema from './schemas/UserSchema'
|
|
8
10
|
|
|
9
11
|
import em from '../src/em'
|
|
12
|
+
import server from './mongo/server'
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
return { emit: jest.fn() }
|
|
13
|
-
})
|
|
14
|
+
vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
|
|
14
15
|
|
|
15
16
|
describe('plugin - omit all', () => {
|
|
16
|
-
const
|
|
17
|
+
const instance = server('plugin-omit-all')
|
|
17
18
|
|
|
18
19
|
UserSchema.plugin(patchHistoryPlugin, {
|
|
19
20
|
omit: ['__v', 'name', 'role', 'createdAt', 'updatedAt'],
|
|
@@ -22,11 +23,11 @@ describe('plugin - omit all', () => {
|
|
|
22
23
|
const User = model('User', UserSchema)
|
|
23
24
|
|
|
24
25
|
beforeAll(async () => {
|
|
25
|
-
await
|
|
26
|
+
await instance.create()
|
|
26
27
|
})
|
|
27
28
|
|
|
28
29
|
afterAll(async () => {
|
|
29
|
-
await
|
|
30
|
+
await instance.destroy()
|
|
30
31
|
})
|
|
31
32
|
|
|
32
33
|
beforeEach(async () => {
|
|
@@ -34,6 +35,10 @@ describe('plugin - omit all', () => {
|
|
|
34
35
|
await mongoose.connection.collection('history').deleteMany({})
|
|
35
36
|
})
|
|
36
37
|
|
|
38
|
+
afterEach(async () => {
|
|
39
|
+
vi.resetAllMocks()
|
|
40
|
+
})
|
|
41
|
+
|
|
37
42
|
it('should createHistory', async () => {
|
|
38
43
|
const user = await User.create({ name: 'John', role: 'user' })
|
|
39
44
|
expect(user.name).toBe('John')
|
|
@@ -167,6 +172,7 @@ describe('plugin - omit all', () => {
|
|
|
167
172
|
expect(user.name).toBe('John')
|
|
168
173
|
|
|
169
174
|
if (isMongooseLessThan7) {
|
|
175
|
+
// @ts-expect-error not available in Mongoose 6 and below
|
|
170
176
|
await User.update({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
|
|
171
177
|
} else {
|
|
172
178
|
await User.findOneAndUpdate({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
|
|
@@ -200,6 +206,7 @@ describe('plugin - omit all', () => {
|
|
|
200
206
|
expect(alice.name).toBe('Alice')
|
|
201
207
|
|
|
202
208
|
if (isMongooseLessThan7) {
|
|
209
|
+
// @ts-expect-error not available in Mongoose 6 and below
|
|
203
210
|
await User.update({ role: 'user' }, { $set: { name: 'Bob' } }, { multi: true }).exec()
|
|
204
211
|
} else {
|
|
205
212
|
await User.updateMany({ role: 'user' }, { $set: { name: 'Bob' } }).exec()
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
|
|
1
3
|
import { isMongooseLessThan7 } from '../src/version'
|
|
2
4
|
|
|
3
5
|
import mongoose, { model } from 'mongoose'
|
|
@@ -7,13 +9,12 @@ import { patchHistoryPlugin } from '../src/plugin'
|
|
|
7
9
|
import UserSchema from './schemas/UserSchema'
|
|
8
10
|
|
|
9
11
|
import em from '../src/em'
|
|
12
|
+
import server from './mongo/server'
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
return { emit: jest.fn() }
|
|
13
|
-
})
|
|
14
|
+
vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
|
|
14
15
|
|
|
15
16
|
describe('plugin - patch history disabled', () => {
|
|
16
|
-
const
|
|
17
|
+
const instance = server('plugin-patch-history-disabled')
|
|
17
18
|
|
|
18
19
|
UserSchema.plugin(patchHistoryPlugin, {
|
|
19
20
|
patchHistoryDisabled: true,
|
|
@@ -22,11 +23,11 @@ describe('plugin - patch history disabled', () => {
|
|
|
22
23
|
const User = model('User', UserSchema)
|
|
23
24
|
|
|
24
25
|
beforeAll(async () => {
|
|
25
|
-
await
|
|
26
|
+
await instance.create()
|
|
26
27
|
})
|
|
27
28
|
|
|
28
29
|
afterAll(async () => {
|
|
29
|
-
await
|
|
30
|
+
await instance.destroy()
|
|
30
31
|
})
|
|
31
32
|
|
|
32
33
|
beforeEach(async () => {
|
|
@@ -34,6 +35,10 @@ describe('plugin - patch history disabled', () => {
|
|
|
34
35
|
await mongoose.connection.collection('history').deleteMany({})
|
|
35
36
|
})
|
|
36
37
|
|
|
38
|
+
afterEach(async () => {
|
|
39
|
+
vi.resetAllMocks()
|
|
40
|
+
})
|
|
41
|
+
|
|
37
42
|
it('should createHistory', async () => {
|
|
38
43
|
const user = await User.create({ name: 'John', role: 'user' })
|
|
39
44
|
expect(user.name).toBe('John')
|
|
@@ -94,6 +99,7 @@ describe('plugin - patch history disabled', () => {
|
|
|
94
99
|
expect(user.name).toBe('John')
|
|
95
100
|
|
|
96
101
|
if (isMongooseLessThan7) {
|
|
102
|
+
// @ts-expect-error not available in Mongoose 6 and below
|
|
97
103
|
await User.update({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
|
|
98
104
|
} else {
|
|
99
105
|
await User.findOneAndUpdate({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
|
|
@@ -110,6 +116,7 @@ describe('plugin - patch history disabled', () => {
|
|
|
110
116
|
expect(alice.name).toBe('Alice')
|
|
111
117
|
|
|
112
118
|
if (isMongooseLessThan7) {
|
|
119
|
+
// @ts-expect-error not available in Mongoose 6 and below
|
|
113
120
|
await User.update({ role: 'user' }, { $set: { name: 'Bob' } }, { multi: true }).exec()
|
|
114
121
|
} else {
|
|
115
122
|
await User.updateMany({ role: 'user' }, { $set: { name: 'Bob' } }).exec()
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
|
|
1
3
|
import mongoose, { model } from 'mongoose'
|
|
2
4
|
import { isMongooseLessThan7 } from '../src/version'
|
|
3
5
|
|
|
@@ -7,15 +9,14 @@ import UserSchema from './schemas/UserSchema'
|
|
|
7
9
|
import { USER_DELETED } from './constants/events'
|
|
8
10
|
|
|
9
11
|
import em from '../src/em'
|
|
12
|
+
import server from './mongo/server'
|
|
10
13
|
|
|
11
|
-
const preDeleteMock =
|
|
14
|
+
const preDeleteMock = vi.fn()
|
|
12
15
|
|
|
13
|
-
|
|
14
|
-
return { emit: jest.fn() }
|
|
15
|
-
})
|
|
16
|
+
vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
|
|
16
17
|
|
|
17
18
|
describe('plugin - preDelete test', () => {
|
|
18
|
-
const
|
|
19
|
+
const instance = server('plugin-pre-delete')
|
|
19
20
|
|
|
20
21
|
UserSchema.plugin(patchHistoryPlugin, {
|
|
21
22
|
eventDeleted: USER_DELETED,
|
|
@@ -26,11 +27,11 @@ describe('plugin - preDelete test', () => {
|
|
|
26
27
|
const User = model('User', UserSchema)
|
|
27
28
|
|
|
28
29
|
beforeAll(async () => {
|
|
29
|
-
await
|
|
30
|
+
await instance.create()
|
|
30
31
|
})
|
|
31
32
|
|
|
32
33
|
afterAll(async () => {
|
|
33
|
-
await
|
|
34
|
+
await instance.destroy()
|
|
34
35
|
})
|
|
35
36
|
|
|
36
37
|
beforeEach(async () => {
|
|
@@ -38,6 +39,10 @@ describe('plugin - preDelete test', () => {
|
|
|
38
39
|
await mongoose.connection.collection('history').deleteMany({})
|
|
39
40
|
})
|
|
40
41
|
|
|
42
|
+
afterEach(async () => {
|
|
43
|
+
vi.resetAllMocks()
|
|
44
|
+
})
|
|
45
|
+
|
|
41
46
|
it('should deleteMany and execute preDelete', async () => {
|
|
42
47
|
await User.create({ name: 'John', role: 'user' })
|
|
43
48
|
await User.create({ name: 'Jane', role: 'user' })
|
|
@@ -125,6 +130,7 @@ describe('plugin - preDelete test', () => {
|
|
|
125
130
|
const john = await User.create({ name: 'John', role: 'user' })
|
|
126
131
|
|
|
127
132
|
if (isMongooseLessThan7) {
|
|
133
|
+
// @ts-expect-error not available in Mongoose 6 and below
|
|
128
134
|
await john?.remove()
|
|
129
135
|
} else {
|
|
130
136
|
await john?.deleteOne()
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
|
|
1
3
|
import mongoose, { model } from 'mongoose'
|
|
2
4
|
|
|
3
5
|
import { patchHistoryPlugin } from '../src/plugin'
|
|
@@ -6,13 +8,12 @@ import UserSchema from './schemas/UserSchema'
|
|
|
6
8
|
import { USER_CREATED } from './constants/events'
|
|
7
9
|
|
|
8
10
|
import em from '../src/em'
|
|
11
|
+
import server from './mongo/server'
|
|
9
12
|
|
|
10
|
-
|
|
11
|
-
return { emit: jest.fn() }
|
|
12
|
-
})
|
|
13
|
+
vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
|
|
13
14
|
|
|
14
15
|
describe('plugin - preSave test', () => {
|
|
15
|
-
const
|
|
16
|
+
const instance = server('plugin-pre-save')
|
|
16
17
|
|
|
17
18
|
UserSchema.plugin(patchHistoryPlugin, {
|
|
18
19
|
eventCreated: USER_CREATED,
|
|
@@ -22,11 +23,11 @@ describe('plugin - preSave test', () => {
|
|
|
22
23
|
const User = model('User', UserSchema)
|
|
23
24
|
|
|
24
25
|
beforeAll(async () => {
|
|
25
|
-
await
|
|
26
|
+
await instance.create()
|
|
26
27
|
})
|
|
27
28
|
|
|
28
29
|
afterAll(async () => {
|
|
29
|
-
await
|
|
30
|
+
await instance.destroy()
|
|
30
31
|
})
|
|
31
32
|
|
|
32
33
|
beforeEach(async () => {
|
|
@@ -34,8 +35,13 @@ describe('plugin - preSave test', () => {
|
|
|
34
35
|
await mongoose.connection.collection('history').deleteMany({})
|
|
35
36
|
})
|
|
36
37
|
|
|
38
|
+
afterEach(async () => {
|
|
39
|
+
vi.resetAllMocks()
|
|
40
|
+
})
|
|
41
|
+
|
|
37
42
|
it('should create a User and execute save, and omit User role in history', async () => {
|
|
38
43
|
const john = await User.create({ name: 'John', role: 'user' })
|
|
44
|
+
// @ts-expect-error __v is a hidden field in Mongoose model
|
|
39
45
|
const { __v, role, ...doc } = john.toJSON()
|
|
40
46
|
|
|
41
47
|
expect(em.emit).toHaveBeenCalledTimes(1)
|
package/tests/plugin.test.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
|
|
1
3
|
import { isMongooseLessThan7 } from '../src/version'
|
|
2
4
|
|
|
3
5
|
import mongoose from 'mongoose'
|
|
@@ -6,15 +8,15 @@ import History from '../src/models/History'
|
|
|
6
8
|
import { patchHistoryPlugin } from '../src/plugin'
|
|
7
9
|
import UserSchema from './schemas/UserSchema'
|
|
8
10
|
|
|
9
|
-
import em from '../src/em'
|
|
10
11
|
import { USER_CREATED, USER_DELETED, USER_UPDATED } from './constants/events'
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
import em from '../src/em'
|
|
14
|
+
import server from './mongo/server'
|
|
15
|
+
|
|
16
|
+
vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
|
|
15
17
|
|
|
16
18
|
describe('plugin', () => {
|
|
17
|
-
const
|
|
19
|
+
const instance = server('plugin')
|
|
18
20
|
|
|
19
21
|
UserSchema.plugin(patchHistoryPlugin, {
|
|
20
22
|
eventCreated: USER_CREATED,
|
|
@@ -26,11 +28,11 @@ describe('plugin', () => {
|
|
|
26
28
|
const User = mongoose.model('User', UserSchema)
|
|
27
29
|
|
|
28
30
|
beforeAll(async () => {
|
|
29
|
-
await
|
|
31
|
+
await instance.create()
|
|
30
32
|
})
|
|
31
33
|
|
|
32
34
|
afterAll(async () => {
|
|
33
|
-
await
|
|
35
|
+
await instance.destroy()
|
|
34
36
|
})
|
|
35
37
|
|
|
36
38
|
beforeEach(async () => {
|
|
@@ -38,6 +40,10 @@ describe('plugin', () => {
|
|
|
38
40
|
await mongoose.connection.collection('history').deleteMany({})
|
|
39
41
|
})
|
|
40
42
|
|
|
43
|
+
afterEach(async () => {
|
|
44
|
+
vi.resetAllMocks()
|
|
45
|
+
})
|
|
46
|
+
|
|
41
47
|
it('should createHistory', async () => {
|
|
42
48
|
const user = await User.create({ name: 'John', role: 'user' })
|
|
43
49
|
expect(user.name).toBe('John')
|
|
@@ -269,6 +275,7 @@ describe('plugin', () => {
|
|
|
269
275
|
expect(user.name).toBe('John')
|
|
270
276
|
|
|
271
277
|
if (isMongooseLessThan7) {
|
|
278
|
+
// @ts-expect-error not available in Mongoose 6 and below
|
|
272
279
|
await User.update({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
|
|
273
280
|
} else {
|
|
274
281
|
await User.findOneAndUpdate({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
|
|
@@ -324,6 +331,7 @@ describe('plugin', () => {
|
|
|
324
331
|
expect(alice.name).toBe('Alice')
|
|
325
332
|
|
|
326
333
|
if (isMongooseLessThan7) {
|
|
334
|
+
// @ts-expect-error not available in Mongoose 6 and below
|
|
327
335
|
await User.update({ role: 'user' }, { $set: { name: 'Bob' } }, { multi: true }).exec()
|
|
328
336
|
} else {
|
|
329
337
|
await User.findOneAndUpdate({ role: 'user' }, { $set: { name: 'Bob' } }).exec()
|
package/vite.config.mts
ADDED
package/jest-mongodb-config.ts
DELETED
package/jest.config.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
const { recursive } = require('merge')
|
|
2
|
-
const mongo = require('@shelf/jest-mongodb/jest-preset')
|
|
3
|
-
|
|
4
|
-
const config = recursive(mongo, {
|
|
5
|
-
roots: ['<rootDir>/src/', '<rootDir>/tests/'],
|
|
6
|
-
clearMocks: true,
|
|
7
|
-
collectCoverage: true,
|
|
8
|
-
collectCoverageFrom: ['src/**/*.ts', '!src/**/*.d.ts', '!src/interfaces/**/*.ts'],
|
|
9
|
-
coverageDirectory: 'coverage',
|
|
10
|
-
testMatch: ['<rootDir>/tests/**/*.test.ts'],
|
|
11
|
-
transform: {
|
|
12
|
-
'^.+\\.(t|j)sx?$': '@swc-node/jest',
|
|
13
|
-
},
|
|
14
|
-
testPathIgnorePatterns: ['node_modules'],
|
|
15
|
-
watchPathIgnorePatterns: ['globalConfig'],
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
module.exports = config
|