Disable logs globally using mocha hooks

This commit is contained in:
Felipe Lalanne 2022-08-25 11:55:21 -04:00
parent a5a24e6462
commit a4da25c1ef
5 changed files with 48 additions and 24 deletions

View File

@ -6,8 +6,9 @@ module.exports = {
// Files to execute before running suites
'ts-node/register/transpile-only',
'tsconfig-paths/register',
'test/lib/chai.ts',
'mocha-pod/skip-setup',
'test/lib/chai.ts',
'test/lib/mocha-hooks.ts',
],
spec: ['test/integration/**/*.spec.ts'],
timeout: '30000',

39
test/lib/mocha-hooks.ts Normal file
View File

@ -0,0 +1,39 @@
import * as sinon from 'sinon';
import log from '~/lib/supervisor-console';
/**
* Mocha runs these hooks before/after each test suite (beforeAll/afterAll)
* or before/after each test (beforeEach/afterEach), the same as with regular test hooks.
*
* Do here any setup that needs to affect all tests. When in doubt though, use regular hooks
* https://mochajs.org/#test-fixture-decision-tree-wizard-thing
*/
export const mochaHooks = {
beforeAll() {
// disable log output during testing
sinon.stub(log, 'debug');
sinon.stub(log, 'warn');
sinon.stub(log, 'info');
sinon.stub(log, 'success');
sinon.stub(log, 'event');
sinon.stub(log, 'error');
},
afterEach() {
(log.debug as sinon.SinonStub).reset();
(log.warn as sinon.SinonStub).reset();
(log.info as sinon.SinonStub).reset();
(log.success as sinon.SinonStub).reset();
(log.event as sinon.SinonStub).reset();
(log.error as sinon.SinonStub).reset();
},
afterAll() {
(log.debug as sinon.SinonStub).restore();
(log.warn as sinon.SinonStub).restore();
(log.info as sinon.SinonStub).restore();
(log.success as sinon.SinonStub).restore();
(log.event as sinon.SinonStub).restore();
(log.error as sinon.SinonStub).restore();
},
};

View File

@ -7,6 +7,7 @@ module.exports = {
'ts-node/register/transpile-only',
'tsconfig-paths/register',
'test/lib/chai.ts',
'test/lib/mocha-hooks.ts',
],
spec: ['test/unit/**/*.spec.ts'],
timeout: '30000',

View File

@ -90,7 +90,7 @@ describe('compose/network: unit tests', () => {
});
it('warns about IPAM configuration without both gateway and subnet', () => {
const logSpy = sinon.spy(log, 'warn');
const logStub = log.warn as sinon.SinonStub;
Network.fromComposeObject('default', 12345, 'deadbeef', {
ipam: {
@ -104,12 +104,12 @@ describe('compose/network: unit tests', () => {
},
});
expect(logSpy).to.have.been.calledOnce;
expect(logSpy).to.have.been.calledWithMatch(
expect(logStub).to.have.been.calledOnce;
expect(logStub).to.have.been.calledWithMatch(
'Network IPAM config entries must have both a subnet and gateway',
);
logSpy.resetHistory();
logStub.resetHistory();
Network.fromComposeObject('default', 12345, 'deadbeef', {
ipam: {
@ -123,12 +123,10 @@ describe('compose/network: unit tests', () => {
},
});
expect(logSpy).to.have.been.calledOnce;
expect(logSpy).to.have.been.calledWithMatch(
expect(logStub).to.have.been.calledOnce;
expect(logStub).to.have.been.calledWithMatch(
'Network IPAM config entries must have both a subnet and gateway',
);
logSpy.restore();
});
it('parses values from a compose object', () => {

View File

@ -1,5 +1,4 @@
import * as _ from 'lodash';
import * as sinon from 'sinon';
import { expect } from 'chai';
import { createContainer } from '~/test-lib/mockerode';
@ -9,8 +8,6 @@ import Volume from '~/src/compose/volume';
import * as ServiceT from '~/src/compose/types/service';
import * as constants from '~/lib/constants';
import log from '~/lib/supervisor-console';
const configs = {
simple: {
compose: require('~/test-data/docker-states/simple/compose.json'),
@ -30,18 +27,6 @@ const configs = {
};
describe('compose/service: unit tests', () => {
before(() => {
// disable log output during testing
sinon.stub(log, 'debug');
sinon.stub(log, 'warn');
sinon.stub(log, 'info');
sinon.stub(log, 'success');
});
after(() => {
sinon.restore();
});
describe('Creating a service instance from a compose object', () => {
it('extends environment variables with additional OS info', async () => {
const extendEnvVarsOpts = {