Mocking

At some point or another, you might have to mock functions or variables. You can do both with Ponicode and here is how.

How to?

Start by opening the Ponicode interface for the function you want to test. To do so, right click on it and select "Ponicode: Unit Test".

open-ponicode-interface


opened-ponicode-interface


Now you want to right click the function you wish to mock and select "Ponicode: Mock"

open-ponicode-mocking


You can now mock the value of the function directly in the Ponicode interface.

opened-ponicode-mocking


State of support (FUNCTIONS)

We recommend to always export functions in your project that use mocks or will be mocked, in order to avoid special edge cases proper to Javascript.

All Ponicode-specific limitations can be lifted. Tell us what you want at ping@ponicode.com 😊.

1. User-defined functions

Ponicode can mock functions that are defined in the same projects.

Those functions must be exported.

Exclusions
  • We do not support mocking non-exported functions
  • We do not support mocking conditionally exported functions
  • We do not support mocking direct exports (for example: module.exports = function ())
Hazards
  • Because of how mock frameworks are implemented, the reference to the mocked function must not have been copied.
  • The mocked function and the tested function must live in the same context
Examples

✅ Works

const db = require('./db')

function foo () {
    db.query()
}

module.exports = { foo }

Fails

const { query } = require('./db')

function foo () {
    query()
}

module.exports = { foo }

The reference to db.query was copied before the mock was set up.

Fails

const query = require('./db').query

function foo () {
    query()
}

module.exports = { foo }

The reference to db.query was copied before the mock was set up.

Fails with Jest

const { query } = require('./db')

function foo () {
    query()
}

foo and db will not live in the same context, because foo is not exported and Jest uses another Javascript engine.

Work around: Export foo

What will happen?

  1. Jest is launched
  2. foo is imported via rewire, and will live in Node context
  3. db is imported by foo, and will live in Node context.
  4. From the test file, db is imported via Jest’s require, and will live in Jest’s context.
  5. Jest will mock db in Jest’s context
  6. foo will call db.query in Node’s context, the mock will not work

This is a Ponicode-specific limitation.

2. Package functions

Ponicode can mock functions that are defined in node modules.

Ponicode can also mock functions from node built-in modules (fs, util, etc)

Exclusions
  • The tested function must be exported (this is a Ponicode-specific limitation).
  • We do not support mocking direct exports.
Examples

✅ Works

const axios = require('axios')

    function foo () {
        axios.get()
    }
    
    module.exports = { foo }

❌ Fails

const axios = require('axios')

    function foo () {
        axios.get()
    }

foo is not exported. Ponicode-specific limitation.

❌ Fails

const { get } = require('axios')

    function foo () {
        get()
    }
    
    module.exports = { foo }

The reference to axios.get was copied before the mock was set up.

❌ Fails

const axios = require('axios')    function foo () {        axios()    }        module.exports = { foo }

We cannot mock axios itself. This is a Ponicode-specific limitation.

❌ Fails

const pkg = require('package')

    function foo () {
        pkg[0]()
    }
    
    module.exports = { foo }

We cannot mock pkg[0]. Only simply exported members are supported. This is a Ponicode-specific limitation.

❌ Fails

const pkg = require('package')

    function foo () {
        pkg[0]()
    }
    
    module.exports = { foo }

We cannot mock axios.get. Ponicode’s inferer only resolves conventional imports. This is a Ponicode-specific limitation.

2. Package functions

Ponicode can mock native functions that do not need to be imported. Here are some examples.

Javascript

Example: Math.random

Node

Example: Buffer.from

Browser

Example: fetch

State of support (VARIABLES)

Ponicode can mock global variables that are defined in the same file as the tested function.

Criteria
  • The variable must be defined in global scope.
  • If you want to stub the variable, the variable must not be a constant.

Ready to write beautiful code?

Smart and simple docstring generation. Now available in your favorite code hosting platform.

Try it now