microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
test/index.ts
144lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | // This file is used by VS Code's default test runner to configure Mocha before the test run. |
| 5 | |
| 6 | import * as path from "path"; |
| 7 | import Mocha = require("mocha"); |
| 8 | import NYCPackage from "nyc"; |
| 9 | |
| 10 | async function loadGlob(): Promise<any> { |
| 11 | try { |
| 12 | // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 13 | return require("glob"); |
| 14 | } catch (e) { |
| 15 | if (e && /ERR_REQUIRE_ESM/.test(String(e))) { |
| 16 | return await import("glob"); |
| 17 | } |
| 18 | throw e; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | function setupCoverage(): NYCPackage { |
| 23 | // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 24 | const NYC = require("nyc"); |
| 25 | const nyc = new NYC({ |
| 26 | cwd: path.join(__dirname, ".."), |
| 27 | include: ["src/**/*.js"], |
| 28 | exclude: ["test/**", ".vscode-test/**"], |
| 29 | reporter: ["text", "html"], |
| 30 | all: true, |
| 31 | instrument: true, |
| 32 | hookRequire: true, |
| 33 | hookRunInContext: true, |
| 34 | hookRunInThisContext: true, |
| 35 | }); |
| 36 | |
| 37 | nyc.reset(); |
| 38 | nyc.wrap(); |
| 39 | |
| 40 | return nyc; |
| 41 | } |
| 42 | |
| 43 | export async function run(): Promise<void> { |
| 44 | const nyc = process.env.COVERAGE ? setupCoverage() : null; |
| 45 | |
| 46 | // Provide harmless stubs for Mocha BDD hooks if some non-target test files |
| 47 | // (e.g. smoke tests) get loaded indirectly before Mocha initialization. |
| 48 | const hookNames = ["before", "after", "beforeEach", "afterEach"]; // minimal surface |
| 49 | for (const hn of hookNames) { |
| 50 | if (!(global as any)[hn]) { |
| 51 | (global as any)[hn] = () => { |
| 52 | /* stub */ |
| 53 | }; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | const mocha = new Mocha({ |
| 58 | ui: "tdd", |
| 59 | grep: new RegExp("(debuggerContext|localizationContext)"), // Do not run tests intended for the debuggerContext and localizationContext |
| 60 | reporter: "mocha-multi-reporters", |
| 61 | reporterOptions: { |
| 62 | reporterEnabled: "mocha-junit-reporter, mochawesome", |
| 63 | mochaJunitReporterReporterOptions: { |
| 64 | mochaFile: path.join(__dirname, "ExtensionTests.xml"), |
| 65 | }, |
| 66 | mochawesomeReporterOptions: { |
| 67 | reportDir: `${path.resolve(__dirname, "..")}/mochawesome-report`, |
| 68 | reportFilename: "Rn-Test-Report", |
| 69 | quiet: true, |
| 70 | }, |
| 71 | }, |
| 72 | color: true, |
| 73 | }); |
| 74 | |
| 75 | mocha.invert(); |
| 76 | |
| 77 | const testsRoot = __dirname; |
| 78 | const globPkg = await loadGlob(); |
| 79 | |
| 80 | // Cross-version glob: supports glob v11 Promise API and glob v7 callback API |
| 81 | const getTestFiles = (pattern: string, cwd: string): Promise<string[]> => { |
| 82 | const candidateFns: any[] = []; |
| 83 | if (globPkg) { |
| 84 | if (typeof globPkg.glob === "function") { |
| 85 | candidateFns.push(globPkg.glob); |
| 86 | } |
| 87 | if (typeof globPkg === "function") { |
| 88 | candidateFns.push(globPkg); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | for (const fn of candidateFns) { |
| 93 | try { |
| 94 | const res = fn(pattern, { cwd }); |
| 95 | if (res && typeof res.then === "function") { |
| 96 | return res as Promise<string[]>; // glob >= 11 Promise API |
| 97 | } |
| 98 | } catch (_e) { |
| 99 | // ignore and fallback to callback path |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return new Promise<string[]>((resolve, reject) => { |
| 104 | const cb = (err: Error | null, files: string[]) => (err ? reject(err) : resolve(files)); |
| 105 | for (const fn of candidateFns) { |
| 106 | try { |
| 107 | fn(pattern, { cwd }, cb); |
| 108 | return; // rely on callback |
| 109 | } catch (_e) { |
| 110 | // try next |
| 111 | } |
| 112 | } |
| 113 | reject(new Error("Unsupported glob package shape")); |
| 114 | }); |
| 115 | }; |
| 116 | |
| 117 | // Exclude smoke test bundle and localization driver; only run unit/integration tests here |
| 118 | return getTestFiles("extension/**/*.test.js", testsRoot) |
| 119 | .then(files => files.filter(f => !/[\\/]exponent[\\/]/i.test(f))) |
| 120 | .then(files => { |
| 121 | files.forEach(f => mocha.addFile(path.resolve(testsRoot, f))); |
| 122 | |
| 123 | return new Promise<void>((resolve, reject) => { |
| 124 | try { |
| 125 | mocha.run((failures: any) => { |
| 126 | if (failures > 0) { |
| 127 | reject(new Error(`${failures} tests failed.`)); |
| 128 | } else { |
| 129 | resolve(); |
| 130 | } |
| 131 | }); |
| 132 | } catch (err) { |
| 133 | reject(err as Error); |
| 134 | } |
| 135 | }); |
| 136 | }) |
| 137 | .finally(() => { |
| 138 | if (nyc) { |
| 139 | nyc.writeCoverageFile(); |
| 140 | return nyc.report(); |
| 141 | } |
| 142 | return void 0; |
| 143 | }); |
| 144 | } |
| 145 | |