mock-vir
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

mock-vir

The heroic mock for anything.

Usage

npm i -D mock-vir

Creating a mock

Create a simple mock with createMockVir. Make sure to pass in the type generic for what you are mocking. After creating the mock, you can access all properties on the mock as if it were the originally given type.

import {createMockVir} from 'mock-vir';

type ThingToMock = {
    nestedObject: {
        exampleChild: string;
    };
};

const myMock = createMockVir<ThingToMock>();

console.log(myMock.nestedObject.exampleChild);

Nested mocks

Nested objects will always be accessible from the mock, even without defining them all before run-time. (This is possible by using the JS built-in Proxy.)

import {createMockVir} from 'mock-vir';

type ThingToMock = {
    exampleChild: string;
};

const myMock = createMockVir<ThingToMock>();

console.log(myMock.exampleChild);

Setting a mock's value

To set a mock's value (so it can be accessed in tests), just set the value directly on the mock.

import {createMockVir} from 'mock-vir';

type ThingToMock = {
    nestedObject: {
        exampleChild: string;
    };
};

const myMock = createMockVir<ThingToMock>();

myMock.nestedObject.exampleChild = 'whatever';

// this will log "whatever"
console.log(myMock.nestedObject.exampleChild);

Setting a function's return value

This is where things get interesting! You can set the return value for a mock's nested function by using the keyForSettingMockReturnValue symbol.

Note: when accessing this symbol, you must as cast your mock to use WithMockVir so that TypeScript knows this is indeed the mock wrapper of the base type, not the base type itself.

import {createMockVir, keyForSettingMockReturnValue, WithMockVir} from 'mock-vir';

type ThingToMock = {
    nestedObject: {
        exampleChild: () => string;
    };
};

const myMock = createMockVir<ThingToMock>() as WithMockVir<ThingToMock>;

myMock.nestedObject.exampleChild[keyForSettingMockReturnValue] = 'whatever';

// this will log "whatever"
console.log(myMock.nestedObject.exampleChild());

Accessing arguments from a function call after the fact

After running a test, you can access the arguments that a function in the mock was called with.

Note: when accessing this symbol, you must as cast your mock to use WithMockVir so that TypeScript knows this is indeed the mock wrapper of the base type, not the base type itself.

import {
    createMockVir,
    keyForReadingLastCalledArgs,
    keyForSettingMockReturnValue,
    WithMockVir,
} from 'mock-vir';

type ThingToMock = {
    nestedObject: {
        exampleChild: (dummyInput: number) => string;
    };
};

const myMock = createMockVir<ThingToMock>() as WithMockVir<ThingToMock>;

myMock.nestedObject.exampleChild[keyForSettingMockReturnValue] = 'whatever';

// call the function
// this will happen in the source code that you are testing
myMock.nestedObject.exampleChild(42);

// this will log 42
console.log(myMock.nestedObject.exampleChild[keyForReadingLastCalledArgs]);

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.0
    88
    • latest

Version History

Package Sidebar

Install

npm i mock-vir

Weekly Downloads

113

Version

1.0.0

License

(MIT or CC0 1.0)

Unpacked Size

23.1 kB

Total Files

16

Last publish

Collaborators

  • electrovir