GithubHelp home page GithubHelp logo

typestrong / ts-mockito Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nagrock/ts-mockito

30.0 4.0 8.0 791 KB

Mocking library for TypeScript

License: MIT License

JavaScript 0.64% TypeScript 99.36%
hacktoberfest

ts-mockito's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ts-mockito's Issues

Unsafe Argument matchers due to return any

When trying to enable the recommended-type-checked from typescript eslint, I found out that most of ts-mockito argument matchers are actually unsafe since most of them returns any, which completely disables type checking.

An example of a common violated rule when using argument matchers, is no-unsafe-argument

Probably, returning any should be avoided for all argument matchers. Here's a suggestion that I'm evaluating:

export declare function anyOfClass<T>(expectedClass: new (...args: any[]) => T): T;
export declare function anyFunction(): Function;
export declare function anyNumber(): number;
export declare function anyString(): string;
export declare function anything<T>(): T;
export declare function between(min: number, max: number): number;
export declare function deepEqual<T>(expectedValue: T): T;
export declare function notNull<T>(): T;
export declare function strictEqual<T>(expectedValue: T): T;
export declare function match(expectedValue: RegExp | string): string;
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
export declare function objectContaining<T extends Object, V extends Optional<T, keyof T>>(expectedValue: V): T;

Importing this package breaks due to incorrect "main" and "typings"

In the latest version, the package cannot be imported as the "main" and "typings" values of the package.json point to an invalid path. It looks like they're pointing to a lib folder which does not exist in what has been submitted to npm. I can see the lib folder is generated on source compilation but it looks like the project pushes one level lower to npm.

This should either be fixed by changing the package.json to point at the right place, or re-pushing with the lib folder to npm. I will submit a PR for the former but feel free to go with the latter

Feature request: Allow overlapping matchers

It seems like overlapping matchers end up raising an error. It would be helpful if matchers could simply take precedence over each other, e.g. according to the order in which they are specified.

This could allow specifying default (non-null) behavior for a function as well as a different result for specific inputs.

This would make it easy to effectively implement #41 on my own.

Feature request: option to throw instead of returning `null` for non-mocked properties

When writing tests, I like to require explicitly mocking all properties of a mock object that end up getting used by my code. Is there a way for a mock to immediately raise if any property is accessed that has not been mocked via a call to when?

The other thing that irks me about returning null by default is that the mock object may not actually be abiding by it's type, leading to potentially unexpected behavior when writing tests using mocks.

How to mock a standalone function?

I want to mock a standalone function (i.e. a function that's not a method of some object) but I can't find information on that in the readme.

Here is a simplified version of what I'm trying to do:

type Callback = (x: number) => void;

const caller = (callback: Callback) => {
    callback(Math.random())
}

let mockCallback: Callback = mock<Callback>();

caller(mockCallback);            // fails when using mock without instance
caller(instance(mockCallback));  // fails when using mock with instance
caller(x => console.log(x))      // works when not using a mock

As shown above, the first two caller(...) calls that use the mock would fail. They fail with the error:

TypeError: callback is not a function
 ❯ caller src/test.test.ts:7:5
      5|
      6| const caller = (callback: Callback) => {
      7|     callback(Math.random())
       |     ^
      8| }

I want to mock a standalone function so that I can capture its arguments and do stuff with them. Could you please help me out? Thanks

Publishing to npm

@LironHazan what's your npm username? I'm going to give you publish access to publish @typestrong/ts-mockito

Also, I think you'll need to merge #11 before you do. #11 renames the package from @cspotcode/ to @typestrong/

Using spy on a TS > 4.3.x project causes CombineLatest ref to be function instead of Observable

@pauleustice

Thinking out load:
With TS 4.4.x following line fails:
service.combinedProviderObservable$.subscribe((value)

TypeError: service.combinedProviderObservable$.subscribe is not a function

I've printed the service for 4.4.x and got:
'**combinedProviderObservable$**': [Function (anonymous)],

And for 4.3.x we get:

     'combinedProviderObservable$': Observable {
        _isScalar: false,
        source: Observable {
          _isScalar: false,
          source: [Observable],
          operator: [CombineLatestOperator]
        },
        operator: MapOperator { project: [Function (anonymous)], thisArg: undefined }
      }

When removing the use of spy it works in 4.4.x,
In TS 4.4.x: When using spy CombineLatest returns a function ref and without the spy the CombineLatest returns an observable.
Won't happen in TS 4.3.x
Wow I don't have a clue,
I'm unfamiliar as well with the spy related code, but what I can do in this repo is adding the test with rxjs that would fail and then we can debug on ts-mockito source

In-Code Documentation with TSDoc

As a developer, I would like to have an in-code-documentation, so that I can read the features and expected behaviours from within the IDE. I would strive for a gradual implementation, so that over the future development the documentation gets progressively better.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.