diff --git a/helloworld-test-sample/.vscode/launch.json b/helloworld-test-sample/.vscode/launch.json index de547d6e..e5cb50d5 100644 --- a/helloworld-test-sample/.vscode/launch.json +++ b/helloworld-test-sample/.vscode/launch.json @@ -21,7 +21,7 @@ "runtimeExecutable": "${execPath}", "args": [ "--extensionDevelopmentPath=${workspaceFolder}", - "--extensionTestsPath=${workspaceFolder}/out/test/suite/testRunner" + "--extensionTestsPath=${workspaceFolder}/out/test/suite" ], "outFiles": ["${workspaceFolder}/out/test/**/*.js"], "preLaunchTask": "npm: watch" diff --git a/helloworld-test-sample/package.json b/helloworld-test-sample/package.json index bd211ff5..9ed8ef3d 100644 --- a/helloworld-test-sample/package.json +++ b/helloworld-test-sample/package.json @@ -27,12 +27,15 @@ "vscode:prepublish": "npm run compile", "compile": "tsc -p ./", "lint": "tslint -p ./", - "watch": "tsc -watch -p ./" + "watch": "tsc -watch -p ./", + "test": "node ./out/test/runTest.js" }, "devDependencies": { + "@types/glob": "^7.1.1", "@types/mocha": "^5.2.6", "@types/node": "^8.10.25", "@types/vscode": "^1.32.0", + "glob": "^7.1.4", "mocha": "^6.1.4", "source-map-support": "^0.5.12", "tslint": "^5.16.0", diff --git a/helloworld-test-sample/test/testRunner.ts b/helloworld-test-sample/test/complexTestRunner.ts similarity index 71% rename from helloworld-test-sample/test/testRunner.ts rename to helloworld-test-sample/test/complexTestRunner.ts index af62551b..9a20811a 100644 --- a/helloworld-test-sample/test/testRunner.ts +++ b/helloworld-test-sample/test/complexTestRunner.ts @@ -3,10 +3,10 @@ import * as Mocha from 'mocha'; export function run(testsRoot: string, cb: (error: any, failures?: number) => void): void { let mocha = new Mocha({ - ui: 'tdd', - reporter: 'nyan' + ui: 'tdd' }); - mocha.useColors(true) + mocha.reporter('spec'); + mocha.useColors(true); const files = [path.resolve(__dirname, '../../out/test/suite/extension.test.js')]; @@ -26,12 +26,14 @@ export function run(testsRoot: string, cb: (error: any, failures?: number) => vo return true; }; - mocha.run(failures => { - cb(null, failures); - }).on('test end', () => { - process.stdout.write = processStdoutWrite; - console.log('\n' + stdOutMessages + '\n'); - }) + mocha + .run(failures => { + cb(null, failures); + }) + .on('test end', () => { + process.stdout.write = processStdoutWrite; + console.log('\n' + stdOutMessages + '\n'); + }); } catch (err) { cb(err); } diff --git a/helloworld-test-sample/test/runTest.ts b/helloworld-test-sample/test/runTest.ts index d282c406..aa147c6c 100644 --- a/helloworld-test-sample/test/runTest.ts +++ b/helloworld-test-sample/test/runTest.ts @@ -1,25 +1,25 @@ -import * as path from 'path' +import * as path from 'path'; -import { runTests } from 'vscode-test' +import { runTests } from 'vscode-test'; async function go() { - try { - const extensionPath = path.resolve(__dirname, '../../') - const testRunnerPath = path.resolve(__dirname, './testRunner.js') - const testWorkspace = path.resolve(__dirname, '../../test/suite/fixture') + try { + const extensionPath = path.resolve(__dirname, '../../'); + const testRunnerPath = path.resolve(__dirname, './suite'); + const testWorkspace = path.resolve(__dirname, '../../test/suite/fixture'); - /** - * Basic usage - */ - await runTests({ - extensionPath, - testRunnerPath, - testWorkspace - }) - } catch (err) { - console.error('Failed to run tests') - process.exit(1) - } + await runTests({ + // The folder containing the Extension Manifest package.json + extensionPath, + // The path to test runner + testRunnerPath, + // The workspace to open on starting up VS Code + testWorkspace + }); + } catch (err) { + console.error('Failed to run tests'); + process.exit(1); + } } -go() \ No newline at end of file +go(); diff --git a/helloworld-test-sample/test/suite/extension.test.ts b/helloworld-test-sample/test/suite/extension.test.ts index 2b1ad1ee..820cf906 100644 --- a/helloworld-test-sample/test/suite/extension.test.ts +++ b/helloworld-test-sample/test/suite/extension.test.ts @@ -1,28 +1,18 @@ import * as assert from 'assert'; +import { before } from 'mocha'; // You can import and use all API from the 'vscode' module // as well as import your extension to test it import * as vscode from 'vscode'; // import * as myExtension from '../extension'; -// Defines a Mocha test suite to group tests of similar kind together -suite('Extension Tests', function() { - // Defines a Mocha unit test - test('Something 1', function() { - vscode.window.showInformationMessage('test') +suite('Extension Test Suite', () => { + before(() => { + vscode.window.showInformationMessage('Start all tests.'); + }); + + test('Sample test', () => { assert.equal(-1, [1, 2, 3].indexOf(5)); assert.equal(-1, [1, 2, 3].indexOf(0)); }); - - // test('Something 1', function() { assert.equal(1, 2); }); - // test('Something 1', function() { assert.equal(1, 1); }); - // test('Something 1', function() { assert.equal(1, 2); }); - // test('Something 1', function() { assert.equal(1, 2); }); - // test('Something 1', function() { assert.equal(1, 2); }); - // test('Something 1', function() { assert.equal(1, 1); }); - // test('Something 1', function() { assert.equal(1, 2); }); - // test('Something 1', function() { assert.equal(1, 2); }); - // test('Something 1', function() { assert.equal(1, 1); }); - // test('Something 1', function() { assert.equal(1, 2); }); - // test('Something 1', function() { assert.equal(1, 2); }); }); diff --git a/helloworld-test-sample/test/suite/index.ts b/helloworld-test-sample/test/suite/index.ts new file mode 100644 index 00000000..49b0f207 --- /dev/null +++ b/helloworld-test-sample/test/suite/index.ts @@ -0,0 +1,32 @@ +import * as path from 'path'; +import * as Mocha from 'mocha'; +import * as glob from 'glob'; + +export function run(testsRoot: string, cb: (error: any, failures?: number) => void): void { + // Create the mocha test + const mocha = new Mocha({ + ui: 'tdd', + timeout: 10000 + }); + mocha.useColors(true); + + glob('**/**.test.js', { cwd: testsRoot }, (err, files) => { + if (err) { + return cb(err); + } + + // Add files to the test suite + files.forEach(f => mocha.addFile(path.resolve(testsRoot, f))); + + try { + // Run the mocha test + mocha + .run(failures => { + cb(null, failures); + }); + + } catch (err) { + cb(err); + } + }); +} diff --git a/helloworld-test-sample/yarn.lock b/helloworld-test-sample/yarn.lock index 97a5266d..da59464a 100644 --- a/helloworld-test-sample/yarn.lock +++ b/helloworld-test-sample/yarn.lock @@ -18,11 +18,35 @@ esutils "^2.0.2" js-tokens "^4.0.0" +"@types/events@*": + version "3.0.0" + resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7" + integrity sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g== + +"@types/glob@^7.1.1": + version "7.1.1" + resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.1.tgz#aa59a1c6e3fbc421e07ccd31a944c30eba521575" + integrity sha512-1Bh06cbWJUHMC97acuD6UMG29nMt0Aqz1vF3guLfG+kHHJhy3AyohZFFxYk2f7Q1SQIrNwvncxAE0N/9s70F2w== + dependencies: + "@types/events" "*" + "@types/minimatch" "*" + "@types/node" "*" + +"@types/minimatch@*": + version "3.0.3" + resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d" + integrity sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA== + "@types/mocha@^5.2.6": version "5.2.6" resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-5.2.6.tgz#b8622d50557dd155e9f2f634b7d68fd38de5e94b" integrity sha512-1axi39YdtBI7z957vdqXI4Ac25e7YihYQtJa+Clnxg1zTJEaIRbndt71O3sP4GAMgiAm0pY26/b9BrY4MR/PMw== +"@types/node@*": + version "12.0.2" + resolved "https://registry.yarnpkg.com/@types/node/-/node-12.0.2.tgz#3452a24edf9fea138b48fad4a0a028a683da1e40" + integrity sha512-5tabW/i+9mhrfEOUcLDu2xBPsHJ+X5Orqy9FKpale3SjDA17j5AEpYq5vfy3oAeAHGcvANRCO3NV3d2D6q3NiA== + "@types/node@^8.10.25": version "8.10.48" resolved "https://registry.yarnpkg.com/@types/node/-/node-8.10.48.tgz#e385073561643a9ba6199a1985ffc03530f90781" @@ -320,7 +344,7 @@ glob@7.1.3: once "^1.3.0" path-is-absolute "^1.0.0" -glob@^7.1.1: +glob@^7.1.1, glob@^7.1.4: version "7.1.4" resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.4.tgz#aa608a2f6c577ad357e1ae5a5c26d9a8d1969255" integrity sha512-hkLPepehmnKk41pUGm3sYxoFs/umurYfYJCerbXEyFIWcAzvpipAgVkBqqT9RBKMGjnq6kMuyYwha6csxbiM1A==