-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathprisma.test.ts
77 lines (62 loc) · 2.17 KB
/
prisma.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* eslint-disable @typescript-eslint/unbound-method */
import { Hub, Scope } from '@sentry/core';
import { logger } from '@sentry/utils';
import { Span } from '../../../src';
import { Prisma } from '../../../src/node/integrations/prisma';
import { getTestClient } from '../../testutils';
type PrismaMiddleware = (params: unknown, next: (params?: unknown) => Promise<unknown>) => Promise<unknown>;
class PrismaClient {
public user: { create: () => Promise<unknown> | undefined } = {
create: () => this._middleware?.({ action: 'create', model: 'user' }, () => Promise.resolve('result')),
};
private _middleware?: PrismaMiddleware;
constructor() {
this._middleware = undefined;
}
public $use(cb: PrismaMiddleware) {
this._middleware = cb;
}
}
describe('setupOnce', function () {
const Client: PrismaClient = new PrismaClient();
let scope = new Scope();
let parentSpan: Span;
let childSpan: Span;
beforeAll(() => {
new Prisma({ client: Client }).setupOnce(
() => undefined,
() => new Hub(undefined, scope),
);
});
beforeEach(() => {
scope = new Scope();
parentSpan = new Span();
childSpan = parentSpan.startChild();
jest.spyOn(scope, 'getSpan').mockReturnValueOnce(parentSpan);
jest.spyOn(parentSpan, 'startChild').mockReturnValueOnce(childSpan);
jest.spyOn(childSpan, 'finish');
});
it('should add middleware with $use method correctly', done => {
void Client.user.create()?.then(res => {
expect(res).toBe('result');
expect(scope.getSpan).toBeCalled();
expect(parentSpan.startChild).toBeCalledWith({
description: 'user create',
op: 'db.sql.prisma',
});
expect(childSpan.finish).toBeCalled();
done();
});
});
it("doesn't attach when using otel instrumenter", () => {
const loggerLogSpy = jest.spyOn(logger, 'log');
const client = getTestClient({ instrumenter: 'otel' });
const hub = new Hub(client);
const integration = new Prisma({ client: Client });
integration.setupOnce(
() => {},
() => hub,
);
expect(loggerLogSpy).toBeCalledWith('Prisma Integration is skipped because of instrumenter configuration.');
});
});