Skip to content

Commit

Permalink
test(lib-storage): removed redundant done callbacks
Browse files Browse the repository at this point in the history
async-await doesn't work with done callbacks in Jest v27
Refs: jestjs/jest#11404
  • Loading branch information
trivikr committed Oct 5, 2021
1 parent 32c105a commit 8460ac6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 73 deletions.
79 changes: 26 additions & 53 deletions lib/lib-storage/src/Upload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ jest.mock("@aws-sdk/client-s3", () => ({
}));

import { S3 } from "@aws-sdk/client-s3";
import { Upload, Progress } from "./index";
import { Readable } from "stream";

import { Progress, Upload } from "./index";

const DEFAULT_PART_SIZE = 1024 * 1024 * 5;

describe(Upload.name, () => {
Expand All @@ -61,7 +62,7 @@ describe(Upload.name, () => {
Body: "this-is-a-sample-payload",
};

it("correctly exposes the event emitter API", (done) => {
it("correctly exposes the event emitter API", () => {
const upload = new Upload({
params,
client: new S3({}),
Expand All @@ -71,11 +72,9 @@ describe(Upload.name, () => {
expect(upload.eventNames).toBeDefined();
expect(upload.off).toBeDefined();
expect(upload.on).toBeDefined();

done();
});

it("should upload using PUT when empty buffer", async (done) => {
it("should upload using PUT when empty buffer", async () => {
const buffer = Buffer.from("");
const actionParams = { ...params, Body: buffer };
const upload = new Upload({
Expand All @@ -100,11 +99,9 @@ describe(Upload.name, () => {
expect(completeMultipartMock).toHaveBeenCalledTimes(0);
// no tags were passed.
expect(putObjectTaggingMock).toHaveBeenCalledTimes(0);

done();
});

it("should upload using PUT when empty stream", async (done) => {
it("should upload using PUT when empty stream", async () => {
const stream = new Readable({});
stream.push(null);
const actionParams = { ...params, Body: stream };
Expand All @@ -130,11 +127,9 @@ describe(Upload.name, () => {
expect(completeMultipartMock).toHaveBeenCalledTimes(0);
// no tags were passed.
expect(putObjectTaggingMock).toHaveBeenCalledTimes(0);

done();
});

it("should upload using PUT when parts are smaller than one part", async (done) => {
it("should upload using PUT when parts are smaller than one part", async () => {
const upload = new Upload({
params,
client: new S3({}),
Expand All @@ -157,11 +152,9 @@ describe(Upload.name, () => {
expect(completeMultipartMock).toHaveBeenCalledTimes(0);
// no tags were passed.
expect(putObjectTaggingMock).toHaveBeenCalledTimes(0);

done();
});

it("should upload using PUT when parts are smaller than one part stream", async (done) => {
it("should upload using PUT when parts are smaller than one part stream", async () => {
const streamBody = Readable.from(
(function* () {
yield params.Body;
Expand Down Expand Up @@ -189,11 +182,9 @@ describe(Upload.name, () => {
expect(completeMultipartMock).toHaveBeenCalledTimes(0);
// no tags were passed.
expect(putObjectTaggingMock).toHaveBeenCalledTimes(0);

done();
});

it("should upload using multi-part when parts are larger than part size", async (done) => {
it("should upload using multi-part when parts are larger than part size", async () => {
// create a string that's larger than 5MB.
const partSize = 1024 * 1024 * 5;
const largeBuffer = Buffer.from("#".repeat(partSize + 10));
Expand Down Expand Up @@ -255,10 +246,9 @@ describe(Upload.name, () => {
expect(putObjectTaggingMock).toHaveBeenCalledTimes(0);
// put was not called
expect(putObjectMock).toHaveBeenCalledTimes(0);
done();
});

it("should upload using multi-part when parts are larger than part size stream", async (done) => {
it("should upload using multi-part when parts are larger than part size stream", async () => {
// create a string that's larger than 5MB.
const largeBuffer = Buffer.from("#".repeat(DEFAULT_PART_SIZE + 10));
const firstBuffer = largeBuffer.subarray(0, DEFAULT_PART_SIZE);
Expand Down Expand Up @@ -324,10 +314,9 @@ describe(Upload.name, () => {
expect(putObjectTaggingMock).toHaveBeenCalledTimes(0);
// put was not called
expect(putObjectMock).toHaveBeenCalledTimes(0);
done();
});

it("should add tags to the object if tags have been added PUT", async (done) => {
it("should add tags to the object if tags have been added PUT", async () => {
const tags = [
{
Key: "k1",
Expand Down Expand Up @@ -357,11 +346,9 @@ describe(Upload.name, () => {
TagSet: tags,
},
});

done();
});

it("should add tags to the object if tags have been added multi-part", async (done) => {
it("should add tags to the object if tags have been added multi-part", async () => {
const largeBuffer = Buffer.from("#".repeat(DEFAULT_PART_SIZE + 10));
const actionParams = { ...params, Body: largeBuffer };
const tags = [
Expand Down Expand Up @@ -393,44 +380,35 @@ describe(Upload.name, () => {
TagSet: tags,
},
});

done();
});

it("should validate partsize", async (done) => {
it("should validate partsize", () => {
try {
const upload = new Upload({
new Upload({
params,
partSize: 6,
client: new S3({}),
});

//should not get here.
expect(1).toEqual(0);
fail();
} catch (error) {
expect(error).toBeDefined();
done();
}
});

it("should validate queue size", (done) => {
it("should validate queue size", () => {
try {
const upload = new Upload({
new Upload({
params,
queueSize: -1,
client: new S3({}),
});

//should not get here.
expect(1).toEqual(0);
fail();
} catch (error) {
expect(error).toBeDefined();
done();
}
done();
});

it("should provide progress updates", async (done) => {
it("should provide progress updates", async () => {
const upload = new Upload({
params,
client: new S3({}),
Expand All @@ -444,12 +422,11 @@ describe(Upload.name, () => {
part: 1,
total: 24,
});
done();
});
await upload.done();
});

it("should provide progress updates multi-part buffer", async (done) => {
it("should provide progress updates multi-part buffer", async () => {
const partSize = 1024 * 1024 * 5;
const largeBuffer = Buffer.from("#".repeat(partSize + 10));
const firstBuffer = largeBuffer.subarray(0, partSize);
Expand All @@ -459,7 +436,7 @@ describe(Upload.name, () => {
client: new S3({}),
});

let received = [];
const received = [];
upload.on("httpUploadProgress", (progress: Progress) => {
received.push(progress);
});
Expand All @@ -479,10 +456,9 @@ describe(Upload.name, () => {
total: largeBuffer.byteLength,
});
expect(received.length).toBe(2);
done();
});

it("should provide progress updates multi-part stream", async (done) => {
it("should provide progress updates multi-part stream", async () => {
const partSize = 1024 * 1024 * 5;
const largeBuffer = Buffer.from("#".repeat(partSize + 10));
const streamBody = Readable.from(
Expand All @@ -496,7 +472,7 @@ describe(Upload.name, () => {
client: new S3({}),
});

let received = [];
const received = [];
upload.on("httpUploadProgress", (progress: Progress) => {
received.push(progress);
});
Expand All @@ -516,18 +492,17 @@ describe(Upload.name, () => {
total: undefined,
});
expect(received.length).toBe(2);
done();
});

it("should provide progress updates empty buffer", async (done) => {
it("should provide progress updates empty buffer", async () => {
const buffer = Buffer.from("");
const actionParams = { ...params, Body: buffer };
const upload = new Upload({
params: actionParams,
client: new S3({}),
});

let received = [];
const received = [];
upload.on("httpUploadProgress", (progress: Progress) => {
received.push(progress);
});
Expand All @@ -540,18 +515,17 @@ describe(Upload.name, () => {
total: 0,
});
expect(received.length).toBe(1);
done();
});

it("should provide progress updates empty stream", async (done) => {
it("should provide progress updates empty stream", async () => {
const stream = Readable.from((function* () {})());
const actionParams = { ...params, Body: stream };
const upload = new Upload({
params: actionParams,
client: new S3({}),
});

let received = [];
const received = [];
upload.on("httpUploadProgress", (progress: Progress) => {
received.push(progress);
});
Expand All @@ -564,6 +538,5 @@ describe(Upload.name, () => {
total: 0,
});
expect(received.length).toBe(1);
done();
});
});
9 changes: 3 additions & 6 deletions lib/lib-storage/src/chunks/getChunkBuffer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe.only(getChunkBuffer.name, () => {
const getBuffer = (size: number) => Buffer.from("#".repeat(size));

describe("Buffer chunking", () => {
it("should come back with small sub buffers", async (done) => {
it("should come back with small sub buffers", async () => {
const chunklength = 100;
const totalLength = 1000;
const buffer = getBuffer(totalLength);
Expand All @@ -25,10 +25,9 @@ describe.only(getChunkBuffer.name, () => {
}

expect(chunkNum).toEqual(expectedNumberOfChunks);
done();
});

it("should come back with the last chunk the remainder size", async (done) => {
it("should come back with the last chunk the remainder size", async () => {
const chunklength = 1000;
const totalLength = 2200;
const buffer = getBuffer(totalLength);
Expand All @@ -46,10 +45,9 @@ describe.only(getChunkBuffer.name, () => {
expect(chunks[1].lastPart).toBe(undefined);
expect(byteLength(chunks[2].data)).toBe(totalLength % chunklength);
expect(chunks[2].lastPart).toBe(true);
done();
});

it("should come back with one chunk if it is a small buffer", async (done) => {
it("should come back with one chunk if it is a small buffer", async () => {
const chunklength = 1000;
const totalLength = 200;
const buffer = getBuffer(totalLength);
Expand All @@ -63,7 +61,6 @@ describe.only(getChunkBuffer.name, () => {
expect(chunks.length).toEqual(1);
expect(byteLength(chunks[0].data)).toBe(totalLength % chunklength);
expect(chunks[0].lastPart).toBe(true);
done();
});
});
});
12 changes: 5 additions & 7 deletions lib/lib-storage/src/chunks/getDataReadable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Readable } from "stream";

import { byteLength } from "../bytelength";
import { RawDataPart as DataPart } from "../Upload";
import { getChunkStream as chunkFromReadable } from "./getChunkStream";
import { getDataReadable } from "./getDataReadable";
import { RawDataPart as DataPart } from "../Upload";

const fs = require("fs");

Expand Down Expand Up @@ -31,7 +32,7 @@ describe(chunkFromReadable.name, () => {
return chunks;
};

it("should return chunks of a specific size", async (done) => {
it("should return chunks of a specific size", async () => {
const chunks = await getChunks(58, 1, 20);

expect(chunks.length).toBe(3);
Expand All @@ -46,10 +47,9 @@ describe(chunkFromReadable.name, () => {
expect(byteLength(chunks[2].data)).toEqual(18);
expect(chunks[2].partNumber).toEqual(3);
expect(chunks[2].lastPart).toBe(true);
done();
});

it("should properly chunk a file", async (done) => {
it("should properly chunk a file", async () => {
const fileStream = fs.createReadStream(__dirname + "/sample.file");
const chunks: DataPart[] = [];
const chunker = chunkFromReadable<Readable>(fileStream, _6MB, getDataReadable);
Expand All @@ -61,10 +61,9 @@ describe(chunkFromReadable.name, () => {
expect(byteLength(chunks[0].data)).toEqual(byteLength(fileStream));
expect(chunks[0].partNumber).toEqual(1);
expect(chunks[0].lastPart).toBe(true);
done();
});

it("should properly chunk a large stream of unknown size", async (done) => {
it("should properly chunk a large stream of unknown size", async () => {
//should go into 11 chunks. Each with ~6mb and the last with 3mb
const chunks = await getChunks(_6MB / 2, 21, _6MB);

Expand All @@ -75,6 +74,5 @@ describe(chunkFromReadable.name, () => {
}
expect(byteLength(chunks[10].data)).toEqual(_6MB / 2);
expect(chunks[10].lastPart).toBe(true);
done();
});
});
10 changes: 3 additions & 7 deletions lib/lib-storage/src/chunks/getDataReadableStream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,16 @@ describe("chunkFromReadable.name", () => {
return chunks;
};

it("should a single chunk if the stream is smaller than partsize", async (done) => {
it("should a single chunk if the stream is smaller than partsize", async () => {
const chunks = await getChunks(34, 2, 100);

expect(chunks.length).toBe(1);
expect(byteLength(chunks[0].data)).toEqual(68);
expect(chunks[0].partNumber).toEqual(1);
expect(chunks[0].lastPart).toBe(true);

done();
});

it("should return chunks of a specific size", async (done) => {
it("should return chunks of a specific size", async () => {
const chunks = await getChunks(58, 1, 20);

expect(chunks.length).toBe(3);
Expand All @@ -61,10 +59,9 @@ describe("chunkFromReadable.name", () => {
expect(byteLength(chunks[2].data)).toEqual(18);
expect(chunks[2].partNumber).toEqual(3);
expect(chunks[2].lastPart).toBe(true);
done();
});

it("should properly chunk a large stream of unknown size", async (done) => {
it("should properly chunk a large stream of unknown size", async () => {
const chunks = await getChunks(_6MB / 2, 21, _6MB);

expect(chunks.length).toEqual(11);
Expand All @@ -74,6 +71,5 @@ describe("chunkFromReadable.name", () => {
}
expect(byteLength(chunks[10].data)).toEqual(_6MB / 2);
expect(chunks[10].lastPart).toBe(true);
done();
});
});

0 comments on commit 8460ac6

Please sign in to comment.