Skip to content

Commit 33bb5cd

Browse files
authored
test: try using only sync fs in test (#1139)
1 parent cb35507 commit 33bb5cd

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

.changeset/mighty-pugs-tell.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

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

+20-17
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,49 +41,52 @@ 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);
74+
fsReadStream.close();
7475
});
7576

7677
it("when path is a Buffer", () => {
77-
const fsReadStream = createReadStream(Buffer.from(__filename));
78+
const fsReadStream = fs.createReadStream(Buffer.from(__filename));
7879
expect(calculateBodyLength(fsReadStream)).toEqual(fileSize);
80+
fsReadStream.close();
7981
});
8082
});
8183

8284
it("should handle stream created using fd.createReadStream", async () => {
83-
const fd = await promises.open(__filename, "r");
85+
const fd = await fs.promises.open(__filename, "r");
8486
if ((fd as any).createReadStream) {
8587
const fdReadStream = (fd as any).createReadStream();
8688
expect(calculateBodyLength(fdReadStream)).toEqual(fileSize);
89+
fdReadStream.close();
8790
}
8891
});
8992
});

0 commit comments

Comments
 (0)