Skip to content

Commit

Permalink
fix: set test state failed if error is thrown in before hook (#160)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Danielku15 <[email protected]>
  • Loading branch information
muhammadyusuf-kurbonov and Danielku15 authored Oct 20, 2024
1 parent f491294 commit 873dd26
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This extension automatically discovers and works with the `.mocharc.js/cjs/yaml/
- `syntax` Parse the file and try to extract the tests from the syntax tree.
- The `extractTimeout` limiting how long the extraction of tests for a single file is allowed to take.
- The `test` and `suite` identifiers the process extracts. Defaults to `["it", "test"]` and `["describe", "suite"]` respectively, covering Mocha's common interfaces.
- The `hooks` identifiers to avoid Mocha executing stuff on test discovery. Defaults to `["before", "after", "beforeEach", "afterEach"]`.

- `mocha-vscode.debugOptions`: options, normally found in the launch.json, to pass when debugging the extension. See [the docs](https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-attributes) for a complete list of options.

Expand Down
14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"title": "Mocha for VS Code",
"properties": {
"mocha-vscode.extractSettings": {
"markdownDescription": "Configures how tests get extracted. You can configure:\n\n- The `extractWith` mode, that specifies if tests are extracted.\n - `evaluation-cjs` (default) Translate the test file to CommonJS and evaluate it with all dependencies mocked.\n - `evaluation-cjs-full` Translate the test file to CommonJS and fully evaluate it with all dependencies.\n - `syntax` Parse the file and try to extract the tests from the syntax tree.\n- The `extractTimeout` limiting how long the extraction of tests for a single file is allowed to take.\n- The `test` and `suite` identifiers the process extracts. Defaults to `[\"it\", \"test\"]` and `[\"describe\", \"suite\"]` respectively, covering Mocha's common interfaces.\n\n- `mocha-vscode.debugOptions`: options, normally found in the launch.json, to pass when debugging the extension. See [the docs](https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-attributes) for a complete list of options.",
"markdownDescription": "Configures how tests get extracted. You can configure:\n\n- The `extractWith` mode, that specifies if tests are extracted.\n - `evaluation-cjs` (default) Translate the test file to CommonJS and evaluate it with all dependencies mocked.\n - `evaluation-cjs-full` Translate the test file to CommonJS and fully evaluate it with all dependencies.\n - `syntax` Parse the file and try to extract the tests from the syntax tree.\n- The `extractTimeout` limiting how long the extraction of tests for a single file is allowed to take.\n- The `test` and `suite` identifiers the process extracts. Defaults to `[\"it\", \"test\"]` and `[\"describe\", \"suite\"]` respectively, covering Mocha's common interfaces.\n- The `hooks` identifiers to avoid Mocha executing stuff on test discovery. Defaults to `[\"before\", \"after\", \"beforeEach\", \"afterEach\"]`\n\n- `mocha-vscode.debugOptions`: options, normally found in the launch.json, to pass when debugging the extension. See [the docs](https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-attributes) for a complete list of options.",
"type": "object",
"properties": {
"suite": {
Expand All @@ -53,6 +53,12 @@
"type": "string"
}
},
"hooks": {
"type": "array",
"items": {
"type": "string"
}
},
"extractWith": {
"type": "string",
"enum": [
Expand All @@ -74,6 +80,12 @@
"it",
"test"
],
"hooks": [
"before",
"after",
"beforeEach",
"afterEach"
],
"extractWith": "evaluation-cjs",
"extractTimeout": 10000
},
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const configFilePattern = '**/.mocharc.{js,cjs,yaml,yml,json,jsonc}';
export const defaultTestSymbols: IExtensionSettings = {
suite: ['describe', 'suite'],
test: ['it', 'test'],
hooks: ['before', 'after', 'beforeEach', 'afterEach'],
extractWith: 'evaluation-cjs',
extractTimeout: 10_000,
};
Expand Down
2 changes: 2 additions & 0 deletions src/discoverer/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ export class EvaluationTestDiscoverer implements ITestDiscoverer {
return suiteFunction;
} else if (symbols.value.test.includes(prop as string)) {
return testFunction;
} else if (symbols.value.hooks.includes(prop as string)) {
return placeholder();
} else if (prop in target) {
return target[prop]; // top-level `var` defined get set on the contextObj
} else if (prop in globalThis && !replacedGlobals.has(prop as string)) {
Expand Down
1 change: 1 addition & 0 deletions src/discoverer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface IParsedNode {
export interface IExtensionSettings {
suite: readonly string[];
test: readonly string[];
hooks: readonly string[];
extractWith: 'syntax' | 'evaluation-cjs' | 'evaluation-cjs-full';
extractTimeout: number;
}
Expand Down
11 changes: 10 additions & 1 deletion src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,16 @@ class CompiledFileTests {
for (const item of items) {
let candidate: vscode.TestItem | undefined = item;
for (let i = 0; i < path.length && candidate; i++) {
candidate = candidate.children.get(path[i]);
const pathPart = path[i];
if (
pathPart.startsWith('"before all" hook') ||
pathPart.startsWith('"before each" hook') ||
pathPart.startsWith('"after all" hook') ||
pathPart.startsWith('"after each" hook')
) {
break;
}
candidate = candidate.children.get(pathPart);
}
if (candidate !== undefined) {
return candidate;
Expand Down
125 changes: 125 additions & 0 deletions src/test/integration/with-hooks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/**
* Copyright (C) Daniel Kuschny (Danielku15) and contributors.
* Copyright (C) Microsoft Corporation. All rights reserved.
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/

import { expect } from 'chai';
import * as vscode from 'vscode';
import { captureTestRun, expectTestTree, getController } from '../util';

describe('with-hooks', () => {
it('discovers tests', async () => {
const c = await getController();

expectTestTree(c, [
[
'hello.test.js',
[
['with beforeAll hook', [['addition'], ['failing'], ['subtraction']]],
['with beforeEach hook', [['addition'], ['failing'], ['subtraction']]],
[
'with broken after hook (suite must be failed)',
[['addition'], ['failing'], ['subtraction']],
],
[
'with broken afterEach hook (suite must be failed)',
[['addition (success)'], ['failing (skipped)'], ['subtraction (skipped)']],
],
[
'with broken before hook (suite must be failed)',
[['addition (skipped)'], ['failing (skipped)'], ['subtraction (skipped)']],
],
[
'with broken beforeEach hook (suite must be failed)',
[['addition (skipped)'], ['failing (skipped)'], ['subtraction (skipped)']],
],
],
],
]);
});

it('runs tests', async () => {
const c = await getController();
const profiles = c.profiles;
expect(profiles).to.have.lengthOf(2);

const run = await captureTestRun(
c,
new vscode.TestRunRequest(
undefined,
undefined,
profiles.find((p) => p.kind === vscode.TestRunProfileKind.Run),
),
);

run.expectStates({
'hello.test.js/with beforeAll hook/addition': ['enqueued', 'started', 'passed'],
'hello.test.js/with beforeAll hook/subtraction': ['enqueued', 'started', 'passed'],
'hello.test.js/with beforeAll hook/failing': ['enqueued', 'started', 'failed'],
'hello.test.js/with beforeEach hook/addition': ['enqueued', 'started', 'passed'],
'hello.test.js/with beforeEach hook/subtraction': ['enqueued', 'started', 'passed'],
'hello.test.js/with beforeEach hook/failing': ['enqueued', 'started', 'failed'],
'hello.test.js/with broken before hook (suite must be failed)/addition (skipped)': [
'enqueued',
'skipped',
],
'hello.test.js/with broken before hook (suite must be failed)/subtraction (skipped)': [
'enqueued',
'skipped',
],
'hello.test.js/with broken before hook (suite must be failed)/failing (skipped)': [
'enqueued',
'skipped',
],
'hello.test.js/with broken beforeEach hook (suite must be failed)/addition (skipped)': [
'enqueued',
'started',
'skipped',
],
'hello.test.js/with broken beforeEach hook (suite must be failed)/subtraction (skipped)': [
'enqueued',
'skipped',
],
'hello.test.js/with broken beforeEach hook (suite must be failed)/failing (skipped)': [
'enqueued',
'skipped',
],
'hello.test.js/with broken after hook (suite must be failed)/addition': [
'enqueued',
'started',
'passed',
],
'hello.test.js/with broken after hook (suite must be failed)/subtraction': [
'enqueued',
'started',
'passed',
],
'hello.test.js/with broken after hook (suite must be failed)/failing': [
'enqueued',
'started',
'failed',
],
'hello.test.js/with broken afterEach hook (suite must be failed)/addition (success)': [
'enqueued',
'started',
'passed',
],
'hello.test.js/with broken afterEach hook (suite must be failed)/subtraction (skipped)': [
'enqueued',
'skipped',
],
'hello.test.js/with broken afterEach hook (suite must be failed)/failing (skipped)': [
'enqueued',
'skipped',
],
'hello.test.js/with broken before hook (suite must be failed)': ['failed'],
'hello.test.js/with broken beforeEach hook (suite must be failed)': ['failed'],
'hello.test.js/with broken after hook (suite must be failed)': ['failed'],
'hello.test.js/with broken afterEach hook (suite must be failed)': ['failed'],
});
});
});
3 changes: 3 additions & 0 deletions test-workspaces/with-hooks/.mocharc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
spec: '**/*.test.js'
};
103 changes: 103 additions & 0 deletions test-workspaces/with-hooks/hello.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const { strictEqual } = require('node:assert');

describe('with beforeAll hook', () => {
before(() => {
console.log('Before hook executed once!');
});

it('addition', async () => {
strictEqual(1 + 1, 2);
});

it('subtraction', async () => {
strictEqual(1 - 1, 0);
});
it('failing', async () => {
strictEqual(1 * 1, 0);
});
});

describe('with beforeEach hook', () => {
beforeEach(() => {
console.log('BeforeEach hook executed every time!');
});

it('addition', async () => {
strictEqual(1 + 1, 2);
});

it('subtraction', async () => {
strictEqual(1 - 1, 0);
});
it('failing', async () => {
strictEqual(1 * 1, 0);
});
});

describe('with broken before hook (suite must be failed)', () => {
before(() => {
throw new Error('Before hook is broken!!!');
});

it('addition (skipped)', async () => {
strictEqual(1 + 1, 2);
});

it('subtraction (skipped)', async () => {
strictEqual(1 - 1, 0);
});
it('failing (skipped)', async () => {
strictEqual(1 * 1, 0);
});
});

describe('with broken beforeEach hook (suite must be failed)', () => {
beforeEach(() => {
throw new Error('BeforeEach hook is broken!!!');
});

it('addition (skipped)', async () => {
strictEqual(1 + 1, 2);
});

it('subtraction (skipped)', async () => {
strictEqual(1 - 1, 0);
});
it('failing (skipped)', async () => {
strictEqual(1 * 1, 0);
});
});

describe('with broken after hook (suite must be failed)', () => {
after(() => {
throw new Error('After hook is broken!!!');
});

it('addition', async () => {
strictEqual(1 + 1, 2);
});

it('subtraction', async () => {
strictEqual(1 - 1, 0);
});
it('failing', async () => {
strictEqual(1 * 1, 0);
});
});

describe('with broken afterEach hook (suite must be failed)', () => {
afterEach(() => {
throw new Error('After each hook is broken!!!');
});

it('addition (success)', async () => {
strictEqual(1 + 1, 2);
});

it('subtraction (skipped)', async () => {
strictEqual(1 - 1, 0);
});
it('failing (skipped)', async () => {
strictEqual(1 * 1, 0);
});
});

0 comments on commit 873dd26

Please sign in to comment.