Skip to content

Commit c0835a0

Browse files
committed
test: update type tests
1 parent 077952e commit c0835a0

File tree

3 files changed

+37
-56
lines changed

3 files changed

+37
-56
lines changed

src/operations/find_and_modify.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,7 @@ class FindAndModifyOperation extends CommandOperation<Document> {
133133
upsert: false
134134
};
135135

136-
// Default to returning the raw result.
137-
if (!('includeResultMetadata' in options)) {
138-
options.includeResultMetadata = true;
139-
}
136+
options.includeResultMetadata ??= true;
140137

141138
const sort = formatSort(options.sort);
142139
if (sort) {

test/types/community/collection/findX.test-d.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,25 @@ const fooObjWithArray: FooWithArray = {
288288
const fooFilterWithArray: Filter<FooWithArray> = fooObjWithArray;
289289

290290
declare const coll: Collection<{ a: number; b: string }>;
291+
expectType<WithId<{ a: number; b: string }> | null>((await coll.findOneAndDelete({ a: 3 })).value);
292+
expectType<WithId<{ a: number; b: string }> | null>(
293+
(await coll.findOneAndReplace({ a: 3 }, { a: 5, b: 'new string' })).value
294+
);
295+
expectType<WithId<{ a: number; b: string }> | null>(
296+
(
297+
await coll.findOneAndUpdate(
298+
{ a: 3 },
299+
{
300+
$set: {
301+
a: 5
302+
}
303+
}
304+
)
305+
).value
306+
);
307+
308+
// Including { includeResultMetadata: false } option will change the return type
309+
// to the modified document or null.
291310
expectType<WithId<{ a: number; b: string }> | null>(
292311
await coll.findOneAndDelete({ a: 3 }, { includeResultMetadata: false })
293312
);
@@ -299,12 +318,20 @@ expectType<WithId<{ a: number; b: string }> | null>(
299318
)
300319
);
301320
expectType<WithId<{ a: number; b: string }> | null>(
302-
await coll.findOneAndUpdate({ a: 3 }, { $set: { a: 5 } }, { includeResultMetadata: false })
321+
await coll.findOneAndUpdate(
322+
{ a: 3 },
323+
{
324+
$set: {
325+
a: 5
326+
}
327+
},
328+
{ includeResultMetadata: false }
329+
)
303330
);
304331

305332
// projections do not change the return type - our typing doesn't support this
306333
expectType<WithId<{ a: number; b: string }> | null>(
307-
await coll.findOneAndDelete({ a: 3 }, { projection: { _id: 0 }, includeResultMetadata: false })
334+
(await coll.findOneAndDelete({ a: 3 }, { projection: { _id: 0 } })).value
308335
);
309336

310337
// NODE-3568: Uncomment when ModifyResult is removed in 5.0

test/types/schema_helpers.test-d.ts

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,22 @@ expectAssignable<SchemaWithIdInterface | null>(await interfaceTestCollection.fin
7272
expectAssignable<SchemaWithIdType>((await typeTestCollection.find().toArray())[0]);
7373
expectAssignable<SchemaWithIdInterface>((await interfaceTestCollection.find().toArray())[0]);
7474
expectAssignable<SchemaWithIdType | null>(
75-
await typeTestCollection.findOneAndDelete({ a: 1 }, { includeResultMetadata: false })
75+
(await typeTestCollection.findOneAndDelete({ a: 1 })).value
7676
);
7777
expectAssignable<SchemaWithIdInterface | null>(
78-
await interfaceTestCollection.findOneAndDelete({ a: 1 }, { includeResultMetadata: false })
78+
(await interfaceTestCollection.findOneAndDelete({ a: 1 })).value
7979
);
8080
expectAssignable<SchemaWithIdType | null>(
81-
await typeTestCollection.findOneAndReplace({ a: 1 }, { a: 5 }, { includeResultMetadata: false })
81+
(await typeTestCollection.findOneAndReplace({ a: 1 }, { a: 5 })).value
8282
);
8383
expectAssignable<SchemaWithIdInterface | null>(
84-
await interfaceTestCollection.findOneAndReplace(
85-
{ a: 1 },
86-
{ a: 5 },
87-
{ includeResultMetadata: false }
88-
)
84+
(await interfaceTestCollection.findOneAndReplace({ a: 1 }, { a: 5 })).value
8985
);
9086
expectAssignable<SchemaWithIdType | null>(
91-
await typeTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 }, { includeResultMetadata: false })
87+
(await typeTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 })).value
9288
);
9389
expectAssignable<SchemaWithIdInterface | null>(
94-
await interfaceTestCollection.findOneAndUpdate(
95-
{ a: 1 },
96-
{ a: 5 },
97-
{ includeResultMetadata: false }
98-
)
90+
(await interfaceTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 })).value
9991
);
10092

10193
// OptionalId assignability when wrapping a schema with _id: number
@@ -123,7 +115,7 @@ expectAssignable<SchemaWithIdNumberInterface | null>(
123115
(await interfaceNumberTestCollection.findOneAndDelete({ a: 1 })).value
124116
);
125117
expectAssignable<SchemaWithIdNumberType | null>(
126-
(await typeNumberTestCollection.findOneAndReplace({ a: 1 }, { a: 5 }, {})).value
118+
(await typeNumberTestCollection.findOneAndReplace({ a: 1 }, { a: 5 })).value
127119
);
128120
expectAssignable<SchemaWithIdNumberInterface | null>(
129121
(await interfaceNumberTestCollection.findOneAndReplace({ a: 1 }, { a: 5 })).value
@@ -135,41 +127,6 @@ expectAssignable<SchemaWithIdNumberInterface | null>(
135127
(await interfaceNumberTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 })).value
136128
);
137129

138-
expectAssignable<SchemaWithIdNumberType | null>(
139-
await typeNumberTestCollection.findOneAndDelete({ a: 1 }, { includeResultMetadata: false })
140-
);
141-
expectAssignable<SchemaWithIdNumberInterface | null>(
142-
await interfaceNumberTestCollection.findOneAndDelete({ a: 1 }, { includeResultMetadata: false })
143-
);
144-
expectAssignable<SchemaWithIdNumberType | null>(
145-
await typeNumberTestCollection.findOneAndReplace(
146-
{ a: 1 },
147-
{ a: 5 },
148-
{ includeResultMetadata: false }
149-
)
150-
);
151-
expectAssignable<SchemaWithIdNumberInterface | null>(
152-
await interfaceNumberTestCollection.findOneAndReplace(
153-
{ a: 1 },
154-
{ a: 5 },
155-
{ includeResultMetadata: false }
156-
)
157-
);
158-
expectAssignable<SchemaWithIdNumberType | null>(
159-
await typeNumberTestCollection.findOneAndUpdate(
160-
{ a: 1 },
161-
{ a: 5 },
162-
{ includeResultMetadata: false }
163-
)
164-
);
165-
expectAssignable<SchemaWithIdNumberInterface | null>(
166-
await interfaceNumberTestCollection.findOneAndUpdate(
167-
{ a: 1 },
168-
{ a: 5 },
169-
{ includeResultMetadata: false }
170-
)
171-
);
172-
173130
/** ----------------------------------------------------------------------
174131
* OptionalUnlessRequiredId
175132
*/

0 commit comments

Comments
 (0)