Socket
Socket
Sign inDemoInstall

ws-dependency-mock

Package Overview
Dependencies
8
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ws-dependency-mock

Library for mock web socket server.


Version published
Weekly downloads
3
Maintainers
1
Install size
1.48 MB
Created
Weekly downloads
 

Readme

Source

##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
  • ws
  • class-validatior

Enjoy!

FAQs

Last updated on 31 Aug 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc