jspm-mock
v2.0.0
Published
The "jspm-mock" module swaps any jspm module with a fake alternative.
Downloads
3
Maintainers
Readme
jspm-mock
The "jspm-mock" module swaps any jspm module with a fake alternative. Essential for mocking dependencies in unit tests.
Installation
jspm install npm:jspm-mock --dev
- Note: This package assumes that you already have a working copy of jspm installed.
Usage
Import inside jspm environment (i.e. specs):
import jspmMock from 'jspm-mock'
Mock using functions:
jspmMock.mock('fs', function () {
console.log("Testing FAKE function!")
})
// same as...
// jspmMock.mock('fs', function () {
// default: function () {
// console.log("Testing FAKE function!")
// }
// })
jspmMock.get('fs').then(module => {
console.log('module()', module())
})
.catch(console.error)
Mock using objects:
jspmMock.mock('fs', {
actionOne: function () {
console.log("Testing FAKE function object one!")
},
actionTwo: function () {
console.log("Testing FAKE function object two!")
}
})
jspmMock.get('fs').then(module => {
console.log('module.actionOne()', module.actionOne())
console.log('module.actionTwo()', module.actionTwo())
})
.catch(console.error)
Mock using raw sources:
jspmMock.mock(
'fs',
`export default function () {
console.log('Hello from fake module!')
}`
)
jspmMock.get('fs').then(module => {
console.log('module()', module())
})
.catch(console.error)
Unmock when the time is right:
jspmMock.unmock('fs')