cloudflare/cloudflare-typescript

Public

mirrored fromhttps://github.com/cloudflare/cloudflare-typescriptAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v7

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/uploads.test.ts

106lines · modecode

1import fs from 'fs';
2import type { ResponseLike } from 'cloudflare/internal/to-file';
3import { toFile } from 'cloudflare/core/uploads';
4
5class MyClass {
6 name: string = 'foo';
7}
8
9function mockResponse({ url, content }: { url: string; content?: Blob }): ResponseLike {
10 return {
11 url,
12 blob: async () => content || new Blob([]),
13 };
14}
15
16describe('toFile', () => {
17 it('throws a helpful error for mismatched types', async () => {
18 await expect(
19 // @ts-expect-error intentionally mismatched type
20 toFile({ foo: 'string' }),
21 ).rejects.toThrowErrorMatchingInlineSnapshot(
22 `"Unexpected data type: object; constructor: Object; props: ["foo"]"`,
23 );
24
25 await expect(
26 // @ts-expect-error intentionally mismatched type
27 toFile(new MyClass()),
28 ).rejects.toThrowErrorMatchingInlineSnapshot(
29 `"Unexpected data type: object; constructor: MyClass; props: ["name"]"`,
30 );
31 });
32
33 it('disallows string at the type-level', async () => {
34 // @ts-expect-error we intentionally do not type support for `string`
35 // to help people avoid passing a file path
36 const file = await toFile('contents');
37 expect(file.text()).resolves.toEqual('contents');
38 });
39
40 it('extracts a file name from a Response', async () => {
41 const response = mockResponse({ url: 'https://example.com/my/audio.mp3' });
42 const file = await toFile(response);
43 expect(file.name).toEqual('audio.mp3');
44 });
45
46 it('extracts a file name from a File', async () => {
47 const input = new File(['foo'], 'input.jsonl');
48 const file = await toFile(input);
49 expect(file.name).toEqual('input.jsonl');
50 });
51
52 it('extracts a file name from a ReadStream', async () => {
53 const input = fs.createReadStream('tests/uploads.test.ts');
54 const file = await toFile(input);
55 expect(file.name).toEqual('uploads.test.ts');
56 });
57
58 it('does not copy File objects', async () => {
59 const input = new File(['foo'], 'input.jsonl', { type: 'jsonl' });
60 const file = await toFile(input);
61 expect(file).toBe(input);
62 expect(file.name).toEqual('input.jsonl');
63 expect(file.type).toBe('jsonl');
64 });
65
66 it('is assignable to File and Blob', async () => {
67 const input = new File(['foo'], 'input.jsonl', { type: 'jsonl' });
68 const result = await toFile(input);
69 const file: File = result;
70 const blob: Blob = result;
71 void file, blob;
72 });
73});
74
75describe('missing File error message', () => {
76 let prevGlobalFile: unknown;
77 let prevNodeFile: unknown;
78 beforeEach(() => {
79 // The file shim captures the global File object when it's first imported.
80 // Reset modules before each test so we can test the error thrown when it's undefined.
81 jest.resetModules();
82 const buffer = require('node:buffer');
83 // @ts-ignore
84 prevGlobalFile = globalThis.File;
85 prevNodeFile = buffer.File;
86 // @ts-ignore
87 globalThis.File = undefined;
88 buffer.File = undefined;
89 });
90 afterEach(() => {
91 // Clean up
92 // @ts-ignore
93 globalThis.File = prevGlobalFile;
94 require('node:buffer').File = prevNodeFile;
95 jest.resetModules();
96 });
97
98 test('is thrown', async () => {
99 const uploads = await import('cloudflare/core/uploads');
100 await expect(
101 uploads.toFile(mockResponse({ url: 'https://example.com/my/audio.mp3' })),
102 ).rejects.toMatchInlineSnapshot(
103 `[Error: \`File\` is not defined as a global, which is required for file uploads.]`,
104 );
105 });
106});