verb: appear or claim to be or do something, especially falsely; profess.
Overrides standard requires() - just require & fire Purport before your other dependencies
const Purport = require("purport")()
const yourModule = require("yourModule")
const localImport = require("./example")
//Access your module like normal
yourModule.yourFunc()
//Your module is referenced via Purport as the string passed into require()
Purport.yourModule.stub("yourFunc", console.log)
Purport['./example'].record(console.log)
methodName: String value with the exact name of your function (ex: function test()=>{} would be 'test')
Call to override your required libraries method with a stub function
Purport.yourModule.stub('testFunction', () => true) //replaces yourModule.testFunction
console.log(yourModule.testFunction()) //true
//also works with async/promises
Purport.yourModule.stub('testAsync', async () => { ... })
Purport.yourModule.stub('testPromise', new Promise( ... ))
methodName: String value with the exact name of your function (ex: function test()=>{} would be 'test')
-Purport.yourModule.args.yourFunc - An array of argument arrays in the same order as the function was called
Purport.yourModule.record('someFunc')
Purport.yourModule.callCount.someFunc // 0
Purport.yourModule.wasCalled.someFunc() // false
yourModule.someFunc()
Purport.yourModule.callCount.someFunc // 1
Purport.yourModule.wasCalled.someFunc() // true
TODO: async/promises? not tested/described ;(
methodName: String value with the exact name of your function (ex: function test()=>{} would be 'test')
cb: The actual method to execute; can use async/await, promises, or the original method (ex: () => { return true })
hooks: Optional events to fire directly before and after the function executes formatted like this: { preHook: ()=>{}, postHook: ()=>{} }
-Purport.yourModule.args.yourFunc - An array of argument arrays in the same order as the function was called
const myTestFunc = () => { console.log("hello") }
const myOtherTestFunc = () => { console.log("goodbye") }
Purport.yourModule.mock('thatFunction', (arg1) => { console.log("how are you?") }, { preHook: myTestFunc, postHook: myOtherTestFunc })
Purport.yourModule.callCount.thatFunction // 0
Purport.yourModule.wasCalled.thatFunction() // false
yourModule.thatFunction() // hello, how are you?, goodbye
Purport.yourModule.callCount.thatFunction // 1
Purport.yourModule.wasCalled.thatFunction() // true
yourModule.falseFunction() //returns false
Purport.yourModule.stub('falseFunction', () => true)
yourModule.falseFunction() //returns true
Purport.yourModule.reset()
yourModule.falseFunction() //returns false