Files
nest/integration/nest-application/get-url/e2e/express.spec.ts
2026-02-15 14:17:05 +01:00

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()',
);
});
});