Skip to content

Commit

Permalink
[botbuilder-core] TestAdapter unhandled promises
Browse files Browse the repository at this point in the history
Description

TestAdapter throws unhalded promise rejections on errors

- The testadapter should handle errors
- For that, TestFlow must implement the thenable implementation correctly

Specific Changes

  - Implemented further the tenable on TestFlow to allow handling catch clauses correctly

Testing

A test is added to verify the behavior change (If you run that test on main before this PR is merged, it fails)
  • Loading branch information
alexrecuenco committed Nov 30, 2021
1 parent 2bd9b49 commit fa5cc81
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
13 changes: 11 additions & 2 deletions libraries/botbuilder-core/src/testAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -978,8 +978,17 @@ export class TestFlow {
* Adds a `then()` step to the tests promise chain.
* @param onFulfilled Code to run if the test is currently passing.
*/
public then(onFulfilled?: () => void): TestFlow {
return new TestFlow(this.previous.then(onFulfilled), this.adapter, this.callback);
public then(onFulfilled?: () => void, onRejected?: (err) => void): TestFlow {
return new TestFlow(this.previous.then(onFulfilled, onRejected), this.adapter, this.callback);
}

/**
* Adds a finally clause. Note that you can't keep chaining afterwards.
* @param onFinally
* @returns
*/
public finally(onFinally: () => void): Promise<void> {
return Promise.resolve(this.previous.finally(onFinally));
}

/**
Expand Down
35 changes: 35 additions & 0 deletions libraries/botbuilder-core/tests/testAdapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,41 @@ describe(`TestAdapter`, function () {
await adapter.receiveActivity('test');
});

it(`should reject on unhandled error.`, async function () {
const adapter = new TestAdapter((context) => {
throw new Error('Intentional Error');
});
await assert.rejects(async () => {
await adapter.send('Message');
})
});

it(`should call finally when no error happens`, async function () {
const adapter = new TestAdapter(async (context) => undefined);
let runFinally = false;

await adapter.send('Message').finally(() => {
runFinally = true
})
assert(runFinally, "Finally was not called");

});

it(`should call finally when an error happens`, async function () {
const adapter = new TestAdapter((context) => {
throw new Error('Intentional Error');
});
let runFinally = false;

await assert.rejects(async () => {
await adapter.send('Message')
.finally(() => {
runFinally = true
})
});
assert(runFinally, "Finally was not called");
});

it(`should support receiveActivity() called with an Activity.`, async function () {
const adapter = new TestAdapter((context) => {
assert(context.activity.type === ActivityTypes.Message, `wrong type.`);
Expand Down

0 comments on commit fa5cc81

Please sign in to comment.