|
| 1 | +// @ts-expect-error - magicast is ESM and TS complains about that. It works though |
| 2 | +import { parseModule } from 'magicast'; |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | +import { instrumentHandleError } from '../../src/react-router/codemods/handle-error'; |
| 5 | + |
| 6 | +describe('React Router Handle Error Codemod', () => { |
| 7 | + describe('instrumentHandleError', () => { |
| 8 | + it('should add Sentry handle request and error functions to empty server entry', () => { |
| 9 | + const entryServerAst = parseModule(''); |
| 10 | + |
| 11 | + instrumentHandleError(entryServerAst); |
| 12 | + |
| 13 | + const result = entryServerAst.generate().code; |
| 14 | + |
| 15 | + expect(result).toContain( |
| 16 | + 'import { createReadableStreamFromReadable,} from "@react-router/node"', |
| 17 | + ); |
| 18 | + expect(result).toContain( |
| 19 | + 'import { renderToPipeableStream,} from "react-dom/server"', |
| 20 | + ); |
| 21 | + expect(result).toContain('import { ServerRouter,} from "react-router"'); |
| 22 | + expect(result).toContain( |
| 23 | + 'const handleRequest = Sentry.createSentryHandleRequest', |
| 24 | + ); |
| 25 | + expect(result).toContain( |
| 26 | + 'export const handleError = Sentry.createSentryHandleError', |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should add Sentry functions to server entry with existing imports', () => { |
| 31 | + const entryServerAst = parseModule(` |
| 32 | + import { createRequestHandler } from '@react-router/node'; |
| 33 | + import express from 'express'; |
| 34 | +
|
| 35 | + const app = express(); |
| 36 | + `); |
| 37 | + |
| 38 | + instrumentHandleError(entryServerAst); |
| 39 | + |
| 40 | + const result = entryServerAst.generate().code; |
| 41 | + |
| 42 | + expect(result).toContain( |
| 43 | + 'import {createRequestHandler, createReadableStreamFromReadable} from \'@react-router/node\'', |
| 44 | + ); |
| 45 | + expect(result).toContain('import express from \'express\''); |
| 46 | + expect(result).toContain( |
| 47 | + 'import {renderToPipeableStream} from \'react-dom/server\'', |
| 48 | + ); |
| 49 | + expect(result).toContain('import {ServerRouter} from \'react-router\''); |
| 50 | + expect(result).toContain( |
| 51 | + 'const handleRequest = Sentry.createSentryHandleRequest', |
| 52 | + ); |
| 53 | + expect(result).toContain( |
| 54 | + 'export const handleError = Sentry.createSentryHandleError', |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should replace existing default export with handleRequest', () => { |
| 59 | + const entryServerAst = parseModule(` |
| 60 | + import { createRequestHandler } from '@react-router/node'; |
| 61 | +
|
| 62 | + const handler = createRequestHandler({ |
| 63 | + build: require('./build'), |
| 64 | + }); |
| 65 | +
|
| 66 | + export default handler; |
| 67 | + `); |
| 68 | + |
| 69 | + instrumentHandleError(entryServerAst); |
| 70 | + |
| 71 | + const result = entryServerAst.generate().code; |
| 72 | + |
| 73 | + expect(result).toContain('export default handleRequest'); |
| 74 | + expect(result).not.toContain('export default handler'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should handle server entry with function default export', () => { |
| 78 | + const entryServerAst = parseModule(` |
| 79 | + import { createRequestHandler } from '@react-router/node'; |
| 80 | +
|
| 81 | + export default function handler(request, response) { |
| 82 | + return createRequestHandler({ |
| 83 | + build: require('./build'), |
| 84 | + })(request, response); |
| 85 | + } |
| 86 | + `); |
| 87 | + |
| 88 | + instrumentHandleError(entryServerAst); |
| 89 | + |
| 90 | + const result = entryServerAst.generate().code; |
| 91 | + |
| 92 | + expect(result).toContain( |
| 93 | + 'const handleRequest = Sentry.createSentryHandleRequest', |
| 94 | + ); |
| 95 | + expect(result).toContain( |
| 96 | + 'export const handleError = Sentry.createSentryHandleError', |
| 97 | + ); |
| 98 | + expect(result).toContain('export default handleRequest'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should add proper Sentry handle request configuration', () => { |
| 102 | + const entryServerAst = parseModule(''); |
| 103 | + |
| 104 | + instrumentHandleError(entryServerAst); |
| 105 | + |
| 106 | + const result = entryServerAst.generate().code; |
| 107 | + |
| 108 | + expect(result).toMatchSnapshot(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should add proper Sentry handle error configuration', () => { |
| 112 | + const entryServerAst = parseModule(''); |
| 113 | + |
| 114 | + instrumentHandleError(entryServerAst); |
| 115 | + |
| 116 | + const result = entryServerAst.generate().code; |
| 117 | + |
| 118 | + expect(result).toContain( |
| 119 | + 'export const handleError = Sentry.createSentryHandleError({', |
| 120 | + ); |
| 121 | + expect(result).toContain('logErrors: false'); |
| 122 | + }); |
| 123 | + }); |
| 124 | +}); |
0 commit comments