diff --git a/package-lock.json b/package-lock.json index c80ad2b..157bb43 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,6 @@ "chai": "^4.3.7", "matchdep": "^2.0.0", "mocha": "^10.2.0", - "mockery": "^2.1.0", "nyc": "^15.1.0", "semantic-release": "^21.0.2", "sinon": "^15.0.4", @@ -6990,12 +6989,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", diff --git a/package.json b/package.json index fef24c2..977ae0d 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,6 @@ "chai": "^4.3.7", "matchdep": "^2.0.0", "mocha": "^10.2.0", - "mockery": "^2.1.0", "nyc": "^15.1.0", "semantic-release": "^21.0.2", "sinon": "^15.0.4", diff --git a/test/fixtures/RequireMocker.js b/test/fixtures/RequireMocker.js new file mode 100644 index 0000000..291d1d3 --- /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/help_message_visibility_test.js b/test/help_message_visibility_test.js index da389d9..3407218 100644 --- a/test/help_message_visibility_test.js +++ b/test/help_message_visibility_test.js @@ -6,7 +6,7 @@ const path = require('path') const chai = require('chai') const expect = chai.expect -const mockery = require('mockery') +const { hook, reset } = require('./fixtures/RequireMocker.js') chai.use(require('sinon-chai')) @@ -31,11 +31,7 @@ const newTestRobot = function newTestRobot () { describe('help in private', () => describe('message visibility', () => { beforeEach(function () { - mockery.enable({ - warnOnReplace: false, - warnOnUnregistered: false - }) - mockery.registerMock('hubot-mock-adapter', require('./fixtures/MockAdapter.js')) + hook('hubot-mock-adapter', require('./fixtures/MockAdapter.js')) this.robot = newTestRobot() this.robot.run() this.user = this.robot.brain.userForName('john') @@ -43,7 +39,7 @@ describe('help in private', () => describe('message visibility', () => { afterEach(function () { this.robot.shutdown() - mockery.disable() + reset() }) context('when HUBOT_HELP_REPLY_IN_PRIVATE is unset', () => it('replies in the same room', function (done) { diff --git a/test/help_test.js b/test/help_test.js index bf62acf..79d2ff4 100644 --- a/test/help_test.js +++ b/test/help_test.js @@ -6,7 +6,7 @@ const path = require('path') const chai = require('chai') const expect = chai.expect -const mockery = require('mockery') +const { hook, reset } = require('./fixtures/RequireMocker.js') chai.use(require('sinon-chai')) @@ -30,11 +30,7 @@ const newTestRobot = function newTestRobot () { describe('help', () => describe('getHelpCommands', () => { beforeEach(function () { - mockery.enable({ - warnOnReplace: false, - warnOnUnregistered: false - }) - mockery.registerMock('hubot-mock-adapter', require('./fixtures/MockAdapter.js')) + hook('hubot-mock-adapter', require('./fixtures/MockAdapter.js')) this.robot = newTestRobot() this.robot.run() this.user = this.robot.brain.userForName('john') @@ -42,7 +38,7 @@ describe('help', () => describe('getHelpCommands', () => { afterEach(function () { this.robot.shutdown() - mockery.disable() + reset() }) context('when HUBOT_HELP_HIDDEN_COMMANDS is not set', () => it('lists all commands', function (done) {