m-a-r-c-e-l-i-n-o / jspm-mock

The "jspm-mock" module swaps any type of jspm module with an alternative. Essential for mocking dependencies in unit tests.
Other
0 stars 1 forks source link

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'

Get modules

jspmMock.get('fs')

* Note: All modules except jspm-mock must be imported via jspmMock.get. Modules do NOT have to be imported before calling jspmMock.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')