1.0.0 • Published 9 months ago

mock-vir v1.0.0

Weekly downloads
-
License
(MIT or CC0 1.0)
Repository
github
Last release
9 months ago

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]);
1.0.0

9 months ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.3

12 months ago

0.1.0

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago

0.0.0

2 years ago