Skip to content

Commit 8060f23

Browse files
committed
Move to using proxyquire instead of manual mocks
1 parent 17f0d64 commit 8060f23

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
},
3737
"devDependencies": {
3838
"pre-commit": "git://github.com/observing/pre-commit.git",
39+
"proxyquire": "^2.1.3",
3940
"standard": "^16.0.3",
4041
"tap": "^15.0.9"
4142
}

test.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const Hook = require('./')
44
const tty = require('tty')
55
const ttySupportColor = tty.isatty(process.stdout.fd)
66

7+
const proxyquire = require('proxyquire')
8+
79
t.test('pre-commit', function (t) {
810
t.plan(5)
911

@@ -144,25 +146,22 @@ t.test('pre-commit', function (t) {
144146
t.test('overrides the `pre-commit` config property in package.json with the config inside pre-commit.json if it exists', function (t) {
145147
t.plan(1)
146148

147-
const fs = require('fs')
148-
const { existsSync, readFileSync } = fs
149-
150-
fs.existsSync = function existsSyncMock () {
151-
return true
152-
}
153-
154-
fs.readFileSync = function readFileSyncMock () {
155-
return Buffer.from(JSON.stringify({ run: ['lint', 'bench'] }))
156-
}
149+
const Hook = proxyquire('.', {
150+
fs: {
151+
existsSync () {
152+
return true
153+
},
154+
readFileSync () {
155+
const rawText = JSON.stringify({ run: ['lint', 'bench'] })
156+
return Buffer.from(rawText)
157+
}
158+
}
159+
})
157160

158161
hook = new Hook(function () {}, { ignorestatus: true })
159162

160163
// ----
161164
t.same(hook.config.run, ['lint', 'bench'])
162-
163-
// Restoring mocks
164-
fs.existsSync = existsSync
165-
fs.readFileSync = readFileSync
166165
})
167166
})
168167

0 commit comments

Comments
 (0)