Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Initialize application only once #12009

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/core/nest-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ export class NestApplication
}

public async init(): Promise<this> {
if (this.isInitialized) {
return this;
}

this.applyOptions();
await this.httpAdapter?.init();

Expand Down
25 changes: 25 additions & 0 deletions packages/core/test/nest-application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { NestApplication } from '../nest-application';
import { mapToExcludeRoute } from './../middleware/utils';
import { NoopHttpAdapter } from './utils/noop-adapter.spec';
import { MicroserviceOptions } from '@nestjs/microservices';
import * as sinon from 'sinon';

describe('NestApplication', () => {
describe('Hybrid Application', () => {
Expand Down Expand Up @@ -79,4 +80,28 @@ describe('NestApplication', () => {
});
});
});
describe('Double initialization', () => {
it('should initialize application only once', async () => {
const noopHttpAdapter = new NoopHttpAdapter({});
const httpAdapterSpy = sinon.spy(noopHttpAdapter);

const applicationConfig = new ApplicationConfig();

const container = new NestContainer(applicationConfig);
container.setHttpAdapter(noopHttpAdapter);

const instance = new NestApplication(
container,
noopHttpAdapter,
applicationConfig,
new GraphInspector(container),
{},
);

await instance.init();
await instance.init();

expect(httpAdapterSpy.init.calledOnce).to.be.true;
});
});
});