diff --git a/package-lock.json b/package-lock.json index 333483e50..287f7bf15 100644 --- a/package-lock.json +++ b/package-lock.json @@ -105,6 +105,7 @@ "process": "^0.11.10", "sinon": "^9.2.4", "sinon-chai": "^3.7.0", + "source-map-support": "^0.5.21", "stream-browserify": "^3.0.0", "terser-webpack-plugin": "^5.3.10", "ts-loader": "^9.5.1", @@ -17344,6 +17345,7 @@ "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz", "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==", "dev": true, + "license": "MIT", "dependencies": { "buffer-from": "^1.0.0", "source-map": "^0.6.0" diff --git a/package.json b/package.json index 0ed0ca22b..cc2ca9d41 100644 --- a/package.json +++ b/package.json @@ -1174,6 +1174,7 @@ "process": "^0.11.10", "sinon": "^9.2.4", "sinon-chai": "^3.7.0", + "source-map-support": "^0.5.21", "stream-browserify": "^3.0.0", "terser-webpack-plugin": "^5.3.10", "ts-loader": "^9.5.1", diff --git a/src/test/runTest.ts b/src/test/runTest.ts index ab986a8a3..0a0afe6c9 100644 --- a/src/test/runTest.ts +++ b/src/test/runTest.ts @@ -17,41 +17,65 @@ async function startTestMongoDBServer() { }); } +let testMongoDBServer: MongoCluster; + +function cleanup() { + console.log('Stopping MongoDB server on port', TEST_DATABASE_PORT); + void testMongoDBServer?.close(); +} + async function main(): Promise { - const testMongoDBServer = await startTestMongoDBServer(); - - let failed = false; - - try { - // The folder containing the Extension Manifest package.json - // Passed to `--extensionDevelopmentPath` - const extensionDevelopmentPath = path.join(__dirname, '../../'); - - // The path to test runner pased to --extensionTestsPath - const extensionTestsPath = path.join(__dirname, './suite/index'); - - // This is the workspace we open in our tests. - const testWorkspace = path.join(__dirname, '../../out/test'); - - // Download VS Code, unzip it and run the integration test - await runTests({ - version: 'insiders', // Download latest insiders. - extensionDevelopmentPath, - extensionTestsPath, - launchArgs: [testWorkspace, '--disable-extensions'], - }); - } catch (err) { - console.error('Failed to run tests:'); - console.error(err); - failed = true; - } finally { - console.log('Stopping MongoDB server on port', TEST_DATABASE_PORT); - await testMongoDBServer.close(); - } + testMongoDBServer = await startTestMongoDBServer(); - if (failed) { - process.exit(1); - } + // The folder containing the Extension Manifest package.json + // Passed to `--extensionDevelopmentPath` + const extensionDevelopmentPath = path.join(__dirname, '../../'); + + // The path to test runner passed to --extensionTestsPath + const extensionTestsPath = path.join(__dirname, './suite/index'); + + // This is the workspace we open in our tests. + const testWorkspace = path.join(__dirname, '../../out/test'); + + // Download VS Code, unzip it and run the integration test + await runTests({ + version: 'insiders', // Download latest insiders. + extensionDevelopmentPath, + extensionTestsPath, + launchArgs: [testWorkspace, '--disable-extensions'], + }); + + cleanup(); } +process.once('SIGINT', () => { + console.log('Process was interrupted. Cleaning-up and exiting.'); + cleanup(); + process.kill(process.pid, 'SIGINT'); +}); + +process.once('SIGTERM', () => { + console.log('Process was terminated. Cleaning-up and exiting.'); + cleanup(); + process.kill(process.pid, 'SIGTERM'); +}); + +process.once('uncaughtException', (err: Error) => { + console.log('Uncaught exception. Cleaning-up and exiting.'); + cleanup(); + throw err; +}); + +process.on('unhandledRejection', (err: Error) => { + if (!err.message.match('Test run failed with code 1')?.[0]) { + // Log an unhandled exception when it's not the regular test failure. + // Test failures are logged in the test runner already so we avoid a generic message here. + console.log('Unhandled exception. Cleaning-up and exiting.'); + console.error(err.stack || err.message || err); + } + + cleanup(); + process.exitCode = 1; +}); + void main(); diff --git a/src/test/suite/index.ts b/src/test/suite/index.ts index dc73f614b..9833e8055 100644 --- a/src/test/suite/index.ts +++ b/src/test/suite/index.ts @@ -1,3 +1,5 @@ +import sourceMapSupport from 'source-map-support'; +sourceMapSupport.install(); import Mocha from 'mocha'; import glob from 'glob'; import path from 'path';