1.0.15 • Published 4 years ago

decomock v1.0.15

Weekly downloads
4
License
ISC
Repository
github
Last release
4 years ago

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;
}

!!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

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago