Skip to content

Commit 1f981b0

Browse files
authored
fix: more explicit checks (#11)
* fix: more explicit checks * fix: build
1 parent e37f805 commit 1f981b0

File tree

3 files changed

+141
-108
lines changed

3 files changed

+141
-108
lines changed

src/basic.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import assert from 'node:assert'
2-
import { describe, beforeEach } from 'vitest'
1+
import { describe, beforeEach, expect } from 'vitest'
32
import type { Test } from './declarations.js'
43
import type { Application } from '@feathersjs/feathers'
54

@@ -35,38 +34,43 @@ export default (options: BasicTestOptions) => {
3534

3635
const config: TestConfigBasic = {
3736
'.id': () => {
38-
assert.strictEqual(
39-
service.id,
40-
idProp,
41-
'id property is set to expected name',
42-
)
37+
expect(service.id, 'id property is set to expected name').toBe(idProp)
4338
},
4439
'.options': () => {
45-
assert.ok(service.options, 'Options are available in service.options')
40+
expect(service, 'service.options is defined').toHaveProperty('options')
41+
42+
expect(
43+
service.options,
44+
'Options are available in service.options',
45+
).toBeTypeOf('object')
4646
},
4747
'.events': () => {
48-
assert.ok(
49-
service.events.includes('testing'),
50-
'service.events is set and includes "testing"',
48+
expect(service, 'service has events').toHaveProperty('events')
49+
expect(service.events, 'service.events is an array').toBeInstanceOf(
50+
Array,
5151
)
52+
expect(
53+
service.events.includes('testing'),
54+
'service.events includes "testing"',
55+
).toBe(true)
5256
},
5357
'._get': () => {
54-
assert.strictEqual(typeof service._get, 'function')
58+
expect(typeof service._get).toBe('function')
5559
},
5660
'._find': () => {
57-
assert.strictEqual(typeof service._find, 'function')
61+
expect(typeof service._find).toBe('function')
5862
},
5963
'._create': () => {
60-
assert.strictEqual(typeof service._create, 'function')
64+
expect(typeof service._create).toBe('function')
6165
},
6266
'._update': () => {
63-
assert.strictEqual(typeof service._update, 'function')
67+
expect(typeof service._update).toBe('function')
6468
},
6569
'._patch': () => {
66-
assert.strictEqual(typeof service._patch, 'function')
70+
expect(typeof service._patch).toBe('function')
6771
},
6872
'._remove': () => {
69-
assert.strictEqual(typeof service._remove, 'function')
73+
expect(typeof service._remove).toBe('function')
7074
},
7175
}
7276

src/methods.ts

Lines changed: 114 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,32 @@ type MethodTests = {
1818
| '.get'
1919
| '.get + $select'
2020
| '.get + id + query'
21+
| '.get + id + query id'
2122
| '.get + NotFound (string)'
2223
| '.get + NotFound (integer)'
23-
| '.get + id + query id'
2424
remove:
2525
| '.remove'
2626
| '.remove + $select'
2727
| '.remove + id + query'
28+
| '.remove + id + query id'
2829
| '.remove + NotFound (string)'
2930
| '.remove + NotFound (integer)'
3031
| '.remove + multi'
3132
| '.remove + multi no pagination'
32-
| '.remove + id + query id'
3333
update:
3434
| '.update'
3535
| '.update + $select'
3636
| '.update + id + query'
37+
| '.update + id + query id'
3738
| '.update + NotFound (string)'
3839
| '.update + NotFound (integer)'
3940
| '.update + query + NotFound'
40-
| '.update + id + query id'
4141
patch:
4242
| '.patch'
4343
| '.patch + $select'
44+
| '.patch + $select unchanged'
4445
| '.patch + id + query'
46+
| '.patch + id + query id'
4547
| '.patch multiple'
4648
| '.patch multiple no pagination'
4749
| '.patch multi query same'
@@ -51,8 +53,6 @@ type MethodTests = {
5153
// | '.patch multi + $limit'
5254
| '.patch + NotFound (string)'
5355
| '.patch + NotFound (integer)'
54-
| '.patch + query + NotFound'
55-
| '.patch + id + query id'
5656
create:
5757
| '.create'
5858
| '.create + $select'
@@ -141,15 +141,6 @@ export default (options: MethodTestOptions) => {
141141
NotFound,
142142
)
143143
},
144-
'.get + NotFound (string)': async () => {
145-
await assert.rejects(
146-
() => service.get('568225fbfe21222432e836ff'),
147-
NotFound,
148-
)
149-
},
150-
'.get + NotFound (integer)': async () => {
151-
await assert.rejects(() => service.get(123141231231), NotFound)
152-
},
153144
'.get + id + query id': async () => {
154145
const alice = await service.create({
155146
name: 'Alice',
@@ -164,6 +155,15 @@ export default (options: MethodTestOptions) => {
164155
NotFound,
165156
)
166157
},
158+
'.get + NotFound (string)': async () => {
159+
await assert.rejects(
160+
() => service.get('568225fbfe21222432e836ff'),
161+
NotFound,
162+
)
163+
},
164+
'.get + NotFound (integer)': async () => {
165+
await assert.rejects(() => service.get(123141231231), NotFound)
166+
},
167167
},
168168
remove: {
169169
'.remove': async () => {
@@ -199,6 +199,23 @@ export default (options: MethodTestOptions) => {
199199
const stillExists = await service.get(doug[idProp])
200200
assert.ok(stillExists, 'Doug still exists')
201201
},
202+
'.remove + id + query id': async () => {
203+
const alice = await service.create({
204+
name: 'Alice',
205+
age: 12,
206+
})
207+
208+
await assert.rejects(
209+
() =>
210+
service.remove(doug[idProp], {
211+
query: { [idProp]: alice[idProp] },
212+
}),
213+
NotFound,
214+
)
215+
216+
const stillExists = await service.get(doug[idProp])
217+
assert.ok(stillExists, 'Doug still exists')
218+
},
202219
'.remove + NotFound (string)': async () => {
203220
await assert.rejects(
204221
() => service.remove('568225fbfe21222432e836ff'),
@@ -305,28 +322,11 @@ export default (options: MethodTestOptions) => {
305322
},
306323
)
307324
},
308-
'.remove + id + query id': async () => {
309-
const alice = await service.create({
310-
name: 'Alice',
311-
age: 12,
312-
})
313-
314-
await assert.rejects(
315-
() =>
316-
service.remove(doug[idProp], {
317-
query: { [idProp]: alice[idProp] },
318-
}),
319-
NotFound,
320-
)
321-
322-
const stillExists = await service.get(doug[idProp])
323-
assert.ok(stillExists, 'Doug still exists')
324-
},
325325
} satisfies TestConfig<'remove'>,
326326
update: {
327327
'.update': async () => {
328328
const originalData = { [idProp]: doug[idProp], name: 'Dougler' }
329-
const originalCopy = Object.assign({}, originalData)
329+
const originalCopy = { ...originalData }
330330

331331
const data = await service.update(doug[idProp], originalData)
332332

@@ -364,6 +364,11 @@ export default (options: MethodTestOptions) => {
364364

365365
const changed = await service.get(doug[idProp])
366366

367+
assert.strictEqual(
368+
changed.name,
369+
originalData.name,
370+
'data.name changed',
371+
)
367372
assert.strictEqual(changed.age, originalData.age, 'data.age changed')
368373
},
369374
'.update + id + query': async () => {
@@ -380,6 +385,34 @@ export default (options: MethodTestOptions) => {
380385
),
381386
NotFound,
382387
)
388+
389+
const unchanged = await service.get(doug[idProp])
390+
assert.strictEqual(unchanged.name, doug.name, 'name is still Doug')
391+
},
392+
'.update + id + query id': async () => {
393+
const alice = await service.create({
394+
name: 'Alice',
395+
age: 12,
396+
})
397+
398+
await assert.rejects(
399+
() =>
400+
service.update(
401+
doug[idProp],
402+
{
403+
name: 'Dougler',
404+
age: 33,
405+
},
406+
{
407+
query: { [idProp]: alice[idProp] },
408+
},
409+
),
410+
NotFound,
411+
)
412+
413+
const unchanged = await service.get(doug[idProp])
414+
assert.equal(unchanged.name, doug.name, 'name stayed the same')
415+
assert.equal(unchanged.age, doug.age, 'age stayed the same')
383416
},
384417
'.update + NotFound (string)': async () => {
385418
await assert.rejects(
@@ -412,27 +445,6 @@ export default (options: MethodTestOptions) => {
412445
NotFound,
413446
)
414447
},
415-
'.update + id + query id': async () => {
416-
const alice = await service.create({
417-
name: 'Alice',
418-
age: 12,
419-
})
420-
421-
await assert.rejects(
422-
() =>
423-
service.update(
424-
doug[idProp],
425-
{
426-
name: 'Dougler',
427-
age: 33,
428-
},
429-
{
430-
query: { [idProp]: alice[idProp] },
431-
},
432-
),
433-
NotFound,
434-
)
435-
},
436448
} satisfies TestConfig<'update'>,
437449
patch: {
438450
'.patch': async () => {
@@ -476,6 +488,28 @@ export default (options: MethodTestOptions) => {
476488
const changed = await service.get(doug[idProp])
477489
assert.strictEqual(changed.age, originalData.age, 'data.age changed')
478490
},
491+
492+
'.patch + $select unchanged': async () => {
493+
const originalData = {
494+
[idProp]: doug[idProp],
495+
name: 'PatchDoug',
496+
}
497+
498+
const data = await service.patch(doug[idProp], originalData, {
499+
query: { $select: ['name'] },
500+
})
501+
502+
assert.strictEqual(
503+
data[idProp].toString(),
504+
doug[idProp].toString(),
505+
`${idProp} id property matches`,
506+
)
507+
assert.strictEqual(data.name, 'PatchDoug', 'data.name matches')
508+
assert.ok(!('age' in data), 'data.age is not present')
509+
510+
const changed = await service.get(doug[idProp])
511+
assert.strictEqual(changed.age, doug.age, 'data.age unchanged')
512+
},
479513
'.patch + id + query': async () => {
480514
await assert.rejects(
481515
() =>
@@ -490,6 +524,33 @@ export default (options: MethodTestOptions) => {
490524
),
491525
NotFound,
492526
)
527+
528+
const unchanged = await service.get(doug[idProp])
529+
assert.strictEqual(unchanged.name, doug.name, 'name is still Doug')
530+
},
531+
'.patch + id + query id': async () => {
532+
const alice = await service.create({
533+
name: 'Alice',
534+
age: 12,
535+
})
536+
537+
await assert.rejects(
538+
() =>
539+
service.patch(
540+
doug[idProp],
541+
{
542+
age: 33,
543+
},
544+
{
545+
query: { [idProp]: alice[idProp] },
546+
},
547+
),
548+
NotFound,
549+
)
550+
551+
const dougAfter = await service.get(doug[idProp])
552+
553+
assert.equal(dougAfter.age, doug.age, 'age stayed the same')
493554
},
494555
'.patch multiple': async () => {
495556
await withOptions(service, { multi: false }, () =>
@@ -742,43 +803,6 @@ export default (options: MethodTestOptions) => {
742803
NotFound,
743804
)
744805
},
745-
'.patch + query + NotFound': async () => {
746-
const dave = await service.create({ name: 'Dave' })
747-
748-
await assert.rejects(
749-
() =>
750-
service.patch(
751-
dave[idProp],
752-
{ name: 'PatchedDave' },
753-
{ query: { name: 'NotDave' } },
754-
),
755-
NotFound,
756-
)
757-
},
758-
'.patch + id + query id': async () => {
759-
const alice = await service.create({
760-
name: 'Alice',
761-
age: 12,
762-
})
763-
764-
await assert.rejects(
765-
() =>
766-
service.patch(
767-
doug[idProp],
768-
{
769-
age: 33,
770-
},
771-
{
772-
query: { [idProp]: alice[idProp] },
773-
},
774-
),
775-
NotFound,
776-
)
777-
778-
const dougAfter = await service.get(doug[idProp])
779-
780-
assert.equal(doug.age, dougAfter.age, 'age stayed the same')
781-
},
782806
} satisfies TestConfig<'patch'>,
783807
create: {
784808
'.create': async () => {

test/feathers-memory.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { feathers } from '@feathersjs/feathers'
33

44
import { MemoryService } from '@feathersjs/memory'
55

6-
const testSuite = defineTestSuite()
6+
const testSuite = defineTestSuite({
7+
blacklist: [
8+
'.update + id + query', // need to be fixed upstream, see https://github.com/feathersjs/feathers/pull/3617
9+
'.update + id + query id', // need to be fixed upstream, see https://github.com/feathersjs/feathers/pull/3617
10+
],
11+
})
712

813
describe('@feathersjs/memory', () => {
914
type Person = {

0 commit comments

Comments
 (0)