Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Easy way to generate mocks #43

Open
levibostian opened this issue Nov 4, 2020 · 1 comment
Open

Easy way to generate mocks #43

levibostian opened this issue Nov 4, 2020 · 1 comment

Comments

@levibostian
Copy link
Owner

At this time, I am generating mocks manually in my code. The follow a very simple process:

interface Foo {
  bar(): string
}

class FooMock implements Foo {
  public barMock = jest.fn()

  bar: barMock
}

The problem is maintaining them. They are simply annoying to write which gets me to be lazy and not want to write them.

If there was a way to generate them automatically, that would be great. How do we do that?

  • Decorators Will not work because it does not work on interfaces and it only passes the constructor of class when used on a class. No way to traverse an interface.
  • Babel - I would prefer not to need to do this because I don't want my project to use babel. But if there was a way for babel to traverse my code and run my plugin and that's it, then maybe.
  • ts-morph - possibly. It seems to be a nice project but I don't like relying on projects that are "still in development"
  • Create a typescript transformer - these are the official way to create plugins for the typescript compiler.

FYI: handy ast viewer

@levibostian
Copy link
Owner Author

Here is a project that seems to do this: https://github.com/marchaos/jest-mock-extended

However, it has had a history of not being maintained and for some reason, it overrides the jest mock matcher API with Jasmine's. I am thinking of forking it and editing parts of it to be smaller and use the original jest matcher api.

See this issue for discussion around this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant