|
| 1 | +import { Hub, Scope } from '@sentry/hub'; |
| 2 | + |
| 3 | +import { Prisma } from '../../../src/integrations/node/prisma'; |
| 4 | +import { Span } from '../../../src/span'; |
| 5 | + |
| 6 | +type PrismaMiddleware = (params: unknown, next: (params?: unknown) => Promise<unknown>) => Promise<unknown>; |
| 7 | + |
| 8 | +class PrismaClient { |
| 9 | + constructor() { |
| 10 | + this.middleware = undefined; |
| 11 | + } |
| 12 | + |
| 13 | + private middleware?: PrismaMiddleware; |
| 14 | + |
| 15 | + public $use(cb: PrismaMiddleware) { |
| 16 | + this.middleware = cb; |
| 17 | + } |
| 18 | + |
| 19 | + public user = { |
| 20 | + create: () => this.middleware?.({ action: 'create', model: 'user' }, () => Promise.resolve('result')), |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +describe('setupOnce', () => { |
| 25 | + const Client: PrismaClient = new PrismaClient(); |
| 26 | + |
| 27 | + let scope = new Scope(); |
| 28 | + let parentSpan: Span; |
| 29 | + let childSpan: Span; |
| 30 | + |
| 31 | + beforeAll(() => { |
| 32 | + // @ts-ignore |
| 33 | + new Prisma({ client: Client }).setupOnce( |
| 34 | + () => undefined, |
| 35 | + () => new Hub(undefined, scope), |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + scope = new Scope(); |
| 41 | + parentSpan = new Span(); |
| 42 | + childSpan = parentSpan.startChild(); |
| 43 | + jest.spyOn(scope, 'getSpan').mockReturnValueOnce(parentSpan); |
| 44 | + jest.spyOn(parentSpan, 'startChild').mockReturnValueOnce(childSpan); |
| 45 | + jest.spyOn(childSpan, 'finish'); |
| 46 | + }); |
| 47 | + |
| 48 | + it(`should add middleware with $use method correctly`, done => { |
| 49 | + Client.user.create()?.then(res => { |
| 50 | + expect(res).toBe('result'); |
| 51 | + expect(scope.getSpan).toBeCalled(); |
| 52 | + expect(parentSpan.startChild).toBeCalledWith({ |
| 53 | + description: 'user create', |
| 54 | + op: 'db.prisma', |
| 55 | + }); |
| 56 | + expect(childSpan.finish).toBeCalled(); |
| 57 | + done(); |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
0 commit comments