-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix unhandled promise rejections #702
Changes from 3 commits
be83e97
07af7fb
916a138
85a9fa5
1607174
ff9e916
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,10 +71,31 @@ suite('Unit Tests - debugging', () => { | |
assert.equal(tests.testFunctions.length, 2, 'Incorrect number of test functions'); | ||
assert.equal(tests.testSuites.length, 2, 'Incorrect number of test suites'); | ||
|
||
const deferred = createDeferred<string>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With using |
||
const testFunction = [tests.testFunctions[0].testFunction]; | ||
testManager.runTest(CommandSource.commandPalette, { testFunction }, false, true); | ||
const launched = await mockDebugLauncher.launched; | ||
assert.isTrue(launched, 'Debugger not launched'); | ||
const runningPromise = testManager.runTest(CommandSource.commandPalette, { testFunction }, false, true); | ||
|
||
// This promise should never resolve nor reject. | ||
runningPromise | ||
.then(() => 'Debugger stopped when it shouldn\'t have') | ||
.catch(() => 'Debugger crashed when it shouldn\'t have') | ||
// tslint:disable-next-line:no-floating-promises | ||
.then(error => { | ||
deferred.reject(error); | ||
}); | ||
|
||
mockDebugLauncher.launched | ||
// tslint:disable-next-line:no-unsafe-any | ||
.then((launched) => { | ||
if (launched) { | ||
deferred.resolve(''); | ||
} else { | ||
deferred.reject('Debugger not launched'); | ||
} | ||
}) | ||
// tslint:disable-next-line:no-unsafe-any | ||
.catch(ex => deferred.reject(ex)); | ||
return await deferred.promise; | ||
} | ||
|
||
test('Debugger should start (unittest)', async () => { | ||
|
@@ -105,8 +126,7 @@ suite('Unit Tests - debugging', () => { | |
const launched = await mockDebugLauncher.launched; | ||
assert.isTrue(launched, 'Debugger not launched'); | ||
|
||
testManager.discoverTests(CommandSource.commandPalette, true, true, true); | ||
|
||
await testManager.discoverTests(CommandSource.commandPalette, true, true, true); | ||
await expect(runningPromise).to.be.rejectedWith(CANCELLATION_REASON, 'Incorrect reason for ending the debugger'); | ||
} | ||
|
||
|
@@ -151,6 +171,7 @@ suite('Unit Tests - debugging', () => { | |
runningPromise | ||
.then(() => 'Debugger stopped when it shouldn\'t have') | ||
.catch(() => 'Debugger crashed when it shouldn\'t have') | ||
// tslint:disable-next-line: no-floating-promises | ||
.then(error => { | ||
deferred.reject(error); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import { expect, use } from 'chai'; | |
import * as chaiAsPromised from 'chai-as-promised'; | ||
import * as path from 'path'; | ||
import { Uri } from 'vscode'; | ||
import {createDeferred} from '../../client/common/helpers'; | ||
import { Product } from '../../client/common/types'; | ||
import { CANCELLATION_REASON, CommandSource, UNITTEST_PROVIDER } from '../../client/unittests/common/constants'; | ||
import { ITestDiscoveryService } from '../../client/unittests/common/types'; | ||
|
@@ -60,9 +61,28 @@ suite('Unit Tests Stopping Discovery and Runner', () => { | |
|
||
const discoveryPromise = mockTestManager.discoverTests(CommandSource.auto); | ||
mockTestManager.discoveryDeferred.resolve(EmptyTests); | ||
mockTestManager.runTest(CommandSource.ui); | ||
const runningPromise = mockTestManager.runTest(CommandSource.ui); | ||
const deferred = createDeferred<string>(); | ||
|
||
await expect(discoveryPromise).to.eventually.equal(EmptyTests); | ||
// This promise should never resolve nor reject. | ||
runningPromise | ||
.then(() => 'Debugger stopped when it shouldn\'t have') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't do anything, if this isn't necessary, then you need to change this line as follows: This will cause it to fail, then the catch will do what's necessary or There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, makes sense. It is 1:30AM my time and I will fix this tomorrow. Thanks for the review! |
||
.catch(() => 'Debugger crashed when it shouldn\'t have') | ||
// tslint:disable-next-line:no-floating-promises | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See https://stackoverflow.com/a/48603963/5749914 for my take on why this is necessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should change this again as follows: This way, whether the promise resolves or rejects, we're capturing it and accordingly rejecting the |
||
.then(error => { | ||
deferred.reject(error); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
}); | ||
|
||
discoveryPromise.then(result => { | ||
if (result === EmptyTests) { | ||
deferred.resolve(''); | ||
} else { | ||
deferred.reject('tests not empty'); | ||
} | ||
}).catch(error => { | ||
deferred.reject(error); | ||
}); | ||
await deferred.promise; | ||
}); | ||
|
||
test('Discovering tests should stop running tests', async () => { | ||
|
@@ -75,7 +95,7 @@ suite('Unit Tests Stopping Discovery and Runner', () => { | |
await new Promise(resolve => setTimeout(resolve, 1000)); | ||
|
||
// User manually discovering tests will kill the existing test runner. | ||
mockTestManager.discoverTests(CommandSource.ui, true, false, true); | ||
await mockTestManager.discoverTests(CommandSource.ui, true, false, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This caused the coverage to go down because, without the |
||
await expect(runPromise).to.eventually.be.rejectedWith(CANCELLATION_REASON); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forEach
on an async function doesn't do anything.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see below for the fix to this entire test