|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +// Custom reporter to ensure Mocha process exits when we're done with tests. |
| 7 | +// This is a hack, however for some reason the process running the tests do not exit. |
| 8 | +// The hack is to force it to die when tests are done, if this doesn't work we've got a bigger problem on our hands. |
| 9 | + |
| 10 | +// tslint:disable:no-var-requires no-require-imports no-any no-console no-unnecessary-class no-default-export |
| 11 | +const log = require('why-is-node-running'); |
| 12 | +const wtf = require('wtfnode'); |
| 13 | +const mochaTests: any = require('mocha'); |
| 14 | +const { EVENT_RUN_BEGIN, EVENT_RUN_END } = mochaTests.Runner.constants; |
| 15 | +class ExitReporter { |
| 16 | + constructor(runner: any) { |
| 17 | + console.log('Initialize Exit Reporter for Mocha (PVSC).'); |
| 18 | + const stats = runner.stats; |
| 19 | + runner |
| 20 | + .once(EVENT_RUN_BEGIN, () => { |
| 21 | + console.info('Start Exit Reporter for Mocha.'); |
| 22 | + }) |
| 23 | + .once(EVENT_RUN_END, () => { |
| 24 | + process.stdout.cork(); |
| 25 | + console.info('End Exit Reporter for Mocha.'); |
| 26 | + process.stdout.write('If process does not die in 30s, then log and kill.'); |
| 27 | + process.stdout.uncork(); |
| 28 | + // NodeJs generally waits for pending timeouts, however the process running Mocha |
| 29 | + // No idea why it times, out. Once again, this is a hack. |
| 30 | + // Solution (i.e. hack), lets add a timeout with a delay of 30 seconds, |
| 31 | + // & if this process doesn't die, lets kill it. |
| 32 | + function die() { |
| 33 | + setTimeout(() => { |
| 34 | + console.info('Exiting from custom PVSC Mocha Reporter.'); |
| 35 | + try { |
| 36 | + log(); |
| 37 | + wtf(); |
| 38 | + } catch (ex) { |
| 39 | + // Do nothing. |
| 40 | + } |
| 41 | + process.exit(stats.failures === 0 ? 0 : 1); |
| 42 | + try { |
| 43 | + // Lets just close VSC, hopefully that'll be sufficient (more graceful). |
| 44 | + const vscode = require('vscode'); |
| 45 | + vscode.commands.executeCommand('workbench.action.closeWindow'); |
| 46 | + } catch (ex) { |
| 47 | + // Do nothing. |
| 48 | + } |
| 49 | + }, 30000); |
| 50 | + } |
| 51 | + die(); |
| 52 | + }); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +module.exports = ExitReporter; |
0 commit comments