Skip to content

Commit 1a10a43

Browse files
committed
add sdk metadata test
1 parent d98fc69 commit 1a10a43

File tree

11 files changed

+86
-11
lines changed

11 files changed

+86
-11
lines changed

packages/nuxt/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@
4141
"@nuxt/kit": "^3.12.2"
4242
},
4343
"devDependencies": {
44-
"@babel/types": "7.20.7",
4544
"@nuxt/module-builder": "0.8.0",
46-
"nuxt": "^3.12.2",
47-
"vite": "^5.0.10"
45+
"nuxt": "^3.12.2"
4846
},
4947
"scripts": {
5048
"build": "run-p build:transpile build:types",

packages/nuxt/src/client/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from '@sentry/vue';
2+
3+
export { init } from './sdk';

packages/nuxt/src/client/sdk.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { applySdkMetadata } from '@sentry/core';
2+
import {init as initVue } from '@sentry/vue';
3+
import type { SentryVueOptions } from '../common/types';
4+
import type {Options } from '@sentry/types';
5+
6+
/**
7+
* Initializes the client-side of the Nuxt SDK
8+
*
9+
* @param options Configuration options for the SDK.
10+
*/
11+
export function init(options: SentryVueOptions): void {
12+
const sentryOptions = {
13+
...options,
14+
};
15+
16+
// Type cast to `Options` because Vue has slightly different options, but type is not highly relevant here
17+
applySdkMetadata(sentryOptions as Options, 'nuxt', ['nuxt', 'vue']);
18+
19+
initVue(sentryOptions);
20+
}

packages/nuxt/src/common/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import type {init} from '@sentry/vue';
2+
3+
export type SentryVueOptions = Parameters<typeof init>[0] & object;

packages/nuxt/src/index.client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './client';

packages/nuxt/src/module/index.ts renamed to packages/nuxt/src/module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type Resolver, addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit';
2-
import type * as Sentry from '@sentry/vue';
2+
import type { SentryVueOptions } from './common/types';
33

4-
export type ModuleOptions = Parameters<typeof Sentry.init>[0] & object;
4+
export type ModuleOptions = SentryVueOptions;
55

66
export default defineNuxtModule<ModuleOptions>({
77
meta: {
@@ -22,7 +22,7 @@ export default defineNuxtModule<ModuleOptions>({
2222
if (resolver) {
2323
// Ignore because `.resolve` is a valid method of `Resolver`, but because of ts-ignore above, `Resolver` is recognized as any
2424
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
25-
addPlugin(resolver.resolve('runtime/client/plugin.js'));
25+
addPlugin(resolver.resolve('runtime/plugins/sentry.client.js'));
2626
}
2727
},
2828
});

packages/nuxt/src/runtime/client/plugin.ts renamed to packages/nuxt/src/runtime/plugins/sentry.client.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { applySdkMetadata } from '@sentry/core';
2-
import * as Sentry from '@sentry/vue';
31
import { defineNuxtPlugin, useRuntimeConfig } from 'nuxt/app';
2+
import { init } from '../../client';
43

54
export default defineNuxtPlugin(nuxtApp => {
65
const config = useRuntimeConfig();
@@ -10,9 +9,7 @@ export default defineNuxtPlugin(nuxtApp => {
109

1110
const sentryConfig = config.public.sentry || {};
1211

13-
applySdkMetadata(sentryConfig, 'nuxt', ['nuxt', 'vue']);
14-
15-
Sentry.init({
12+
init({
1613
...sentryConfig,
1714
app: nuxtApp.vueApp,
1815
});

packages/nuxt/test/client/sdk.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { SDK_VERSION } from '@sentry/vue';
2+
import * as SentryVue from '@sentry/vue';
3+
import { vi, describe, beforeEach } from 'vitest';
4+
import { init } from '../../src/client';
5+
6+
const vueInit = vi.spyOn(SentryVue, 'init');
7+
8+
describe('Nuxt CLient SDK', () => {
9+
describe('init', () => {
10+
beforeEach(() => {
11+
vi.clearAllMocks();
12+
});
13+
14+
it('adds Nuxt metadata to the SDK options', () => {
15+
expect(vueInit).not.toHaveBeenCalled();
16+
17+
init({
18+
dsn: 'https://[email protected]/1337',
19+
});
20+
21+
const expectedMetadata = {
22+
_metadata: {
23+
sdk: {
24+
name: 'sentry.javascript.nuxt',
25+
version: SDK_VERSION,
26+
packages: [
27+
{ name: 'npm:@sentry/nuxt', version: SDK_VERSION },
28+
{ name: 'npm:@sentry/vue', version: SDK_VERSION },
29+
],
30+
},
31+
},
32+
};
33+
34+
expect(vueInit).toHaveBeenCalledTimes(1);
35+
expect(vueInit).toHaveBeenLastCalledWith(expect.objectContaining(expectedMetadata));
36+
});
37+
});
38+
});

packages/nuxt/test/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../tsconfig.test.json"
3+
}

packages/nuxt/test/vitest.setup.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function setup() {}
2+
3+
if (!globalThis.fetch) {
4+
// @ts-expect-error - Needed for vitest to work with our fetch instrumentation
5+
globalThis.Request = class Request {};
6+
// @ts-expect-error - Needed for vitest to work with our fetch instrumentation
7+
globalThis.Response = class Response {};
8+
}

packages/nuxt/vite.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ import baseConfig from '../../vite/vite.config';
22

33
export default {
44
...baseConfig,
5+
test: {
6+
environment: 'jsdom',
7+
setupFiles: ['./test/vitest.setup.ts'],
8+
},
59
};

0 commit comments

Comments
 (0)