ws-dependency-mock

Library for mock web socket server.

Usage no npm install needed!

<script type="module">
  import wsDependencyMock from 'https://cdn.skypack.dev/ws-dependency-mock';
</script>

README

ws-dependency-mock

Install

npm i ws-dependency-mock --save

Usage

Validations

import { IsInt, IsEmail, Min, IsNotEmpty, IsString, MinLength, MaxLength } from 'class-validator';

export class UserModel {
    @IsNotEmpty()
    @IsInt()
    @Min(1)
    id: number;

    @IsNotEmpty()
    @IsString()
    @MinLength(2)
    @MaxLength(255)
    first: string;

    @IsNotEmpty()
    @IsString()
    @MinLength(2)
    @MaxLength(255)
    last: string;

    @IsNotEmpty()
    @IsString()
    @IsEmail()
    email: number;
}

Contract


import { AbstractContract } from '../../../src/abstract-contract';
import { UserModel } from '../../../test/src/models/user.model';

export class UserContract extends AbstractContract {

    init(): void {
        this
            .add({
                validation: UserModel, // Check validation
                payload: {             // Your mocked payload
                    test: 'success',
                },
                repeat: 1,             // Count of repeat. Default: 1
                timeout: 500           // Timeout return mock payload. Default: 0
            });
    }
}

Contract registration
import { ContractInterface } from '../../src/interfaces/contract.interface';
import { ContractNotFoundException } from '../../src/exceptions/contract-not-found.exception';
import { UserContract } from '../../test/src/contracts/user.contract';

export class ContractFactory {
    create(key: string): ContractInterface {
        switch (key) {
            case 'user': return new UserContract();
            default: throw new ContractNotFoundException(key);
        }
    }
}


Run
const wsm = new WebSocketMock({
    host: 'localhost',
    port: 8080,
});

wsm.run(new ContractFactory());

Dependencies

Enjoy!