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

1.0.15 • Public • Published

npm version

Decomock

Create mocks faster with rules

Installation

npm i decomock --save

enable experimentalDecorators in your tsconfig.json file.

Usage

create a typescript class for your mock instances

export class UserMock {
    @MockRule(IdRule())
    public id?: number;
    @MockRule(SomeFromArrayRule(['Blue', 'Red', 'Green', 'Yellow']))
    public favoritesColors: string[];
    @MockRule(StaticRule('Awesome'))
    public opinionAboutDecomock?: string;
}

Instances of this class will have randomly generated values based on the assigned mock rule.

const mock = new UserMock();
console.log(mock.id) // 235123
console.log(mock.favoritesColors) // ['Red', 'Yellow']
console.log(mock.opinionAboutDecomock) // 'Awesome'

Create your own rules. As long it respect the MockRule interface.

interface IMockRule<T> {
    generate(): T;
}

export function CustomRule<T>(value?: any): IMockRule<T> {
    return {
        generate(): T {
            return // your awesome rule logic here...
        }
    }
}

Or simply on the fly like this

class Human {
    @MockRule({
        generate: () => {
            return Math.floor(Math.random() * 100)
        }
    })
    public age?: number;
}

The values are generated once for each instance. The instance will then behave like a normal object and new assigned value will persist.

!!Two special rules use the built in IdsStore to enable id relationships. The IdRule and the FkRule.

export class UserMock {
    @MockRule(IdRule('user'))
    public id?: number;
}

export class WalletMock {
    @MockRule(FkRule('user'))
    public userId?: number;
}

const user1 = new UserMock();
const user2 = new UserMock()
console.log(user1.id) // 235123
console.log(user2.id) // 992398

const walletMock = new Wallet();
console.log(walletMock.userId) // 235123 or 992398

Contribution

If you want to contribute, feel free to do so! Any code optimization or any rules that you feel that should be in the package would be nice additions to the project.

Contributors

Package Sidebar

Install

npm i decomock

Weekly Downloads

2

Version

1.0.15

License

ISC

Unpacked Size

18.9 kB

Total Files

43

Last publish

Collaborators

  • guignol