npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@typescripttdd/ts-auto-mock

v1.4.1

Published

Typescript transformer to unlock automatic mock creation for interfaces and classes

Downloads

3

Readme

Ts Auto Mock

Actions Status CircleCI npm version Downloads Greenkeeper badge

slack Need help? Join us on Slack link

A Typescript transformer that will allow you to create mock for any types (Interfaces, Classes, ...) without need to create manual fakes/mocks.

Let's have a look.

Requirements

typescript@^3.2.2

Installation

A Transformer needs to be provided at compile time. There are different ways to do it. Please read the following guide to find your configuration

Usage

Create mock

import { createMock } from 'ts-auto-mock';

interface Person {
  id: string;
  getName(): string;
  details: {
      phone: number
  }
}
const mock = createMock<Person>();
mock.id // ""
mock.getName() // ""
mock.details // "{phone: 0} "
Default values

You can also define default values to overrides specific fields You dont have to provide the entire interface, just a partial of the one to mock

import { createMock } from 'ts-auto-mock';

interface Person {
  id: string;
  getName(): string;
  details: {
      phone: number
  }
}
const mock = createMock<Person>({
details: {
    phone: 07423232323
}
});
mock.id // ""
mock.getName() // ""
mock.details // "{phone: 07423232323} "

Create mock list

createMock list it will create a list of mocks automatically

import { createMockList } from 'ts-auto-mock';

interface Person {
  id: string;
}
const mockList = createMockList<Person>(2);
mockList.length // 2
Default values

You can define a function to overrides specific fields The function will have access to the current index

import { createMockList } from 'ts-auto-mock';

interface Person {
  id: string;
}
const mockList = createMockList<Person>(2, (index: number) => {
    return {
        id: "id" + index
    }
});
mockList[0].id // id0
mockList[1].id // id1

Type Examples

The library try to convert the type given to createMock so you dont need to create concrete mock manually. Open this link to see more examples

Type Not Supported

The library will convert to null when the type is not supported. Open this link to see what is not supported

Extension

The library allows you to extends some functionality to work nicely with framework like jasmine or jest Open this link to see more examples

Options

tsAutoMockTransformer(program: ts.Program, options: TsAutoMockOptions)

interface TsAutoMockOptions {
    debug: boolean | 'file' | 'console';
    cacheBetweenTests: boolean;
}

options:

| Name | Default | Description | | ------------- | --------------------------- | --------------- | | debug | false | When set to true or console it will log to the console | | | When set to file it will log to a file (tsAutoMock.log) | cacheBetweenTests | true | When set to true it will reuse mocks between different tests | | | When set to false it create new mocks for each different tests

Debug

We currently support

  • Logs for not supported types It will log any not supported type automatically converted to null. This is useful to report an issue or to investigate a potential bug

cacheBetweenTests

One of the main functionality of ts auto mock is to generate mocks and cache them.

Mocks are currently created in the test file making tests to depend to each other

Example:

  • test1.test.ts has a createMock of Interface.
  • test2.test.ts has a createMock of Interface.
  • test1.test.ts will have the registration of Interface mock
  • test2.test.ts will have a registration import.

If test2 run in a different context than test1 it will not be able to access to the same mock.

Set this property to false when your test run in different context.

We are working on an issue to make sure tests do not depend to each other but they will still take advance of a cache system

Changelog

Authors

License

This project is licensed under the MIT License