Clean up Mocha, Sinon and Chai

This removes the Mocha, Sinon and Chai-related packages and removes
unused code from the test suite.
This commit is contained in:
Koen Vlaswinkel
2022-11-24 17:24:43 +01:00
parent 5841fbccd7
commit e55b8a366e
11 changed files with 42 additions and 1374 deletions

64
.vscode/launch.json vendored
View File

@@ -63,60 +63,52 @@
},
{
"name": "Launch Integration Tests - No Workspace (vscode-codeql)",
"type": "extensionHost",
"type": "node",
"request": "launch",
"runtimeExecutable": "${execPath}",
"program": "${workspaceFolder}/extensions/ql-vscode/node_modules/jest/bin/jest.js",
"showAsyncStacks": true,
"cwd": "${workspaceFolder}/extensions/ql-vscode",
"args": [
"--extensionDevelopmentPath=${workspaceRoot}/extensions/ql-vscode",
"--extensionTestsPath=${workspaceRoot}/extensions/ql-vscode/out/vscode-tests/no-workspace/index",
"--disable-workspace-trust",
"--disable-extensions",
"--disable-gpu"
"--projects",
"out/vscode-tests/no-workspace"
],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/extensions/ql-vscode/out/**/*.js",
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
},
{
"name": "Launch Integration Tests - Minimal Workspace (vscode-codeql)",
"type": "extensionHost",
"type": "node",
"request": "launch",
"runtimeExecutable": "${execPath}",
"program": "${workspaceFolder}/extensions/ql-vscode/node_modules/jest/bin/jest.js",
"showAsyncStacks": true,
"cwd": "${workspaceFolder}/extensions/ql-vscode",
"args": [
"--extensionDevelopmentPath=${workspaceRoot}/extensions/ql-vscode",
"--extensionTestsPath=${workspaceRoot}/extensions/ql-vscode/out/vscode-tests/minimal-workspace/index",
"--disable-workspace-trust",
"--disable-extensions",
"--disable-gpu",
"${workspaceRoot}/extensions/ql-vscode/test/data"
"--projects",
"out/vscode-tests/minimal-workspace"
],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/extensions/ql-vscode/out/**/*.js",
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
},
{
"name": "Launch Integration Tests - With CLI",
"type": "extensionHost",
"type": "node",
"request": "launch",
"runtimeExecutable": "${execPath}",
"program": "${workspaceFolder}/extensions/ql-vscode/node_modules/jest/bin/jest.js",
"showAsyncStacks": true,
"cwd": "${workspaceFolder}/extensions/ql-vscode",
"args": [
"--extensionDevelopmentPath=${workspaceRoot}/extensions/ql-vscode",
"--extensionTestsPath=${workspaceRoot}/extensions/ql-vscode/out/vscode-tests/cli-integration/index",
"--disable-workspace-trust",
"--disable-gpu",
"--disable-extension",
"eamodio.gitlens",
"--disable-extension",
"github.codespaces",
"--disable-extension",
"github.copilot",
"${workspaceRoot}/extensions/ql-vscode/src/vscode-tests/cli-integration/data",
// Uncomment the last line and modify the path to a checked out
// instance of the codeql repository so the libraries are
// available in the workspace for the tests.
// "${workspaceRoot}/../codeql"
"--projects",
"out/vscode-tests/cli-integration"
],
"env": {
// Optionally, set the version to use for the integration tests.
@@ -130,11 +122,19 @@
// If not specified, one will be downloaded automatically.
// This option overrides the CLI_VERSION option.
// "CLI_PATH": "${workspaceRoot}/../semmle-code/target/intree/codeql/codeql",
// Uncomment the last line and modify the path to a checked out
// instance of the codeql repository so the libraries are
// available in the workspace for the tests.
// "TEST_CODEQL_PATH": "${workspaceRoot}/../codeql",
},
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/extensions/ql-vscode/out/**/*.js",
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
},
{
"name": "Launch Storybook",

View File

@@ -37,7 +37,7 @@
"javascript.preferences.quoteStyle": "single",
"editor.wordWrapColumn": 100,
"jest.rootPath": "./extensions/ql-vscode",
"jest.autoRun": "watch",
"jest.autoRun": "off",
"jest.nodeEnv": {
"LANG": "en-US",
"TZ": "UTC"

File diff suppressed because it is too large Load Diff

View File

@@ -1343,8 +1343,6 @@
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^12.1.5",
"@testing-library/user-event": "^14.4.3",
"@types/chai": "^4.1.7",
"@types/chai-as-promised": "~7.1.2",
"@types/child-process-promise": "^2.2.1",
"@types/classnames": "~2.2.9",
"@types/d3": "^7.4.0",
@@ -1359,17 +1357,13 @@
"@types/jest": "^29.0.2",
"@types/js-yaml": "^3.12.5",
"@types/jszip": "~3.1.6",
"@types/mocha": "^9.0.0",
"@types/nanoid": "^3.0.0",
"@types/node": "^16.11.25",
"@types/node-fetch": "~2.5.2",
"@types/proxyquire": "~1.3.28",
"@types/react": "^17.0.2",
"@types/react-dom": "^17.0.2",
"@types/sarif": "~2.1.2",
"@types/semver": "~7.2.0",
"@types/sinon": "~7.5.2",
"@types/sinon-chai": "~3.2.3",
"@types/stream-chain": "~2.0.1",
"@types/stream-json": "~1.7.1",
"@types/tar-stream": "^2.2.2",
@@ -1386,8 +1380,6 @@
"ansi-colors": "^4.1.1",
"applicationinsights": "^2.3.5",
"babel-loader": "^8.2.5",
"chai": "^4.2.0",
"chai-as-promised": "~7.1.1",
"css-loader": "~3.1.0",
"del": "^6.0.0",
"eslint": "^8.23.1",
@@ -1409,13 +1401,8 @@
"jest-runner-vscode": "^3.0.1",
"lint-staged": "~10.2.2",
"mini-css-extract-plugin": "^2.6.1",
"mocha": "^10.0.0",
"mocha-sinon": "~2.1.2",
"npm-run-all": "^4.1.5",
"prettier": "^2.7.1",
"proxyquire": "~2.1.3",
"sinon": "~14.0.0",
"sinon-chai": "~3.5.0",
"tar-stream": "^2.2.0",
"through2": "^4.0.2",
"ts-jest": "^29.0.1",
@@ -1431,7 +1418,7 @@
"husky": {
"hooks": {
"pre-commit": "npm run format-staged",
"pre-push": "npm run lint && scripts/forbid-mocha-only"
"pre-push": "npm run lint && scripts/forbid-test-only"
}
},
"lint-staged": {

View File

@@ -3,7 +3,7 @@ module.exports = {
project: ["../../tsconfig.json"],
},
env: {
mocha: true
jest: true,
},
rules: {
"@typescript-eslint/ban-types": [
@@ -12,11 +12,11 @@ module.exports = {
// For a full list of the default banned types, see:
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/ban-types.md
extendDefaults: true,
"types": {
types: {
// Don't complain about the `Function` type in test files. (Default is `true`.)
"Function": false,
}
}
]
}
}
Function: false,
},
},
],
},
};

