Skip to content

Commit 03182bd

Browse files
committed
fix: transform wrapping
1 parent 8614df3 commit 03182bd

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/cursor/abstract_cursor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ export abstract class AbstractCursor<
533533
const oldTransform = this[kTransform] as (doc: TSchema) => TSchema; // TODO(NODE-3283): Improve transform typing
534534
if (oldTransform) {
535535
this[kTransform] = doc => {
536-
return transform(oldTransform(doc));
536+
return this.makeSafeTransform(transform)(oldTransform(doc));
537537
};
538538
} else {
539539
this[kTransform] = this.makeSafeTransform(transform);

test/integration/node-specific/abstract_cursor.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,26 @@ describe('class AbstractCursor', function () {
153153
await client.close();
154154
});
155155

156+
it('wraps transform in result checking for each map call', async () => {
157+
const control = { functionThatShouldReturnNull: 0 };
158+
const makeCursor = () => {
159+
const cursor = collection.find();
160+
cursor
161+
.map(doc => (control.functionThatShouldReturnNull === 0 ? null : doc))
162+
.map(doc => (control.functionThatShouldReturnNull === 1 ? null : doc))
163+
.map(doc => (control.functionThatShouldReturnNull === 2 ? null : doc));
164+
return cursor;
165+
};
166+
167+
for (const testFn of [0, 1, 2]) {
168+
control.functionThatShouldReturnNull = testFn;
169+
const error = await makeCursor()
170+
.toArray()
171+
.catch(error => error);
172+
expect(error).to.be.instanceOf(MongoAPIError);
173+
}
174+
});
175+
156176
context('toArray() with custom transforms', function () {
157177
for (const value of falseyValues) {
158178
it(`supports mapping to falsey value '${inspect(value)}'`, async function () {

0 commit comments

Comments
 (0)