Hello,
I was about to propose this utility to integrate into our app and run into issue in which I have same token in my service used more than once. Take a look at this:
import { TestBed } from '@automock/jest';
import { Inject, Injectable } from '@nestjs/common';
import { v4 } from 'uuid';
@Injectable()
export class IdGenerator<T extends string> {
generate(): T {
return v4() as T;
}
}
export type IdFor<T extends string> = string & T;
type FirstEntityId = IdFor<'FirstEntity'>;
type SecondEntityId = IdFor<'SecondEntity'>;
@Injectable()
class Hello {
say(): void {
console.log('hello');
}
}
@Injectable()
export class FirstService {
constructor(
private readonly idFirstGenerator: IdGenerator<FirstEntityId>,
private readonly idSecondGenerator: IdGenerator<SecondEntityId>,
private readonly hello: Hello,
) {}
async foo(): Promise<void> {
console.log(this);
this.hello.say();
}
}
@Injectable()
export class SecondService {
constructor(
private readonly idFirstGenerator: IdGenerator<FirstEntityId>,
private readonly hello: Hello,
) {}
async foo(): Promise<void> {
console.log(this);
this.hello.say();
}
}
it('foo 1', async () => {
const { unit, unitRef } = TestBed.create(FirstService).compile();
const helloModule = unitRef.get(Hello);
const spy = jest.spyOn(helloModule, 'say');
await unit.foo();
expect(spy).toBeCalledTimes(1);
});
it('foo 2', async () => {
const { unit, unitRef } = TestBed.create(SecondService).compile();
const helloModule = unitRef.get(Hello);
const spy = jest.spyOn(helloModule, 'say');
await unit.foo();
expect(spy).toBeCalledTimes(1);
});
Hello,
I was about to propose this utility to integrate into our app and run into issue in which I have same token in my service used more than once. Take a look at this: