Files
twenty/packages/twenty-server/test/integration/rest/suites/front-component-built-js.integration-spec.ts
T
Weiko 96bc3594a3 Serve frontend components (#17798)
## Context
Ability to serve frontend component

See:
```typescript
curl -i 'http://localhost:3000/rest/front-components/35063b3f-bc4c-4358-8966-7762677802a3' \
--header 'Authorization: Bearer eyJhb...'
HTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: application/javascript
Date: Mon, 09 Feb 2026 10:28:17 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Transfer-Encoding: chunked

// react-globals:react/jsx-runtime
var jsx = globalThis.jsx;
var jsxs = globalThis.jsxs;
var Fragment = globalThis.React.Fragment;

// src/front-components/test.tsx
var RemoteComponents = globalThis.RemoteComponents;
var Component = () => {
  return /* @__PURE__ */ jsxs(RemoteComponents.HtmlDiv, { style: { padding: "20px", fontFamily: "sans-serif" }, children: [
    /* @__PURE__ */ jsx(RemoteComponents.HtmlH1, { children: "My new component!" }),
    /* @__PURE__ */ jsx(RemoteComponents.HtmlP, { children: "This is your front component: test" })
  ] });
};
var test_default = globalThis.jsx(Component, {});
export {
  test_default as default
};
//# sourceMappingURL=test.mjs.map
```

readFile_v2 returns a Node.js Stream object (Readable). Here we are
using stream pipeline which connects the readable stream (file) to the
writable stream (HTTP response) which efficiently streams the file
content directly to the HTTP response without loading the entire file
into memory. (in chunks, handling backpressure and closing the
connection when the file is fully sent)
2026-02-09 14:52:14 +00:00

87 lines
2.7 KiB
TypeScript

import { createFrontComponent } from 'test/integration/metadata/suites/front-component/utils/create-front-component.util';
import { deleteFrontComponent } from 'test/integration/metadata/suites/front-component/utils/delete-front-component.util';
import { seedBuiltFrontComponentFile } from 'test/integration/metadata/suites/front-component/utils/seed-built-front-component-file.util';
import { makeRestAPIRequest } from 'test/integration/rest/utils/make-rest-api-request.util';
const BUILT_COMPONENT_PATH = 'src/front-components/test-endpoint.mjs';
describe('Front component built JS endpoint', () => {
let frontComponentId: string;
let cleanupBuiltFile: (() => void) | undefined;
beforeAll(async () => {
const { cleanup } = await seedBuiltFrontComponentFile({
builtComponentPath: BUILT_COMPONENT_PATH,
});
cleanupBuiltFile = cleanup;
const { data } = await createFrontComponent({
expectToFail: false,
input: {
name: 'testBuiltJsEndpoint',
componentName: 'TestBuiltJsEndpoint',
sourceComponentPath: 'src/front-components/test-endpoint.tsx',
builtComponentPath: BUILT_COMPONENT_PATH,
builtComponentChecksum: 'test-checksum-123',
},
});
frontComponentId = data.createFrontComponent.id;
});
afterAll(async () => {
if (frontComponentId) {
await deleteFrontComponent({
expectToFail: false,
input: { id: frontComponentId },
});
}
cleanupBuiltFile?.();
});
it('should serve the built JS file with correct content type', async () => {
await makeRestAPIRequest({
method: 'get',
path: `/front-components/${frontComponentId}`,
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
})
.expect(200)
.expect('Content-Type', /application\/javascript/)
.expect((res) => {
expect(res.text).toBe('dummy built component content');
});
});
it('should return 404 for a non-existent front component ID', async () => {
const nonExistentId = '00000000-0000-0000-0000-000000000000';
await makeRestAPIRequest({
method: 'get',
path: `/front-components/${nonExistentId}`,
bearer: APPLE_JANE_ADMIN_ACCESS_TOKEN,
})
.expect(404)
.expect((res) => {
expect(res.body.statusCode).toBe(404);
});
});
it('should return 403 when no token is provided', async () => {
await makeRestAPIRequest({
method: 'get',
path: `/front-components/${frontComponentId}`,
bearer: '',
}).expect(403);
});
it('should return 401 when an invalid token is provided', async () => {
await makeRestAPIRequest({
method: 'get',
path: `/front-components/${frontComponentId}`,
bearer: INVALID_ACCESS_TOKEN,
}).expect(401);
});
});