HomeInstallationCreate mockCreate mock listCreate hydrated mockRegister mockExtensionTypes supportedTypes not supportedConfigPerformanceDefinitely TypedLocal development
Register mock
registerMock
will register your custom mock that will be re-used automatically whenever another mock is identified as converting the same interface.
./person.ts
export interface Person {id: string;}
./person-fake.ts
import { Person } from './person';export class PersonFake extends Person {public id: string;public name: string;constructor() {this.id = "Basic Id";this.name = "Basic name";}}
./context.ts
import { registerMock } from 'ts-auto-mock';import { Person } from './person';import { PersonFake } from './person-fake';registerMock<Person>(() => new PersonFake());
./my-test.ts
interface Wrapper {person: Person;}const mock: Wrapper = createMock<Wrapper>();mock.person // PersonFakemock.person.id // "Basic Id"mock.person.name // "Basic name"
When injecting fake (/stubbed) implementations, we recommend using the extension strategy to retrieve the faked object.
An example of usage for Promise->FakePromise can be found in the test folder.
Note: You can only use it in a common file (webpack context.ts, mocha tsnode.js, etc), using registerMock
in other files will give unexpected results.