-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock.js
27 lines (24 loc) · 891 Bytes
/
mock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
'use strict'
var enabled = false
//tested several mock frameworks (mock-fs, proxyquire, rewiremock, rewire)
//did not get it work in this case (mock sosme "fs" func. with runtime required modules)
//simple implementation (below) did work in 30 min. versus testing/research/troubleshooting the above taking ~ 12 hours!
const mockRequire = _require => mdl => {
//inject behaviour to fs.accessSync function ...
const fs = require('fs'), {accessSync} = fs
fs.accessSync = (...args) => {
if(enabled){
var [filename] = args
if(filename.match(/scope-a\/component-three\/htdocs/)){
enabled = false
throw new Error('simulated filesystem read permission exception')
}
}
return accessSync(...args)
}
return _require(mdl)
}
module.exports = _require => ({
mockRequire: mockRequire(_require),
mockApply: func => (enabled = true) && func()
})