-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Expand file tree
/
Copy pathbar.service.spec.ts
More file actions
57 lines (54 loc) · 1.63 KB
/
bar.service.spec.ts
File metadata and controls
57 lines (54 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { Test } from '@nestjs/testing';
import { BarService } from '../src/bar.service.js';
import { FooService } from '../src/foo.service.js';
describe('Auto-Mocking Bar Deps', () => {
let service: BarService;
let fooService: FooService;
const stub = vi.fn();
beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
providers: [BarService],
})
.useMocker(() => ({ foo: stub }))
.compile();
service = moduleRef.get(BarService);
fooService = moduleRef.get(FooService);
});
it('should be defined', () => {
expect(service).not.toBeUndefined();
expect(fooService).not.toBeUndefined();
});
it('should call bar.bar', () => {
service.bar();
expect(stub).toHaveBeenCalled();
});
});
describe('Auto-Mocking with token in factory', () => {
it('can mock the dependencies', async () => {
const moduleRef = await Test.createTestingModule({
providers: [BarService],
})
.useMocker(token => {
if (token === FooService) {
return { foo: vi.fn() };
}
})
.compile();
const service = moduleRef.get(BarService);
const fooServ = moduleRef.get<{ foo: ReturnType<typeof vi.fn> }>(
FooService as any,
);
service.bar();
expect(fooServ.foo).toHaveBeenCalled();
});
it('cannot mock the dependencies', async () => {
const moduleRef = Test.createTestingModule({
providers: [BarService],
}).useMocker(token => {
if (token === FooService.name + 'something that fails the token') {
return { foo: vi.fn() };
}
}).compile;
await expect(moduleRef()).rejects.toThrow();
});
});