View File

@@ -70,7 +70,6 @@ export function createMockQueryWithResults({
hasInterpretedResults = true,
hasMetadata = undefined,
}: {
sandbox?: sinon.SinonSandbox;
didRunSuccessfully?: boolean;
hasInterpretedResults?: boolean;
hasMetadata?: boolean;

View File

@@ -1,114 +0,0 @@
import * as path from "path";
import * as Mocha from "mocha";
import * as glob from "glob-promise";
import { ensureCli } from "./ensureCli";
import { env } from "vscode";
import { testConfigHelper } from "./test-config";
// Use this handler to avoid swallowing unhandled rejections.
process.on("unhandledRejection", (e) => {
console.error("Unhandled rejection.");
console.error(e);
// Must use a setTimeout in order to ensure the log is fully flushed before exiting
setTimeout(() => {
process.exit(-1);
}, 2000);
});
process.on("exit", (code) => {
// If the exit code is 7, then the test runner has completed, but
// there was an error in exiting vscode.
if (code === 7) {
console.warn(
"Attempted to exit with code 7. This is likely due to a failure to exit vscode. Ignoring this and exiting with code 0.",
);
process.exit(0);
}
});
/**
* Helper function that runs all Mocha tests found in the
* given test root directory.
*
* For each integration test suite, `vscode-test` expects
* a test runner script exporting a function with the signature:
* ```ts
* export function run(): Promise<void>
* ```
*
* To create an integration test suite:
* - create a directory beside this file
* - create integration tests in the directory, named `<name>.test.ts`
* - create an `index.ts` file in the directory, containing:
* ```ts
* import { runTestsInDirectory } from '../index-template';
* export function run(): Promise<void> {
* return runTestsInDirectory(__dirname);
* }
* ```
*
* After https://github.com/microsoft/TypeScript/issues/420 is implemented,
* this pattern can be expressed more neatly using a module interface.
*/
export async function runTestsInDirectory(
testsRoot: string,
useCli = false,
): Promise<void> {
// Create the mocha test
const mocha = new Mocha({
ui: "bdd",
color: true,
globalSetup: [],
globalTeardown: [],
} as any);
(mocha.options as any).globalSetup.push(
// convert this function into an noop since it should not run during tests.
// If it does run during tests, then it can cause some testing environments
// to hang.
((env as any).openExternal = () => {
/**/
}),
);
await ensureCli(useCli);
console.log(`Adding test cases and helpers from ${testsRoot}`);
const files = await glob("**/**.js", { cwd: testsRoot });
// Add test files to the test suite
files
.filter((f) => f.endsWith(".test.js"))
.forEach((f) => {
console.log(`Adding test file ${f}`);
mocha.addFile(path.resolve(testsRoot, f));
});
// Setup the config helper. This needs to run before other helpers so any config they setup
// is restored.
await testConfigHelper(mocha);
// Add helpers. Helper files add global setup and teardown blocks
// for a test run.
files
.filter((f) => f.endsWith(".helper.js"))
.forEach((f) => {
console.log(`Executing helper ${f}`);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const helper = require(path.resolve(testsRoot, f)).default;
helper(mocha);
});
return new Promise((resolve, reject) => {
// Run the mocha test
mocha.run((failures) => {
if (failures > 0) {
reject(new Error(`${failures} tests failed.`));
return;
}
resolve();
});
});
}

View File

@@ -2,9 +2,6 @@ import * as path from "path";
import * as vscode from "vscode";
import * as determiningSelectedQueryTest from "./determining-selected-query-test";
// Temporary until Mocha is fully removed. This is necessary for passing timeouts to `it`.
declare let it: jest.It;
describe("launching with a minimal workspace", () => {
const ext = vscode.extensions.getExtension("GitHub.vscode-codeql");
it("should install the extension", () => {

View File

@@ -26,9 +26,6 @@ import {
import { EvaluationResult, QueryResultType } from "../../pure/legacy-messages";
import { sleep } from "../../pure/time";
// Temporary until Mocha is fully removed. This is necessary for passing timeouts to `it`.
declare let it: jest.It;
describe("query-results", () => {
let queryPath: string;
let cnt = 0;

View File

@@ -105,29 +105,6 @@ export const getTestSetting = (
return TEST_SETTINGS.find((testSetting) => testSetting.setting === setting);
};
export const testConfigHelper = async (mocha: Mocha) => {
// Allow extra time to read settings. Sometimes this can time out.
mocha.timeout(20000);
// Read in all current settings
await Promise.all(TEST_SETTINGS.map((setting) => setting.initialSetup()));
mocha.rootHooks({
async beforeEach() {
// Reset the settings to their initial values before each test
await Promise.all(TEST_SETTINGS.map((setting) => setting.setup()));
},
async afterAll() {
// Restore all settings to their default values after each test suite
// Only do this outside of CI since the sometimes hangs on CI.
if (process.env.CI !== "true") {
await Promise.all(
TEST_SETTINGS.map((setting) => setting.restoreToInitialValues()),
);
}
},
});
};
export const jestTestConfigHelper = async () => {
// Read in all current settings
await Promise.all(TEST_SETTINGS.map((setting) => setting.initialSetup()));