Skip to content

Commit 4d3aafb

Browse files
committed
Add tests for isFile with Node.js' native File and Blob
1 parent 597031b commit 4d3aafb

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"root": true,
33
"parser": "@typescript-eslint/parser",
4+
"globals": {
5+
"globalThis": true
6+
},
47
"parserOptions": {
58
"project": "./tsconfig.eslint.json"
69
},

ava.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default {
77
},
88
nodeArguments: [
99
"--no-warnings",
10+
"--experimental-fetch",
1011
"--loader=ts-node/esm/transpile-only"
1112
],
1213
files: [

src/FormDataEncoder.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ export class FormDataEncoder {
271271
*
272272
* @example
273273
*
274+
* ```ts
274275
* import {Readable} from "stream"
275276
*
276277
* import {FormDataEncoder} from "form-data-encoder"
@@ -298,6 +299,7 @@ export class FormDataEncoder {
298299
* const response = await fetch("https://httpbin.org/post", options)
299300
*
300301
* console.log(await response.json())
302+
* ```
301303
*/
302304
* values(): Generator<Uint8Array | FileLike, void, undefined> {
303305
for (const [name, raw] of this.#form) {
@@ -321,6 +323,7 @@ export class FormDataEncoder {
321323
*
322324
* @example
323325
*
326+
* ```ts
324327
* import {Readable} from "stream"
325328
*
326329
* import {FormData, File, fileFromPath} from "formdata-node"
@@ -345,6 +348,7 @@ export class FormDataEncoder {
345348
* const response = await fetch("https://httpbin.org/post", options)
346349
*
347350
* console.log(await response.json())
351+
* ```
348352
*/
349353
async* encode(): AsyncGenerator<Uint8Array, void, undefined> {
350354
for (const part of this.values()) {

src/util/isFile.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {File as NativeFile, Blob as NativeBlob} from "node:buffer"
2+
13
import test from "ava"
24

35
import {File, Blob} from "formdata-node"
@@ -11,6 +13,12 @@ test("Returns true for a File", t => {
1113
t.true(isFile(file))
1214
})
1315

16+
test("Returns true for native File", t => {
17+
const file = new NativeFile(["Content"], "name.txt")
18+
19+
t.true(isFile(file))
20+
})
21+
1422
test("Returns true for a class that implements File", t => {
1523
class MyFile implements FileLike {
1624
name = ""
@@ -72,3 +80,9 @@ test("Returns false for Blob", t => {
7280

7381
t.false(isFile(blob))
7482
})
83+
84+
test("Returns false for native Blob", t => {
85+
const blob = new NativeBlob(["Content"], {type: "text/plain"})
86+
87+
t.false(isFile(blob))
88+
})

src/util/isFile.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,21 @@ import {isFunction} from "./isFunction.js"
1212
*
1313
* This function will return `true` for FileAPI compatible `File` objects:
1414
*
15-
* ```
15+
* ```ts
1616
* import {createReadStream} from "node:fs"
1717
*
1818
* import {isFile} from "form-data-encoder"
1919
*
2020
* isFile(new File(["Content"], "file.txt")) // -> true
2121
* ```
2222
*
23-
* However, if you pass a Node.js `Buffer` or `ReadStream`, it will return `false`:
23+
* However, if you pass a Node.js `Buffer`, or `Blob`, or `ReadStream`, it will return `false`:
2424
*
2525
* ```js
2626
* import {isFile} from "form-data-encoder"
2727
*
2828
* isFile(Buffer.from("Content")) // -> false
29+
* isFile(new Blob(["Content"])) // -> false
2930
* isFile(createReadStream("path/to/a/file.txt")) // -> false
3031
* ```
3132
*/

0 commit comments

Comments
 (0)