Browse Source

feat(nestjs): Update nest setup in onboarding (#74929)

related to https://github.com/getsentry/sentry-javascript/pull/12920

There is a new way to setup the nest SDK. Updating the product
onboarding accordingly.

---------

Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com>
Nicolas Hrubec 7 months ago
parent
commit
cea631ae99

+ 3 - 7
static/app/gettingStartedDocs/node/nestjs.spec.tsx

@@ -18,22 +18,18 @@ describe('Nest.js onboarding docs', function () {
 
     // Includes import statement
     const allMatches = screen.getAllByText(
-      textWithMarkupMatcher(/import \* as Sentry from "@sentry\/nestjs"/)
+      textWithMarkupMatcher(/import \{ SentryModule } from '@sentry\/nestjs\/setup'/)
     );
     allMatches.forEach(match => {
       expect(match).toBeInTheDocument();
     });
   });
 
-  it('includes error handler', () => {
+  it('includes root module', () => {
     renderWithOnboardingLayout(docs);
 
     expect(
-      screen.getByText(
-        textWithMarkupMatcher(
-          /Sentry\.setupNestErrorHandler\(app, new BaseExceptionFilter\(httpAdapter\)\)/
-        )
-      )
+      screen.getByText(textWithMarkupMatcher(/SentryModule\.forRoot\(\)/))
     ).toBeInTheDocument();
   });
 

+ 43 - 11
static/app/gettingStartedDocs/node/nestjs.tsx

@@ -19,7 +19,6 @@ import {
   getImportInstrumentSnippet,
   getInstallConfig,
   getSdkInitSnippet,
-  getSentryImportSnippet,
 } from 'sentry/utils/gettingStartedDocs/node';
 
 type Params = DocsParams;
@@ -28,26 +27,39 @@ const getSdkSetupSnippet = () => `
 ${getImportInstrumentSnippet('esm')}
 
 // All other imports below
-${getSentryImportSnippet('nestjs', 'esm')}
-import { BaseExceptionFilter, HttpAdapterHost, NestFactory } from '@nestjs/core';
+import { NestFactory } from '@nestjs/core';
 import { AppModule } from './app.module';
 
 async function bootstrap() {
   const app = await NestFactory.create(AppModule);
-
-  const { httpAdapter } = app.get(HttpAdapterHost);
-  Sentry.setupNestErrorHandler(app, new BaseExceptionFilter(httpAdapter));
-
   await app.listen(3000);
 }
 
 bootstrap();
 `;
 
+const getAppModuleSnippet = () => `
+import { Module } from '@nestjs/common';
+import { SentryModule } from '@sentry/nestjs/setup';
+import { AppController } from './app.controller';
+import { AppService } from './app.service';
+
+@Module({
+  imports: [
+    SentryModule.forRoot(),
+    // ...other modules
+  ],
+  controllers: [AppController],
+  providers: [AppService],
+})
+export class AppModule {}
+`;
+
 const getVerifySnippet = () => `
-app.use(async function () {
+@Get("/debug-sentry")
+getError() {
   throw new Error("My first Sentry error!");
-});
+}
 `;
 
 const onboarding: OnboardingConfig = {
@@ -84,11 +96,10 @@ const onboarding: OnboardingConfig = {
         },
         {
           description: tct(
-            'Make sure to import [code1:instrument.js/mjs] at the top of your [code2:main.ts/js] file. Set up the error handler by passing the [code3:BaseExceptionFilter].',
+            'Import [code1:instrument.js/mjs] in your [code2:main.ts/js] file:',
             {
               code1: <code />,
               code2: <code />,
-              code3: <code />,
               docs: (
                 <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/nestjs/install/" />
               ),
@@ -104,6 +115,27 @@ const onboarding: OnboardingConfig = {
             },
           ],
         },
+        {
+          description: tct(
+            'Then you can add the [code1:SentryModule] as a root module. The [code2:SentryModule] needs to be registered before any other module that should be instrumented by Sentry.',
+            {
+              code1: <code />,
+              code2: <code />,
+              docs: (
+                <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/nestjs/install/" />
+              ),
+            }
+          ),
+          code: [
+            {
+              label: 'JavaScript',
+              value: 'javascript',
+              language: 'javascript',
+              filename: 'app.module.(js|ts)',
+              code: getAppModuleSnippet(),
+            },
+          ],
+        },
       ],
     },
     getUploadSourceMapsStep({