Skip to content

Commit 1ff07d5

Browse files
committed
test: try using only sync fs in test
1 parent cb35507 commit 1ff07d5

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

.changeset/mighty-pugs-tell.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

packages/util-body-length-node/src/calculateBodyLength.spec.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createReadStream, lstatSync, promises, writeFileSync } from "fs";
1+
import * as fs from "fs";
22
import * as os from "os";
33
import * as path from "path";
44

@@ -41,50 +41,48 @@ describe(calculateBodyLength.name, () => {
4141
});
4242

4343
it("should handle a Readable from a file", async () => {
44-
const tmpDir = await promises.mkdtemp(path.join(os.tmpdir(), "test1-"));
44+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "test1-"));
4545
const filePath = path.join(tmpDir, "foo");
46-
writeFileSync(filePath, "foo");
47-
const handle = await promises.open(filePath, "r");
48-
const readStream = createReadStream(filePath, { fd: handle.fd });
46+
fs.writeFileSync(filePath, "foo");
47+
const handle = fs.openSync(filePath, "r");
48+
const readStream = fs.createReadStream(filePath, { fd: handle });
4949
expect(calculateBodyLength(readStream)).toEqual(3);
5050
readStream.destroy();
51-
await promises.unlink(filePath);
52-
await promises.rmdir(tmpDir);
51+
fs.unlinkSync(filePath);
52+
fs.rmdirSync(tmpDir);
5353
});
5454

5555
it("should handle Readable with start end from a file", async () => {
56-
const tmpDir = await promises.mkdtemp(path.join(os.tmpdir(), "test2-"));
56+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "test2-"));
5757
const filePath = path.join(tmpDir, "foo");
58-
writeFileSync(filePath, "foo");
59-
const handle = await promises.open(filePath, "r");
60-
const readStream = createReadStream(filePath, { fd: handle.fd, start: 1, end: 1 });
58+
fs.writeFileSync(filePath, "foo");
59+
const handle = fs.openSync(filePath, "r");
60+
const readStream = fs.createReadStream(filePath, { fd: handle, start: 1, end: 1 });
6161
expect(calculateBodyLength(readStream)).toEqual(1);
6262
readStream.destroy();
63-
await promises.unlink(filePath);
64-
await promises.rmdir(tmpDir);
63+
fs.unlinkSync(filePath);
64+
fs.rmdirSync(tmpDir);
6565
});
6666

6767
describe("fs.ReadStream", () => {
68-
const fileSize = lstatSync(__filename).size;
68+
const fileSize = fs.lstatSync(__filename).size;
6969

7070
describe("should handle stream created using fs.createReadStream", () => {
7171
it("when path is a string", () => {
72-
const fsReadStream = createReadStream(__filename);
72+
const fsReadStream = fs.createReadStream(__filename);
7373
expect(calculateBodyLength(fsReadStream)).toEqual(fileSize);
7474
});
7575

7676
it("when path is a Buffer", () => {
77-
const fsReadStream = createReadStream(Buffer.from(__filename));
77+
const fsReadStream = fs.createReadStream(Buffer.from(__filename));
7878
expect(calculateBodyLength(fsReadStream)).toEqual(fileSize);
7979
});
8080
});
8181

8282
it("should handle stream created using fd.createReadStream", async () => {
83-
const fd = await promises.open(__filename, "r");
84-
if ((fd as any).createReadStream) {
85-
const fdReadStream = (fd as any).createReadStream();
86-
expect(calculateBodyLength(fdReadStream)).toEqual(fileSize);
87-
}
83+
const fd = fs.openSync(__filename, "r");
84+
const fdReadStream = fs.createReadStream("", { fd });
85+
expect(calculateBodyLength(fdReadStream)).toEqual(fileSize);
8886
});
8987
});
9088

0 commit comments

Comments
 (0)