mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
57 lines
2.0 KiB
TypeScript
57 lines
2.0 KiB
TypeScript
import { ExpressAdapter } from '@nestjs/platform-express';
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
import express from 'express';
|
|
import { AppModule } from '../src/app.module.js';
|
|
import { randomPort } from './utils.js';
|
|
|
|
describe('Get URL (Express Application)', () => {
|
|
let testModule: TestingModule;
|
|
let port: number;
|
|
|
|
beforeEach(async () => {
|
|
testModule = await Test.createTestingModule({
|
|
imports: [AppModule],
|
|
}).compile();
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
port = await randomPort();
|
|
});
|
|
|
|
it('should be able to get a loopback address', async () => {
|
|
const app = testModule.createNestApplication(new ExpressAdapter(express()));
|
|
await app.listen(port);
|
|
expect(await app.getUrl()).toMatch(
|
|
new RegExp(`http://(\\[::1\\]|127\\.0\\.0\\.1):${port}`),
|
|
);
|
|
await app.close();
|
|
});
|
|
it('should be able to get the IPv4 address', async () => {
|
|
const app = testModule.createNestApplication(new ExpressAdapter(express()));
|
|
await app.listen(port, '127.0.0.1');
|
|
expect(await app.getUrl()).toEqual(`http://127.0.0.1:${port}`);
|
|
await app.close();
|
|
});
|
|
it('should return 127.0.0.1 for 0.0.0.0', async () => {
|
|
const app = testModule.createNestApplication(new ExpressAdapter(express()));
|
|
await app.listen(port, '0.0.0.0');
|
|
expect(await app.getUrl()).toEqual(`http://127.0.0.1:${port}`);
|
|
await app.close();
|
|
});
|
|
it('should return a loopback address in a callback (default bind)', () => {
|
|
const app = testModule.createNestApplication(new ExpressAdapter(express()));
|
|
return app.listen(port, async () => {
|
|
expect(await app.getUrl()).toMatch(
|
|
new RegExp(`http://(\\[::1\\]|127\\.0\\.0\\.1):${port}`),
|
|
);
|
|
await app.close();
|
|
});
|
|
});
|
|
it('should throw an error for calling getUrl before listen', async () => {
|
|
const app = testModule.createNestApplication(new ExpressAdapter(express()));
|
|
await expect(app.getUrl()).rejects.toEqual(
|
|
'app.listen() needs to be called before calling app.getUrl()',
|
|
);
|
|
});
|
|
});
|