|
3 | 3 | * Licensed under the MIT License. See License.txt in the project root for license information.
|
4 | 4 | *--------------------------------------------------------------------------------------------*/
|
5 | 5 |
|
6 |
| -// |
7 |
| -// PLEASE DO NOT MODIFY / DELETE UNLESS YOU KNOW WHAT YOU ARE DOING |
8 |
| -// |
9 |
| -// This file is providing the test runner to use when running extension tests. |
10 |
| -// By default the test runner in use is Mocha based. |
11 |
| -// |
12 |
| -// You can provide your own test runner if you want to override it by exporting |
13 |
| -// a function run(testRoot: string, clb: (error:Error) => void) that the extension |
14 |
| -// host can call to run the tests. The test runner is expected to use console.log |
15 |
| -// to report the results back to the caller. When the tests are finished, return |
16 |
| -// a possible error to the callback or null if none. |
17 |
| - |
18 |
| -import * as testRunner from "./istanbultestrunner"; |
19 |
| - |
20 |
| -// You can directly control Mocha options by uncommenting the following lines |
21 |
| -// See https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically#set-options for more info |
22 |
| -testRunner.configure( |
23 |
| - // Mocha Options |
24 |
| - { |
25 |
| - ui: "tdd", // the TDD UI is being used in extension.test.ts (suite, test, etc.) |
26 |
| - reporter: "pm-mocha-jenkins-reporter", |
| 6 | +"use strict"; |
| 7 | + |
| 8 | +// Recommended modules, loading them here to speed up NYC init |
| 9 | +// and minimize risk of race condition |
| 10 | +import "ts-node/register"; |
| 11 | +import "source-map-support/register"; |
| 12 | + |
| 13 | +import * as Mocha from "mocha"; |
| 14 | +// Simulates the recommended config option |
| 15 | +// extends: "@istanbuljs/nyc-config-typescript", |
| 16 | +import * as baseConfig from "@istanbuljs/nyc-config-typescript"; |
| 17 | +import * as glob from "glob"; |
| 18 | +import * as path from "path"; |
| 19 | + |
| 20 | +const NYC = require("nyc"); |
| 21 | + |
| 22 | +// Linux: prevent a weird NPE when mocha on Linux requires the window size from the TTY |
| 23 | +// Since we are not running in a tty environment, we just implementt he method statically |
| 24 | +const tty = require("tty"); |
| 25 | +if (!tty.getWindowSize) { |
| 26 | + tty.getWindowSize = (): number[] => { |
| 27 | + return [80, 75]; |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +export async function run(): Promise<void> { |
| 32 | + const testsRoot = path.resolve(__dirname, ".."); |
| 33 | + |
| 34 | + process.env.JUNIT_REPORT_PATH = |
| 35 | + path.join(__dirname, "..", "..") + "/test-reports/test-results-ext.xml"; |
| 36 | + |
| 37 | + // Setup coverage pre-test, including post-test hook to report |
| 38 | + const nyc = new NYC({ |
| 39 | + ...baseConfig, |
| 40 | + cwd: path.join(__dirname, "..", "..", ".."), |
| 41 | + reporter: ["text-summary", "html", "lcov", "cobertura"], |
| 42 | + all: true, |
| 43 | + silent: false, |
| 44 | + instrument: true, |
| 45 | + hookRequire: true, |
| 46 | + hookRunInContext: true, |
| 47 | + hookRunInThisContext: true, |
| 48 | + include: ["out/**/*.js"], |
| 49 | + exclude: [ |
| 50 | + "out/test/**", |
| 51 | + "**/node_modules/**", |
| 52 | + "**/libs/**", |
| 53 | + "**/lib/**", |
| 54 | + "**/htmlcontent/**/*.js", |
| 55 | + "**/reactviews/**/*.js", |
| 56 | + "**/*.bundle.js", |
| 57 | + ], |
| 58 | + tempDir: "./coverage/.nyc_output", |
| 59 | + }); |
| 60 | + await nyc.reset(); |
| 61 | + await nyc.wrap(); |
| 62 | + |
| 63 | + // Print a warning for any module that should be instrumented and is already loaded, |
| 64 | + // delete its cache entry and re-require |
| 65 | + // NOTE: This would not be a good practice for production code (possible memory leaks), but can be accepted for unit tests |
| 66 | + Object.keys(require.cache) |
| 67 | + .filter((f) => nyc.exclude.shouldInstrument(f)) |
| 68 | + .forEach((m) => { |
| 69 | + console.warn("Module loaded before NYC, invalidating:", m); |
| 70 | + delete require.cache[m]; |
| 71 | + require(m); |
| 72 | + }); |
| 73 | + |
| 74 | + // Debug which files will be included/excluded |
| 75 | + // console.log('Glob verification', await nyc.exclude.glob(nyc.cwd)); |
| 76 | + |
| 77 | + // Create the mocha test |
| 78 | + const mocha = new Mocha({ |
| 79 | + ui: "tdd", |
| 80 | + timeout: 10 * 1000, |
| 81 | + reporter: "mocha-junit-reporter", |
27 | 82 | reporterOptions: {
|
28 |
| - junit_report_name: "Extension Tests", |
29 |
| - junit_report_path: |
30 |
| - __dirname + "../../test-reports/extension_tests.xml", |
31 |
| - junit_report_stack: 1, |
| 83 | + mochaFile: path.join( |
| 84 | + __dirname, |
| 85 | + "..", |
| 86 | + "..", |
| 87 | + "..", |
| 88 | + "test-reports", |
| 89 | + "test-results-ext.xml", |
| 90 | + ), |
32 | 91 | },
|
33 |
| - useColors: true, // colored output from test results |
34 |
| - }, |
35 |
| - // Coverage configuration options |
36 |
| - { |
37 |
| - coverConfig: "../../../coverconfig.json", |
38 |
| - }, |
39 |
| -); |
40 |
| - |
41 |
| -module.exports = testRunner; |
| 92 | + }); |
| 93 | + (mocha.options as any).color = true; |
| 94 | + |
| 95 | + // Add all files to the test suite |
| 96 | + const files = glob.sync("**/*.test.js", { cwd: testsRoot }); |
| 97 | + files.forEach((f) => mocha.addFile(path.resolve(testsRoot, f))); |
| 98 | + |
| 99 | + const failures: number = await new Promise((resolve) => mocha.run(resolve)); |
| 100 | + await nyc.writeCoverageFile(); |
| 101 | + |
| 102 | + // Capture text-summary reporter's output and log it in console |
| 103 | + console.log(await captureStdout(nyc.report.bind(nyc))); |
| 104 | + |
| 105 | + if (failures > 0) { |
| 106 | + throw new Error(`${failures} tests failed.`); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +async function captureStdout(fn) { |
| 111 | + let w = process.stdout.write, |
| 112 | + buffer = ""; |
| 113 | + process.stdout.write = (s) => { |
| 114 | + buffer = buffer + s; |
| 115 | + return true; |
| 116 | + }; |
| 117 | + await fn(); |
| 118 | + process.stdout.write = w; |
| 119 | + return buffer; |
| 120 | +} |
0 commit comments