Skip to content

Commit

Permalink
Integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daddykotex committed Aug 15, 2022
1 parent b94f978 commit e0144bc
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
node_modules
*.vsix
out/
.vscode-test
23 changes: 23 additions & 0 deletions it/runTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as path from "path";

import { runTests } from "@vscode/test-electron";

async function main() {
try {
// The folder containing the Extension Manifest package.json
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath = path.resolve(__dirname, "../../");

// The path to test runner
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, "./suite/index");

// Download VS Code, unzip it and run the integration test
await runTests({ extensionDevelopmentPath, extensionTestsPath });
} catch (err) {
console.error("Failed to run tests");
process.exit(1);
}
}

main();
8 changes: 8 additions & 0 deletions it/suite/extension.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as vscode from "vscode";

test("Sample test", () => {
vscode.window.showInformationMessage("Start all tests.");

expect([1, 2, 3].indexOf(5)).toEqual(-1);
expect([1, 2, 3].indexOf(0)).toEqual(-1);
});
40 changes: 40 additions & 0 deletions it/suite/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { runCLI } from "jest";

interface ITestRunner {
run(testsRoot: string, clb: (error?: Error, failures?: number) => void): void;
}
const path = require("path");

const jestTestRunnerForVSCodeE2E: ITestRunner = {
run(
testsRoot: string,
reportTestResults: (error?: Error, failures?: number) => void
): void {
const projectRootPath = process.cwd();
const config = path.join(projectRootPath, "jest.e2e.config.js");

runCLI({ config } as any, [projectRootPath])
.then((jestCliCallResult) => {
jestCliCallResult.results.testResults.forEach((testResult) => {
testResult.testResults
.filter((assertionResult) => assertionResult.status === "passed")
.forEach(({ ancestorTitles, title, status }) => {
console.info(` ● ${ancestorTitles} > ${title} (${status})`);
});
});

jestCliCallResult.results.testResults.forEach((testResult) => {
if (testResult.failureMessage) {
console.error(testResult.failureMessage);
}
});

reportTestResults(undefined, jestCliCallResult.results.numFailedTests);
})
.catch((errorCaughtByJestRunner) => {
reportTestResults(errorCaughtByJestRunner, 0);
});
},
};

module.exports = jestTestRunnerForVSCodeE2E;
21 changes: 21 additions & 0 deletions it/vscode-environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// see https://github.com/microsoft/vscode-test/issues/37#issuecomment-700167820
const NodeEnvironment = require("jest-environment-node");
const vscode = require("vscode");

class VsCodeEnvironment extends NodeEnvironment {
async setup() {
await super.setup();
this.global.vscode = vscode;
}

async teardown() {
this.global.vscode = {};
await super.teardown();
}

runScript(script) {
return super.runScript(script);
}
}

module.exports = VsCodeEnvironment;
2 changes: 2 additions & 0 deletions it/vscode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// see https://github.com/microsoft/vscode-test/issues/37#issuecomment-700167820
module.exports = global.vscode;
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ module.exports = {
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
testMatch: ["<rootDir>/tests/**/*.spec.ts"],
};
11 changes: 11 additions & 0 deletions jest.e2e.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const path = require("path");

module.exports = {
moduleFileExtensions: ["js"],
testMatch: ["<rootDir>/out/it/suite/**.test.js"],
testEnvironment: "./it/vscode-environment.js",
verbose: true,
moduleNameMapper: {
vscode: path.join(__dirname, "it", "vscode.js"),
},
};
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"watch": "tsc -watch -p ./",
"clean": "rm -rf node_modules/ out/",
"test": "jest",
"test:it": "yarn run compile && node ./out/it/runTest.js",
"test:watch": "yarn run test --watch",
"build": "vsce package --yarn",
"format": "prettier --write '**/*.{ts,js,json,yml}'",
Expand All @@ -87,7 +88,7 @@
"extension.vsix"
],
"engines": {
"vscode": "^1.43.0"
"vscode": "^1.70.0"
},
"dependencies": {
"follow-redirects": "^1.14.9",
Expand All @@ -97,14 +98,15 @@
"@types/follow-redirects": "^1.14.1",
"@types/jest": "^27.4.0",
"@types/node": "^17.0.21",
"@types/vscode": "1.43.0",
"@types/vscode": "^1.70.0",
"@typescript-eslint/parser": "^2.3.0",
"@vscode/test-electron": "^2.1.5",
"eslint": "^8.10.0",
"jest": "^27.5.1",
"jest-environment-node": "^27.5.1",
"prettier": "^2.5.1",
"ts-jest": "^27.1.3",
"typescript": "^4.6.2",
"vsce": "^2.10.0",
"vscode-test": "^1.6.1"
"vsce": "^2.10.0"
}
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"target": "es2019",
"lib": ["ES2019"],
"outDir": "out",
"rootDirs": ["src", "tests"],
"rootDirs": ["src", "tests", "it"],
"sourceMap": true
},
"include": ["src"],
"include": ["src", "tests", "it"],
"exclude": ["node_modules", ".vscode-test"]
}
28 changes: 14 additions & 14 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e0144bc

Please sign in to comment.