Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

swap tests to use jest and separate e2e and unit tests #56

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
vsix
.vscode-test/
*.vsix
coverage
10 changes: 10 additions & 0 deletions jest.e2e.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('path');

module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: [path.join(__dirname, 'src/test/e2e')],
testMatch: [path.join(__dirname, 'src/test/e2e/**/*.test.ts')],
verbose: true,
cache: false
};
9 changes: 9 additions & 0 deletions jest.unit.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const path = require('path');

module.exports = {
roots: [path.join(__dirname, 'src', 'test', 'unit')],
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: [path.join(__dirname, 'src/test/unit/**/*.test.ts')],
collectCoverage: true
};
11,126 changes: 7,496 additions & 3,630 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,29 @@
"package": "webpack --mode production --devtool hidden-source-map",
"compile-tests": "tsc -p . --outDir out",
"watch-tests": "tsc -p . -w --outDir out",
"pretest": "npm run compile-tests && npm run compile && npm run lint",
"pree2e-test": "npm run compile-tests && npm run compile && npm run lint",
"lint": "eslint src --ext ts",
"test": "node ./out/test/runTest.js"
"test": "npm run unit-test && npm run e2e-test",
"e2e-test": "node ./out/test/e2e/runE2eTest.js",
"unit-test": "jest -c jest.unit.config.js"
},
"devDependencies": {
"@jest/test-result": "^29.2.1",
"@types/download": "^8.0.1",
"@types/glob": "^7.2.0",
"@types/mocha": "^9.1.1",
"@types/jest": "^29.2.0",
"@types/node": "14.x",
"@types/shelljs": "^0.8.11",
"@types/tmp": "^0.2.3",
"@types/vscode": "^1.67.0",
"@typescript-eslint/eslint-plugin": "^5.21.0",
"@typescript-eslint/parser": "^5.21.0",
"@vscode/test-electron": "^2.1.3",
"@vscode/test-electron": "^2.2.0",
"eslint": "^8.14.0",
"glob": "^8.0.1",
"mocha": "^9.2.2",
"jest": "^29.2.2",
"prettier": "^2.7.1",
"ts-jest": "^29.0.3",
"ts-loader": "^9.2.8",
"typescript": "^4.6.4",
"webpack": "^5.73.0",
Expand Down
3 changes: 3 additions & 0 deletions src/test/e2e/__mocks__/vscode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// hack to inject global import
// https://github.com/DonJayamanne/gitHistoryVSCode/blob/cfcc98fd47eaab2d0dfaf2abbabd1457fb2f8910/test/extension/__mocks__/vscode.ts
module.exports = (process as any).__VSCODE as typeof import('vscode');
10 changes: 10 additions & 0 deletions src/test/e2e/extension.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {extensions} from 'vscode';

describe('Extension', () => {
it('can be installed', () => {
const extension = extensions.getExtension(
'ms-kubernetes-tools.aks-devx-tools'
);
expect(extension).toBeTruthy();
});
});
54 changes: 54 additions & 0 deletions src/test/e2e/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as jest from 'jest';
import {AggregatedResult} from '@jest/test-result';
import * as path from 'path';
import * as fs from 'fs';

const extensionRoot = path.join(__dirname, '..', '..', '..');
type Output = {results: AggregatedResult};

export async function run(): Promise<void> {
// hack from https://github.com/DonJayamanne/gitHistoryVSCode/blob/75575aa03d03a06a2b901944d65a0f58f4875e02/test/extension/testRunner.ts#L15
// because Jest doesn't let you inject global imports
(process as any).__VSCODE = require('vscode');

// load Jest config
const jestConfigFile = path.join(extensionRoot, 'jest.e2e.config.js');
const jestConfig = require(jestConfigFile);

return new Promise<void>((resolve, reject) => {
jest
.runCLI(jestConfig, [extensionRoot])
.catch((err) => {
delete (process as any).__VSCODE;
console.log('FAILING IN RUNNING');
return reject(err);
})
.then((out) => {
delete (process as any).__VSCODE;
if (!out) {
return resolve();
}

const {results} = out;
if (results.numFailedTestSuites || results.numFailedTests) {
// If we reject, VS Code doesn't exit gracefully
const extensionDevelopmentPath = path.resolve(
__dirname,
'../../../'
);
const tempDir = path.join(extensionDevelopmentPath, 'temp');
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir);
}
const testFailedPath = path.join(
tempDir,
'aks-devx-tools.e2e.failed'
);

fs.appendFileSync(testFailedPath, 'failed');
}

return resolve();
});
});
}
37 changes: 37 additions & 0 deletions src/test/e2e/runE2eTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as path from 'path';
import * as fs from 'fs';
import {runTests} from '@vscode/test-electron';

async function main() {
try {
const extensionDevelopmentPath = path.resolve(__dirname, '../../../');
const extensionTestsPath = path.resolve(__dirname, './index');

// create test output
const tempDir = path.join(extensionDevelopmentPath, 'temp');
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir);
}
const testFailedPath = path.join(tempDir, 'aks-devx-tools.e2e.failed');
if (fs.existsSync(testFailedPath)) {
fs.unlinkSync(testFailedPath);
}

// Download VS Code, unzip it and run the integration test
await runTests({
extensionDevelopmentPath,
extensionTestsPath
});

// check if tests failed
if (fs.existsSync(testFailedPath)) {
console.error('Tests failed');
process.exit(1);
}
} catch (err) {
console.error('Failed to run tests:', err);
process.exit(1);
}
}

main();
23 changes: 0 additions & 23 deletions src/test/runTest.ts

This file was deleted.

15 changes: 0 additions & 15 deletions src/test/suite/extension.test.ts

This file was deleted.

38 changes: 0 additions & 38 deletions src/test/suite/index.ts

This file was deleted.

7 changes: 7 additions & 0 deletions src/test/unit/example.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// this is an example unit test
// once actual tests are added, this will be removed
describe('Example unit test', () => {
it('tests something', () => {
expect(1).toEqual(1);
});
});