合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
[TOC] ## 概述 [官方示例](https://github.com/microsoft/vscode-test/blob/main/sample/src/test/runTest.ts) 可以方便的对 vscode 的接口进行测试 ## 示例 测试目录 ``` src └── test ├── runTest.ts └── suite1 ├── extension.test.ts └── index.ts ``` <details> <summary>runTest.ts</summary> ``` import * as path from "path"; import { runTests } from '@vscode/test-electron'; async function main() { try { const extensionDevelopmentPath = path.resolve(__dirname, '../../'); const extensionTestsPath = path.resolve(__dirname, './suite1/'); // Download VS Code, unzip it and run the integration test await runTests({ extensionDevelopmentPath, extensionTestsPath }); // 存在多个时,在运行 runTest // const extensionTestsPath2 = path.resolve(__dirname, './suite2/'); // // Download VS Code, unzip it and run the integration test // await runTests({ // extensionDevelopmentPath, // extensionTestsPath2 }); } catch (err) { console.error("Failed to run tests"); process.exit(1); } } main(); ``` </details> <details> <summary>index.ts</summary> ``` import * as path from 'path'; import * as Mocha from 'mocha'; import { glob } from 'glob'; export function run(): Promise<void> { // Create the mocha test const mocha = new Mocha({ ui: 'tdd' }); const testsRoot = path.resolve(__dirname, '..'); return new Promise((c, e) => { glob('**/**.test.js', { cwd: testsRoot }).then((files) => { console.log("===========files =============",files); // 使用 glob.glob 返回 Promise files.forEach(f => mocha.addFile(path.resolve(testsRoot, f))); mocha.run(failures => { if (failures > 0) { e(new Error(`${failures} tests failed.`)); } else { c(); } }); }).catch(err => { return e(err); }); }); } ``` </details> <details> <summary>extension.test.ts</summary> ``` import * as assert from 'assert'; import { before, describe, it } from 'mocha'; import * as vscode from 'vscode'; describe('Extension Test Suite', () => { before(() => { vscode.window.showInformationMessage('开始运行测试!'); }); it('示例测试用例', () => { assert.strictEqual(-1, [1, 2, 3].indexOf(5)); assert.strictEqual(-1, [1, 2, 3].indexOf(0)); }); }); ``` </details> <details> <summary>package.json</summary> ``` "scripts": { "test": "node ./out/test/runTest.js" }, "devDependencies": { "@eslint/js": "^9.13.0", "@stylistic/eslint-plugin": "^2.9.0", "@types/glob": "^7.1.1", "@types/mocha": "^10.0.1", "@types/node": "^20", "@types/vscode": "^1.73.0", "@vscode/test-electron": "^2.3.9", "eslint": "^9.13.0", "glob": "^7.1.4", "mocha": "^10.2.0", "source-map-support": "^0.5.12", "typescript": "^5.7.2", "typescript-eslint": "^8.16.0" } ``` </details> <br/> **运行** ``` pnpm run test ```