Skip to content

Commit

Permalink
tests: add tests for failed mocha hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammadyusuf-kurbonov committed Oct 13, 2024
1 parent 1471a17 commit baf3519
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
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 baf3519

Please sign in to comment.