import generate from '@babel/generator' import { ChunkDefinition, ProjectUIDL } from '@teleporthq/teleport-types' import { InlineScriptErrorGuard } from '@teleporthq/teleport-shared' import { createDocumentFileChunks } from '../src/utils' /** * A script in the project's custom code that throws used to cover every page of * the generated app with Next's "Unhandled Runtime Error" overlay. `_document` * now runs the inline script error guard before anything else in , and * only for a project that has scripts of its own. */ const uidlWith = (globals: Record): ProjectUIDL => ({ name: 'site', globals: { settings: { title: 'Site', language: 'en' }, meta: [{ name: 'description', content: 'Site' }], assets: [{ type: 'style', path: 'https://fonts.test/font.css' }], ...globals, }, root: { name: 'App', node: { type: 'element', content: { elementType: 'Router' } } }, } as unknown as ProjectUIDL) const documentSource = (uidl: ProjectUIDL): string => { const chunks = createDocumentFileChunks(uidl, { assets: {} } as never) const chunk = (chunks.js as ChunkDefinition[])[0] return generate(chunk.content as never).code } describe('generated _document — inline script error guard', () => { it('runs before everything else in when the custom code has a script', () => { const source = documentSource( uidlWith({ customCode: { body: '' } }) ) const head = source.slice(source.indexOf('') + ''.length).trimStart() expect(head.startsWith('