Skip to content

Commit

Permalink
Add tests for FormDataLike and FileLike compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
octet-stream committed Oct 24, 2023
1 parent 29a58a0 commit b0cb2bd
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 12 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,18 @@
"eslint-plugin-import": "2.27.5",
"eslint-plugin-jsx-a11y": "6.7.1",
"eslint-plugin-react": "7.32.2",
"formdata-node": "5.0.0",
"formdata-node": "6.0.2",
"husky": "8.0.3",
"lint-staged": "13.2.2",
"node-fetch": "^3.3.2",
"pinst": "3.0.0",
"sinon": "15.0.4",
"ts-expect": "^1.3.0",
"ts-node": "10.9.1",
"tsup": "7.2.0",
"ttypescript": "1.5.15",
"typescript": "5.0.4",
"undici": "^5.26.5",
"web-streams-polyfill": "4.0.0-beta.3"
}
}
70 changes: 63 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions src/FileLike.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import test from "ava"

import {expectType} from "ts-expect"
import {File as FormDataNodeFile} from "formdata-node"
import {File as NodeFetchFile} from "node-fetch"
import {File as UndiciFile} from "undici"

import type {FileLike} from "./FileLike.js"

test("File is assignable to FileLike", t => {
expectType<FileLike>(new File([], "test.txt"))

t.pass()
})

test("File from formdata-node is assignable to FileLike", t => {
expectType<FileLike>(new FormDataNodeFile([], "test.txt"))

t.pass()
})

test("File from undici is assignable to FileLike", t => {
expectType<FileLike>(new UndiciFile([], "test.txt"))

t.pass()
})

test("File from node-fetch is assignable to FileLike", t => {
expectType<FileLike>(new NodeFetchFile([], "test.txt"))

t.pass()
})
4 changes: 2 additions & 2 deletions src/FileLike.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface FileLike {
/**
* Returns a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) which upon reading returns the data contained within the [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File).
*/
stream(): AsyncIterable<Uint8Array>
stream(): ReadableStream<Uint8Array> | AsyncIterable<Uint8Array>

readonly [Symbol.toStringTag]: string
readonly [Symbol.toStringTag]?: string
}
32 changes: 32 additions & 0 deletions src/FormDataLike.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import test from "ava"

import {expectType} from "ts-expect"
import {FormData as FormDataNode} from "formdata-node"
import {FormData as NodeFetchFormData} from "node-fetch"
import {FormData as UndiciFormData} from "undici"

import type {FormDataLike} from "./FormDataLike.js"

test("FormData is assignable to FormDataLike", t => {
expectType<FormDataLike>(new FormData())

t.pass()
})

test("FormData from formdata-node is assignable to FormDataLike", t => {
expectType<FormDataLike>(new FormDataNode())

t.pass()
})

test("FormData from undici is assignable to FormDataLike", t => {
expectType<FormDataLike>(new UndiciFormData())

t.pass()
})

test("FormData from node-fetch is assignable to FormDataLike", t => {
expectType<FormDataLike>(new NodeFetchFormData())

t.pass()
})
2 changes: 1 addition & 1 deletion src/FormDataLike.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ export interface FormDataLike {
*/
[Symbol.iterator](): IterableIterator<[string, FormDataEntryValue]>

readonly [Symbol.toStringTag]: string
readonly [Symbol.toStringTag]?: string
}
2 changes: 1 addition & 1 deletion src/util/isFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test("Returns true for a class that implements File", t => {

lastModified = Date.now()

async* stream(): AsyncGenerator<Uint8Array> {
async* stream(): AsyncIterable<Uint8Array> {
yield new Uint8Array(0)
}

Expand Down

0 comments on commit b0cb2bd

Please sign in to comment.