diff --git a/package-lock.json b/package-lock.json index 3b2093660..136ac6acf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,7 +26,6 @@ "chai": "^4.3.7", "is-circular": "^1.0.2", "mocha": "^10.2.0", - "mockery": "^2.1.0", "semantic-release": "^21.0.1", "sinon": "^15.0.4", "sinon-chai": "^3.7.0" @@ -4956,12 +4955,6 @@ "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", "dev": true }, - "node_modules/mockery": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/mockery/-/mockery-2.1.0.tgz", - "integrity": "sha512-9VkOmxKlWXoDO/h1jDZaS4lH33aWfRiJiNT/tKj+8OGzrcFDLo8d0syGdbsc3Bc4GvRXPb+NMMvojotmuGJTvA==", - "dev": true - }, "node_modules/modify-values": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/modify-values/-/modify-values-1.0.1.tgz", @@ -14808,12 +14801,6 @@ } } }, - "mockery": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/mockery/-/mockery-2.1.0.tgz", - "integrity": "sha512-9VkOmxKlWXoDO/h1jDZaS4lH33aWfRiJiNT/tKj+8OGzrcFDLo8d0syGdbsc3Bc4GvRXPb+NMMvojotmuGJTvA==", - "dev": true - }, "modify-values": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/modify-values/-/modify-values-1.0.1.tgz", diff --git a/package.json b/package.json index dd3829713..425c09525 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,6 @@ "chai": "^4.3.7", "is-circular": "^1.0.2", "mocha": "^10.2.0", - "mockery": "^2.1.0", "semantic-release": "^21.0.1", "sinon": "^15.0.4", "sinon-chai": "^3.7.0" diff --git a/test/es2015_test.js b/test/es2015_test.js index 5b281a9fe..2aca712c1 100644 --- a/test/es2015_test.js +++ b/test/es2015_test.js @@ -7,7 +7,7 @@ const chai = require('chai') const sinon = require('sinon') chai.use(require('sinon-chai')) -const mockery = require('mockery') +const { hook, reset } = require('./fixtures/RequireMocker.js') const expect = chai.expect @@ -53,19 +53,14 @@ describe('hubot/es2015', function () { }) it('exports Robot class', async function () { - mockery.enable({ - warnOnReplace: false, - warnOnUnregistered: false - }) - mockery.registerMock('hubot-mock-adapter', require('./fixtures/mock-adapter.js')) + hook('hubot-mock-adapter', require('./fixtures/mock-adapter.js')) class MyRobot extends Robot {} const robot = new MyRobot('hubot-mock-adapter', false, 'TestHubot') await robot.loadAdapter() expect(robot).to.be.an.instanceof(Robot) expect(robot.name).to.equal('TestHubot') - - mockery.disable() + reset() }) it('exports Adapter class', function () { diff --git a/test/fixtures/RequireMocker.js b/test/fixtures/RequireMocker.js new file mode 100644 index 000000000..291d1d3b9 --- /dev/null +++ b/test/fixtures/RequireMocker.js @@ -0,0 +1,19 @@ +const Module = require('module') +const originalRequire = Module.prototype.require +const hookModuleToReturnMockFromRequire = (module, mock) => { + Module.prototype.require = function () { + if (arguments[0] === module) { + return mock + } + return originalRequire.apply(this, arguments) + } +} + +const resetModuleMocks = () => { + Module.prototype.require = originalRequire +} + +module.exports = { + hook: hookModuleToReturnMockFromRequire, + reset: resetModuleMocks +} diff --git a/test/middleware_test.js b/test/middleware_test.js index eab7929cb..d3951fa08 100644 --- a/test/middleware_test.js +++ b/test/middleware_test.js @@ -16,8 +16,7 @@ const TextMessage = require('../src/message').TextMessage const Response = require('../src/response') const Middleware = require('../src/middleware') -// mock `hubot-mock-adapter` module from fixture -const mockery = require('mockery') +const { hook, reset } = require('./fixtures/RequireMocker.js') describe('Middleware', function () { describe('Unit Tests', function () { @@ -346,11 +345,7 @@ describe('Middleware', function () { // tested for. describe('Public Middleware APIs', function () { beforeEach(async function () { - mockery.enable({ - warnOnReplace: false, - warnOnUnregistered: false - }) - mockery.registerMock('hubot-mock-adapter', require('./fixtures/mock-adapter.js')) + hook('hubot-mock-adapter', require('./fixtures/mock-adapter.js')) process.env.EXPRESS_PORT = 0 this.robot = new Robot('hubot-mock-adapter', true, 'TestHubot') await this.robot.loadAdapter() @@ -379,7 +374,7 @@ describe('Middleware', function () { }) afterEach(function () { - mockery.disable() + reset() this.robot.shutdown() }) diff --git a/test/robot_test.js b/test/robot_test.js index 606708f18..e7d6f27fb 100644 --- a/test/robot_test.js +++ b/test/robot_test.js @@ -18,17 +18,12 @@ const LeaveMessage = require('../src/message').LeaveMessage const TextMessage = require('../src/message').TextMessage const TopicMessage = require('../src/message').TopicMessage -// mock `hubot-mock-adapter` module from fixture -const mockery = require('mockery') const path = require('path') +const { hook, reset } = require('./fixtures/RequireMocker.js') describe('Robot', function () { beforeEach(async function () { - mockery.enable({ - warnOnReplace: false, - warnOnUnregistered: false - }) - mockery.registerMock('hubot-mock-adapter', require('./fixtures/mock-adapter.js')) + hook('hubot-mock-adapter', require('./fixtures/mock-adapter.js')) process.env.EXPRESS_PORT = 0 this.robot = new Robot('hubot-mock-adapter', true, 'TestHubot') this.robot.alias = 'Hubot' @@ -51,7 +46,7 @@ describe('Robot', function () { }) afterEach(function () { - mockery.disable() + reset() this.robot.shutdown() })