Skip to content

Commit e88f172

Browse files
jbl428imdudu1
andcommitted
test: add http interface module test case
Co-authored-by: imdudu1 <[email protected]>
1 parent f31546b commit e88f172

File tree

5 files changed

+555
-6
lines changed

5 files changed

+555
-6
lines changed

lib/http-interface.module.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Test } from '@nestjs/testing';
2+
import { describe, expect, test } from 'vitest';
3+
import { GetExchange, HttpInterface } from './decorators';
4+
import { StubHttpClient } from './fixtures/stub-http-client';
5+
import { HttpInterfaceModule } from './http-interface.module';
6+
import { imitation } from './supports';
7+
8+
describe('HttpInterfaceModule', () => {
9+
@HttpInterface('http://localhost:3000')
10+
class TestHttpService {
11+
@GetExchange('/api/v1/test')
12+
async request(): Promise<string> {
13+
return imitation();
14+
}
15+
}
16+
17+
test('should request with given client', async () => {
18+
// given
19+
const httpClient = new StubHttpClient();
20+
const module = await Test.createTestingModule({
21+
imports: [HttpInterfaceModule.register({ httpClient })],
22+
providers: [TestHttpService],
23+
}).compile();
24+
await module.init();
25+
const app = module.createNestApplication();
26+
const service = app.get(TestHttpService);
27+
httpClient.addResponse({ data: 'ok' });
28+
29+
// when
30+
const response = await service.request();
31+
32+
// then
33+
expect(response).toBe('{"data":"ok"}');
34+
});
35+
});

lib/http-interface.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface HttpInterfaceConfig {
1414
}
1515

1616
export class HttpInterfaceModule {
17-
static register(config: HttpInterfaceConfig): DynamicModule {
17+
static register(config: HttpInterfaceConfig = {}): DynamicModule {
1818
return {
1919
imports: [DiscoveryModule],
2020
module: HttpInterfaceModule,

lib/supports/node-fetch.injector.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
HTTP_INTERFACE_METADATA,
99
RESPONSE_BODY_METADATA,
1010
} from '../decorators';
11-
import { HttpClient } from '../types/http-client.interface';
11+
import { HttpClient } from '../types';
1212

1313
@Injectable()
1414
export class NodeFetchInjector implements OnModuleInit {
@@ -71,8 +71,11 @@ export class NodeFetchInjector implements OnModuleInit {
7171
.getProviders()
7272
.filter(
7373
(wrapper) =>
74-
Reflect.getMetadata(HTTP_INTERFACE_METADATA, wrapper.metatype) ==
75-
null,
74+
wrapper.metatype?.prototype != null &&
75+
Reflect.hasMetadata(
76+
HTTP_INTERFACE_METADATA,
77+
wrapper.metatype.prototype,
78+
),
7679
);
7780
}
7881
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
"devDependencies": {
3030
"@nestjs/common": "^9.4.0",
3131
"@nestjs/core": "^9.4.0",
32+
"@nestjs/platform-express": "^9.4.0",
33+
"@nestjs/testing": "^9.4.0",
3234
"@vitest/coverage-c8": "^0.30.1",
3335
"class-transformer": "^0.5.1",
3436
"eslint": "^8.37.0",

0 commit comments

Comments
 (0)