Skip to content

Commit

Permalink
fix: remove mockery usage in test (#616)
Browse files Browse the repository at this point in the history
  • Loading branch information
klu909 authored Feb 22, 2024
1 parent f6d181c commit 4129384
Show file tree
Hide file tree
Showing 38 changed files with 221 additions and 692 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
"mocha": "^10.1.0",
"mocha-multi-reporters": "^1.5.1",
"mocha-sonarqube-reporter": "^1.0.2",
"mockery": "^2.1.0",
"nyc": "^15.1.0",
"path": "^0.12.7",
"rewire": "^6.0.0",
"rewiremock": "^3.14.4",
"sinon": "^15.0.0"
},
"dependencies": {
Expand Down
10 changes: 0 additions & 10 deletions test/lib/banner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const { assert } = require('chai');
const sinon = require('sinon');
const mockery = require('mockery');
const schema = require('screwdriver-data-schema');

sinon.assert.expose(assert, { prefix: '' });
Expand All @@ -15,10 +14,6 @@ describe('Banner Model', () => {
let banner;

before(() => {
mockery.enable({
useCleanCache: true,
warnOnUnregistered: false
});
datastore = {
update: sinon.stub(),
remove: sinon.stub().resolves(null)
Expand All @@ -41,11 +36,6 @@ describe('Banner Model', () => {
banner = new BannerModel(createConfig);
});

after(() => {
mockery.deregisterAll();
mockery.disable();
});

it('is constructed properly', () => {
assert.instanceOf(banner, BannerModel);
assert.instanceOf(banner, BaseModel);
Expand Down
25 changes: 4 additions & 21 deletions test/lib/bannerFactory.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const { assert } = require('chai');
const mockery = require('mockery');
const sinon = require('sinon');

sinon.assert.expose(assert, { prefix: '' });
Expand All @@ -23,13 +22,6 @@ describe('Banner Factory', () => {
let factory;
let Banner;

before(() => {
mockery.enable({
useCleanCache: true,
warnOnUnregistered: false
});
});

beforeEach(() => {
datastore = {
save: sinon.stub(),
Expand All @@ -44,15 +36,6 @@ describe('Banner Factory', () => {
factory = new BannerFactory({ datastore });
});

afterEach(() => {
mockery.resetCache();
});

after(() => {
mockery.deregisterAll();
mockery.disable();
});

describe('createClass', () => {
it('should return a Collection', () => {
const model = factory.createClass(bannerData);
Expand Down Expand Up @@ -169,6 +152,10 @@ describe('Banner Factory', () => {
/* eslint-enable global-require */
});

it('should throw an error when config not supplied', () => {
assert.throws(() => BannerFactory.getInstance(), 'No datastore provided to BannerFactory');
});

it('should get an instance', () => {
const f1 = BannerFactory.getInstance(config);
const f2 = BannerFactory.getInstance(config);
Expand All @@ -178,9 +165,5 @@ describe('Banner Factory', () => {

assert.equal(f1, f2);
});

it('should throw an error when config not supplied', () => {
assert.throw(BannerFactory.getInstance, Error, 'No datastore provided to BannerFactory');
});
});
});
21 changes: 4 additions & 17 deletions test/lib/base.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { assert } = require('chai');
const mockery = require('mockery');
const rewiremock = require('rewiremock/node');
const sinon = require('sinon');

sinon.assert.expose(assert, { prefix: '' });
Expand All @@ -15,13 +15,6 @@ describe('Base Model', () => {
let scm;
let multiBuildClusterEnabled;

before(() => {
mockery.enable({
useCleanCache: true,
warnOnUnregistered: false
});
});

beforeEach(() => {
scm = {
foo: 'foo'
Expand All @@ -42,10 +35,10 @@ describe('Base Model', () => {
}
}
};
mockery.registerMock('screwdriver-data-schema', schemaMock);

// eslint-disable-next-line global-require
BaseModel = require('../../lib/base');
BaseModel = rewiremock.proxy('../../lib/base', {
'screwdriver-data-schema': schemaMock
});

config = {
datastore,
Expand All @@ -61,12 +54,6 @@ describe('Base Model', () => {

afterEach(() => {
datastore = null;
mockery.deregisterAll();
mockery.resetCache();
});

after(() => {
mockery.disable();
});

describe('constructor', () => {
Expand Down
22 changes: 4 additions & 18 deletions test/lib/baseFactory.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { assert } = require('chai');
const mockery = require('mockery');
const rewiremock = require('rewiremock/node');
const sinon = require('sinon');

/* eslint max-classes-per-file: ["error", 2] */
Expand All @@ -24,13 +24,6 @@ describe('Base Factory', () => {
let factory;
let schema;

before(() => {
mockery.enable({
useCleanCache: true,
warnOnUnregistered: false
});
});

beforeEach(() => {
scm = {};
datastore = {
Expand All @@ -49,22 +42,15 @@ describe('Base Factory', () => {
}
};

mockery.registerMock('screwdriver-data-schema', schema);

// eslint-disable-next-line global-require
BaseFactory = require('../../lib/baseFactory');
BaseFactory = rewiremock.proxy('../../lib/baseFactory', {
'screwdriver-data-schema': schema
});

factory = new BaseFactory('base', { datastore, scm });
});

afterEach(() => {
datastore = null;
mockery.deregisterAll();
mockery.resetCache();
});

after(() => {
mockery.disable();
});

describe('createClass', () => {
Expand Down
45 changes: 17 additions & 28 deletions test/lib/build.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

const { assert } = require('chai');
const mockery = require('mockery');
const sinon = require('sinon');
const schema = require('screwdriver-data-schema');
const { SCM_STATE_MAP } = require('screwdriver-data-schema').plugins.scm;
const rewiremock = require('rewiremock/node');

sinon.assert.expose(assert, { prefix: '' });

Expand Down Expand Up @@ -52,7 +52,6 @@ describe('Build Model', () => {
let BuildModel;
let datastore;
let executorMock;
let hashaMock;
let build;
let config;
let BaseModel;
Expand All @@ -69,13 +68,6 @@ describe('Build Model', () => {
let jobMock;
let templateMock;

before(() => {
mockery.enable({
useCleanCache: true,
warnOnUnregistered: false
});
});

beforeEach(() => {
datastore = {
get: sinon.stub(),
Expand All @@ -84,9 +76,6 @@ describe('Build Model', () => {
update: sinon.stub(),
remove: sinon.stub()
};
hashaMock = {
sha1: sinon.stub()
};
executorMock = {
start: sinon.stub(),
stop: sinon.stub(),
Expand Down Expand Up @@ -167,19 +156,17 @@ describe('Build Model', () => {
getInstance: sinon.stub().returns(templateFactoryMock)
};

mockery.registerMock('./pipelineFactory', pF);
mockery.registerMock('./userFactory', uF);
mockery.registerMock('./jobFactory', jF);
mockery.registerMock('./stepFactory', sF);
mockery.registerMock('./stageFactory', stageF);
mockery.registerMock('./stageBuildFactory', stageBuildF);
mockery.registerMock('./templateFactory', tF);
mockery.registerMock('screwdriver-hashr', hashaMock);
rewiremock('../../lib/pipelineFactory').with(pF);
rewiremock('../../lib/userFactory').with(uF);
rewiremock('../../lib/jobFactory').with(jF);
rewiremock('../../lib/stepFactory').with(sF);
rewiremock('../../lib/stageFactory').with(stageF);
rewiremock('../../lib/stageBuildFactory').with(stageBuildF);
rewiremock('../../lib/templateFactory').with(tF);
rewiremock.enable();

// eslint-disable-next-line global-require
BuildModel = require('../../lib/build');
// eslint-disable-next-line global-require
BaseModel = require('../../lib/base');

config = {
datastore,
Expand All @@ -204,15 +191,17 @@ describe('Build Model', () => {

afterEach(() => {
datastore = null;
mockery.deregisterAll();
mockery.resetCache();
});

after(() => {
mockery.disable();
rewiremock.disable();
});

it('extends base class', () => {
rewiremock.disable();
// eslint-disable-next-line global-require
BuildModel = require('../../lib/build');
build = new BuildModel(config);
// eslint-disable-next-line global-require
BaseModel = require('../../lib/base');

assert.instanceOf(build, BaseModel);
assert.isFunction(build.start);
assert.isFunction(build.stop);
Expand Down
10 changes: 0 additions & 10 deletions test/lib/buildCluster.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const { assert } = require('chai');
const sinon = require('sinon');
const mockery = require('mockery');
const schema = require('screwdriver-data-schema');

sinon.assert.expose(assert, { prefix: '' });
Expand All @@ -15,10 +14,6 @@ describe('BuildCluster Model', () => {
let buildCluster;

before(() => {
mockery.enable({
useCleanCache: true,
warnOnUnregistered: false
});
datastore = {
update: sinon.stub(),
remove: sinon.stub().resolves(null)
Expand All @@ -44,11 +39,6 @@ describe('BuildCluster Model', () => {
buildCluster = new BuildClusterModel(createConfig);
});

after(() => {
mockery.deregisterAll();
mockery.disable();
});

it('is constructed properly', () => {
assert.instanceOf(buildCluster, BuildClusterModel);
assert.instanceOf(buildCluster, BaseModel);
Expand Down
25 changes: 4 additions & 21 deletions test/lib/buildClusterFactory.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const { assert } = require('chai');
const mockery = require('mockery');
const sinon = require('sinon');

sinon.assert.expose(assert, { prefix: '' });
Expand Down Expand Up @@ -31,13 +30,6 @@ describe('BuildCluster Factory', () => {
let factory;
let BuildCluster;

before(() => {
mockery.enable({
useCleanCache: true,
warnOnUnregistered: false
});
});

beforeEach(() => {
datastore = {
save: sinon.stub(),
Expand All @@ -52,15 +44,6 @@ describe('BuildCluster Factory', () => {
factory = new BuildClusterFactory({ datastore });
});

afterEach(() => {
mockery.resetCache();
});

after(() => {
mockery.deregisterAll();
mockery.disable();
});

describe('createClass', () => {
it('should return a Collection', () => {
const model = factory.createClass(buildClusterData);
Expand Down Expand Up @@ -145,6 +128,10 @@ describe('BuildCluster Factory', () => {
/* eslint-enable global-require */
});

it('should throw an error when config not supplied', () => {
assert.throw(BuildClusterFactory.getInstance, Error, 'No datastore provided to BuildClusterFactory');
});

it('should get an instance', () => {
const f1 = BuildClusterFactory.getInstance(config);
const f2 = BuildClusterFactory.getInstance(config);
Expand All @@ -154,9 +141,5 @@ describe('BuildCluster Factory', () => {

assert.equal(f1, f2);
});

it('should throw an error when config not supplied', () => {
assert.throw(BuildClusterFactory.getInstance, Error, 'No datastore provided to BuildClusterFactory');
});
});
});
Loading

0 comments on commit 4129384

Please sign in to comment.