Skip to content
Closed
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
5 changes: 2 additions & 3 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,8 @@
"clean": "rimraf build coverage sentry-node-*.tgz",
"fix": "eslint . --format stylish --fix",
"lint": "eslint . --format stylish",
"test": "yarn test:jest",
"test:jest": "jest",
"test:watch": "jest --watch",
"test": "vitest run",
"test:watch": "vitest watch",
"yalc:publish": "yalc publish --push --sig"
},
"volta": {
Expand Down
177 changes: 92 additions & 85 deletions packages/node/test/cron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import { cron } from '../src';
import type { CronJob, CronJobParams } from '../src/cron/cron';
import type { NodeCron, NodeCronOptions } from '../src/cron/node-cron';

import type { MockInstance } from 'vitest';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';

describe('cron check-ins', () => {
let withMonitorSpy: jest.SpyInstance;
let withMonitorSpy: MockInstance;

beforeEach(() => {
withMonitorSpy = jest.spyOn(SentryCore, 'withMonitor');
withMonitorSpy = vi.spyOn(SentryCore, 'withMonitor');
});

afterEach(() => {
jest.restoreAllMocks();
vi.restoreAllMocks();
});

describe('cron', () => {
Expand Down Expand Up @@ -48,43 +51,45 @@ describe('cron check-ins', () => {
}
}

test('new CronJob()', done => {
expect.assertions(4);

const CronJobWithCheckIn = cron.instrumentCron(CronJobMock, 'my-cron-job');

new CronJobWithCheckIn(
'* * * Jan,Sep Sun',
() => {
expect(withMonitorSpy).toHaveBeenCalledTimes(1);
expect(withMonitorSpy).toHaveBeenLastCalledWith('my-cron-job', expect.anything(), {
schedule: { type: 'crontab', value: '* * * 1,9 0' },
timezone: 'America/Los_Angeles',
});
done();
},
undefined,
true,
'America/Los_Angeles',
);
});
test('new CronJob()', () =>
new Promise<void>(done => {
expect.assertions(4);

const CronJobWithCheckIn = cron.instrumentCron(CronJobMock, 'my-cron-job');

new CronJobWithCheckIn(
'* * * Jan,Sep Sun',
() => {
expect(withMonitorSpy).toHaveBeenCalledTimes(1);
expect(withMonitorSpy).toHaveBeenLastCalledWith('my-cron-job', expect.anything(), {
schedule: { type: 'crontab', value: '* * * 1,9 0' },
timezone: 'America/Los_Angeles',
});
done();
},
undefined,
true,
'America/Los_Angeles',
);
}));

test('CronJob.from()', done => {
expect.assertions(4);
test('CronJob.from()', () =>
new Promise<void>(done => {
expect.assertions(4);

const CronJobWithCheckIn = cron.instrumentCron(CronJobMock, 'my-cron-job');
const CronJobWithCheckIn = cron.instrumentCron(CronJobMock, 'my-cron-job');

CronJobWithCheckIn.from({
cronTime: '* * * Jan,Sep Sun',
onTick: () => {
expect(withMonitorSpy).toHaveBeenCalledTimes(1);
expect(withMonitorSpy).toHaveBeenLastCalledWith('my-cron-job', expect.anything(), {
schedule: { type: 'crontab', value: '* * * 1,9 0' },
});
done();
},
});
});
CronJobWithCheckIn.from({
cronTime: '* * * Jan,Sep Sun',
onTick: () => {
expect(withMonitorSpy).toHaveBeenCalledTimes(1);
expect(withMonitorSpy).toHaveBeenLastCalledWith('my-cron-job', expect.anything(), {
schedule: { type: 'crontab', value: '* * * 1,9 0' },
});
done();
},
});
}));

test('throws with multiple jobs same name', () => {
const CronJobWithCheckIn = cron.instrumentCron(CronJobMock, 'my-cron-job');
Expand All @@ -108,32 +113,33 @@ describe('cron check-ins', () => {
});

describe('node-cron', () => {
test('calls withMonitor', done => {
expect.assertions(5);

const nodeCron: NodeCron = {
schedule: (expression: string, callback: () => void, options?: NodeCronOptions): unknown => {
expect(expression).toBe('* * * Jan,Sep Sun');
expect(callback).toBeInstanceOf(Function);
expect(options?.name).toBe('my-cron-job');
return callback();
},
};

const cronWithCheckIn = cron.instrumentNodeCron(nodeCron);

cronWithCheckIn.schedule(
'* * * Jan,Sep Sun',
() => {
expect(withMonitorSpy).toHaveBeenCalledTimes(1);
expect(withMonitorSpy).toHaveBeenLastCalledWith('my-cron-job', expect.anything(), {
schedule: { type: 'crontab', value: '* * * 1,9 0' },
});
done();
},
{ name: 'my-cron-job' },
);
});
test('calls withMonitor', () =>
new Promise<void>(done => {
expect.assertions(5);

const nodeCron: NodeCron = {
schedule: (expression: string, callback: () => void, options?: NodeCronOptions): unknown => {
expect(expression).toBe('* * * Jan,Sep Sun');
expect(callback).toBeInstanceOf(Function);
expect(options?.name).toBe('my-cron-job');
return callback();
},
};

const cronWithCheckIn = cron.instrumentNodeCron(nodeCron);

cronWithCheckIn.schedule(
'* * * Jan,Sep Sun',
() => {
expect(withMonitorSpy).toHaveBeenCalledTimes(1);
expect(withMonitorSpy).toHaveBeenLastCalledWith('my-cron-job', expect.anything(), {
schedule: { type: 'crontab', value: '* * * 1,9 0' },
});
done();
},
{ name: 'my-cron-job' },
);
}));

test('throws without supplied name', () => {
const nodeCron: NodeCron = {
Expand All @@ -154,32 +160,33 @@ describe('cron check-ins', () => {
});

describe('node-schedule', () => {
test('calls withMonitor', done => {
expect.assertions(5);

class NodeScheduleMock {
scheduleJob(
nameOrExpression: string | Date | object,
expressionOrCallback: string | Date | object | (() => void),
callback: () => void,
): unknown {
expect(nameOrExpression).toBe('my-cron-job');
expect(expressionOrCallback).toBe('* * * Jan,Sep Sun');
expect(callback).toBeInstanceOf(Function);
return callback();
test('calls withMonitor', () =>
new Promise<void>(done => {
expect.assertions(5);

class NodeScheduleMock {
scheduleJob(
nameOrExpression: string | Date | object,
expressionOrCallback: string | Date | object | (() => void),
callback: () => void,
): unknown {
expect(nameOrExpression).toBe('my-cron-job');
expect(expressionOrCallback).toBe('* * * Jan,Sep Sun');
expect(callback).toBeInstanceOf(Function);
return callback();
}
}
}

const scheduleWithCheckIn = cron.instrumentNodeSchedule(new NodeScheduleMock());
const scheduleWithCheckIn = cron.instrumentNodeSchedule(new NodeScheduleMock());

scheduleWithCheckIn.scheduleJob('my-cron-job', '* * * Jan,Sep Sun', () => {
expect(withMonitorSpy).toHaveBeenCalledTimes(1);
expect(withMonitorSpy).toHaveBeenLastCalledWith('my-cron-job', expect.anything(), {
schedule: { type: 'crontab', value: '* * * 1,9 0' },
scheduleWithCheckIn.scheduleJob('my-cron-job', '* * * Jan,Sep Sun', () => {
expect(withMonitorSpy).toHaveBeenCalledTimes(1);
expect(withMonitorSpy).toHaveBeenLastCalledWith('my-cron-job', expect.anything(), {
schedule: { type: 'crontab', value: '* * * 1,9 0' },
});
done();
});
done();
});
});
}));

test('throws without crontab string', () => {
class NodeScheduleMock {
Expand Down
46 changes: 24 additions & 22 deletions packages/node/test/integration/breadcrumbs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import type { NodeClient } from '../../src/sdk/client';

import { cleanupOtel, mockSdkInit } from '../helpers/mockSdkInit';

import { afterEach, describe, expect, test, vi } from 'vitest';

describe('Integration | breadcrumbs', () => {
const beforeSendTransaction = jest.fn(() => null);
const beforeSendTransaction = vi.fn(() => null);

afterEach(() => {
cleanupOtel();
});

describe('without tracing', () => {
it('correctly adds & retrieves breadcrumbs', async () => {
const beforeSend = jest.fn(() => null);
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);
test('correctly adds & retrieves breadcrumbs', async () => {
const beforeSend = vi.fn(() => null);
const beforeBreadcrumb = vi.fn(breadcrumb => breadcrumb);

mockSdkInit({ beforeSend, beforeBreadcrumb });

Expand Down Expand Up @@ -49,9 +51,9 @@ describe('Integration | breadcrumbs', () => {
);
});

it('handles parallel scopes', async () => {
const beforeSend = jest.fn(() => null);
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);
test('handles parallel scopes', async () => {
const beforeSend = vi.fn(() => null);
const beforeBreadcrumb = vi.fn(breadcrumb => breadcrumb);

mockSdkInit({ beforeSend, beforeBreadcrumb });

Expand Down Expand Up @@ -95,9 +97,9 @@ describe('Integration | breadcrumbs', () => {
});
});

it('correctly adds & retrieves breadcrumbs', async () => {
const beforeSend = jest.fn(() => null);
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);
test('correctly adds & retrieves breadcrumbs', async () => {
const beforeSend = vi.fn(() => null);
const beforeBreadcrumb = vi.fn(breadcrumb => breadcrumb);

mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, enableTracing: true });

Expand Down Expand Up @@ -140,9 +142,9 @@ describe('Integration | breadcrumbs', () => {
);
});

it('correctly adds & retrieves breadcrumbs for the current isolation span only', async () => {
const beforeSend = jest.fn(() => null);
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);
test('correctly adds & retrieves breadcrumbs for the current isolation span only', async () => {
const beforeSend = vi.fn(() => null);
const beforeBreadcrumb = vi.fn(breadcrumb => breadcrumb);

mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, enableTracing: true });

Expand Down Expand Up @@ -192,9 +194,9 @@ describe('Integration | breadcrumbs', () => {
);
});

it('ignores scopes inside of root span', async () => {
const beforeSend = jest.fn(() => null);
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);
test('ignores scopes inside of root span', async () => {
const beforeSend = vi.fn(() => null);
const beforeBreadcrumb = vi.fn(breadcrumb => breadcrumb);

mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, enableTracing: true });

Expand Down Expand Up @@ -233,9 +235,9 @@ describe('Integration | breadcrumbs', () => {
);
});

it('handles deep nesting of scopes', async () => {
const beforeSend = jest.fn(() => null);
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);
test('handles deep nesting of scopes', async () => {
const beforeSend = vi.fn(() => null);
const beforeBreadcrumb = vi.fn(breadcrumb => breadcrumb);

mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, enableTracing: true });

Expand Down Expand Up @@ -291,9 +293,9 @@ describe('Integration | breadcrumbs', () => {
);
});

it('correctly adds & retrieves breadcrumbs in async spans', async () => {
const beforeSend = jest.fn(() => null);
const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);
test('correctly adds & retrieves breadcrumbs in async spans', async () => {
const beforeSend = vi.fn(() => null);
const beforeBreadcrumb = vi.fn(breadcrumb => breadcrumb);

mockSdkInit({ beforeSend, beforeBreadcrumb, beforeSendTransaction, enableTracing: true });

Expand Down
Loading
Loading