Skip to content

Commit

Permalink
test: Utilize async/await to clean up test case
Browse files Browse the repository at this point in the history
  • Loading branch information
W-A-James committed Jun 16, 2021
1 parent 1605d90 commit 70959d9
Showing 1 changed file with 26 additions and 36 deletions.
62 changes: 26 additions & 36 deletions test/functional/cursor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3905,10 +3905,15 @@ describe('Cursor', function () {
let configuration;
let client;
let cursor;
let collection;

beforeEach(function () {
beforeEach(async function () {
configuration = this.configuration;
client = configuration.newClient({ w: 1 }, { maxPoolSize: 1 });
await client.connect().catch(() => {
expect.fail('Failed to connect to client');
});
collection = client.db(configuration.db).collection('cursor_session_tests2');
});

afterEach(async function () {
Expand All @@ -3917,41 +3922,26 @@ describe('Cursor', function () {
});

// NODE-2035
it('should propagate error when exceptions are thrown from an awaited forEach call', function () {
const docs = [{ a: 1 }, { a: 2 }, { a: 3 }];
let collection;
return client
.connect()
.then(
() => {
let db = client.db(configuration.db);
collection = db.collection('cursor_session_tests2');
return collection.insertMany(docs);
},
() => {
expect.fail('Failed to connect to client');
}
)
.then(() => {
cursor = collection.find({});
expect(() => {
cursor
.forEach(() => {
throw new Error('FAILURE IN FOREACH CALL');
})
.then(
() => {
expect.fail('Failed to catch error thrown in awaited forEach');
},
err => {
expect(err.message).to.deep.equal('FAILURE IN FOREACH CALL');
}
);
}).to.not.throw();
})
.catch(() => {
expect.fail('Failed to insert documents');
});
it('should propagate error when exceptions are thrown from an awaited forEach call', async function () {
const docs = [{ unique_key_2035: 1 }, { unique_key_2035: 2 }, { unique_key_2035: 3 }];
await collection.insertMany(docs).catch(() => {
expect.fail('Failed to insert documents');
});
cursor = collection.find({
unique_key_2035: {
$exists: true
}
});
expect(async function () {
await cursor
.forEach(() => {
throw new Error('FAILURE IN FOREACH CALL');
})
.catch(err => {
console.log(err.message);
expect(err.message).to.deep.equal('FAILURE IN FOREACH CALL');
});
}).to.not.throw();
});
});

Expand Down

0 comments on commit 70959d9

Please sign in to comment.