diff --git a/package-lock.json b/package-lock.json index 603efdcfe..22da3b1a9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -59,6 +59,7 @@ "@types/babel__core": "^7.20.1", "@types/babel__traverse": "^7.20.1", "@types/chai": "^4.3.5", + "@types/cross-spawn": "^6.0.5", "@types/debug": "^4.1.8", "@types/enzyme": "^3.10.13", "@types/glob": "^7.2.0", @@ -84,6 +85,7 @@ "chalk": "^4.1.2", "cli-ux": "^5.6.7", "cross-env": "^7.0.3", + "cross-spawn": "^7.0.3", "css-loader": "^6.8.1", "depcheck": "^1.4.3", "download": "^8.0.0", @@ -3534,6 +3536,15 @@ "@types/node": "*" } }, + "node_modules/@types/cross-spawn": { + "version": "6.0.5", + "resolved": "https://registry.npmjs.org/@types/cross-spawn/-/cross-spawn-6.0.5.tgz", + "integrity": "sha512-wsIMP68FvGXk+RaWhraz6Xp4v7sl4qwzHAmtPaJEN2NRTXXI9LtFawUpeTsBNL/pd6QoLStdytCaAyiK7AEd/Q==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/debug": { "version": "4.1.8", "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.8.tgz", @@ -24566,6 +24577,15 @@ "@types/node": "*" } }, + "@types/cross-spawn": { + "version": "6.0.5", + "resolved": "https://registry.npmjs.org/@types/cross-spawn/-/cross-spawn-6.0.5.tgz", + "integrity": "sha512-wsIMP68FvGXk+RaWhraz6Xp4v7sl4qwzHAmtPaJEN2NRTXXI9LtFawUpeTsBNL/pd6QoLStdytCaAyiK7AEd/Q==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/debug": { "version": "4.1.8", "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.8.tgz", diff --git a/package.json b/package.json index abe604029..1100fb6f7 100644 --- a/package.json +++ b/package.json @@ -47,11 +47,12 @@ "watch": "npm run compile && npm-run-all -p watch:*", "watch:extension": "npm run compile:extension -- -watch", "watch:extension-bundles": "webpack --mode development --watch", - "pretest": "npm run compile && mongodb-runner start --id=vscode -- --port 27088", + "pretest": "npm run compile", + "start-test-server": "mongodb-runner start --id=vscode --port 27088", + "stop-test-server": "mongodb-runner stop --id=vscode", "test": "npm run test-webview && npm run test-extension", "test-extension": "cross-env NODE_OPTIONS=--no-force-async-hooks-checks xvfb-maybe node ./out/test/runTest.js", "test-webview": "jest", - "posttest": "mongodb-runner stop --id=vscode", "analyze-bundle": "webpack --mode production --analyze", "vscode:prepublish": "npm run clean && npm run compile:keyfile && npm run compile:resources && webpack --mode production", "check": "npm run lint && npm run depcheck", @@ -1022,6 +1023,7 @@ "@types/babel__core": "^7.20.1", "@types/babel__traverse": "^7.20.1", "@types/chai": "^4.3.5", + "@types/cross-spawn": "^6.0.5", "@types/debug": "^4.1.8", "@types/enzyme": "^3.10.13", "@types/glob": "^7.2.0", @@ -1047,6 +1049,7 @@ "chalk": "^4.1.2", "cli-ux": "^5.6.7", "cross-env": "^7.0.3", + "cross-spawn": "^7.0.3", "css-loader": "^6.8.1", "depcheck": "^1.4.3", "download": "^8.0.0", diff --git a/src/test/runTest.ts b/src/test/runTest.ts index b9d5c3d1b..0fa4378d8 100644 --- a/src/test/runTest.ts +++ b/src/test/runTest.ts @@ -1,9 +1,37 @@ import path from 'path'; import { runTests } from '@vscode/test-electron'; +import crossSpawn from 'cross-spawn'; +import fs from 'fs'; // More information on vscode specific tests: https://github.com/microsoft/vscode-test +function startTestMongoDBServer() { + console.log('Starting MongoDB server'); + crossSpawn.sync('npm', ['run', 'start-test-server'], { stdio: 'inherit' }); +} + +function cleanupTestMongoDBServer() { + console.log('Stopping MongoDB server and cleaning up server data'); + try { + crossSpawn.sync('npm', ['run', 'stop-test-server'], { + // If it's taking too long we might as well kill the process and + // move on, mongodb-runner is flaky sometimes. + timeout: 30_000, + stdio: 'inherit', + }); + } catch (e) { + console.error('Failed to stop MongoDB Server', e); + } + try { + fs.rmdirSync('.mongodb', { recursive: true }); + } catch (e) { + console.error('Failed to clean up server data', e); + } +} + async function main(): Promise { + startTestMongoDBServer(); + try { // The folder containing the Extension Manifest package.json // Passed to `--extensionDevelopmentPath` @@ -26,6 +54,8 @@ async function main(): Promise { console.error('Failed to run tests:'); console.error(err); process.exit(1); + } finally { + cleanupTestMongoDBServer(); } }