Simple type-safe mocking library for TypeScript.
Why use this?
- Usable with either Jasmine or Jest.
- The primary focus is type-safety.
- Spies are setup for mocked functions.
- Nested objects and arrays can be mocked with type-safety
How do I use this?
To create a Mock:
To update a Mock:
More usage examples can be found @ https://stackblitz.com/edit/ts-mockery-examples?file=tests.ts
To mock a static method:
;Mock.staticMethodObjectToMock, 'static','not hi';
We noticed issues when we gave instantiated base class objects to
Mock.of. The reason being that the type checker does not look at the prototype.
Also when setting mocked property to an observable there is a circular reference that would throw an
To mock a function you do not care about:
We got you covered,
Mock.noop will return you a spied on function.
To mock an object you do not care about:
With Mock.all it will use a proxy to create spies as on demand.
How do I configure this?
Create a test setup file to be included in your Jest or Jasmine config.
;// The argument to configure can be either jest, jasmine, noop, or an object that implements the exported SpyAdapater interfaceMock.configure'jest';
The above can be added directly to your karma test shim if you'd like.
To configure in Jest add the mockery configuration into the jest config with the key "setupFiles" like so:
It is important that this file is included before tests run.
Jest for whatever reason does not reset mocks between tests by default. This causes problems with the mocking of static methods. If you intend to use static method mocking you can add "restoreMocks: true" to your jest config and all will be right in the world.