Skip to content

Commit

Permalink
chore: Replace mockery because it's stale and has a security vulnerab…
Browse files Browse the repository at this point in the history
…ility (#1663)
  • Loading branch information
joeyguerra authored Aug 14, 2023
1 parent 91cbe76 commit 9865cb2
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 38 deletions.
13 changes: 0 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 3 additions & 8 deletions test/es2015_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 () {
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/RequireMocker.js
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 3 additions & 8 deletions test/middleware_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -379,7 +374,7 @@ describe('Middleware', function () {
})

afterEach(function () {
mockery.disable()
reset()
this.robot.shutdown()
})

Expand Down
11 changes: 3 additions & 8 deletions test/robot_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -51,7 +46,7 @@ describe('Robot', function () {
})

afterEach(function () {
mockery.disable()
reset()
this.robot.shutdown()
})

Expand Down

0 comments on commit 9865cb2

Please sign in to comment.