This repository was archived by the owner on Jan 28, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathdelete-old-static-assets.test.ts
161 lines (137 loc) · 4.66 KB
/
delete-old-static-assets.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { deleteOldStaticAssets } from "../src/index";
import AWS, {
mockGetObject,
mockGetObjectPromise,
mockListObjectsV2,
mockDeleteObjects
} from "aws-sdk";
// unfortunately can't use __mocks__ because aws-sdk is being mocked in other
// packages in the monorepo
// https://github.com/facebook/jest/issues/2070
jest.mock("aws-sdk", () => require("./aws-sdk.mock"));
const deleteOldAssets = (basePath?: string): Promise<void> => {
return deleteOldStaticAssets({
bucketName: "test-bucket-name",
bucketRegion: "us-west-2",
basePath: basePath || "",
credentials: {
accessKeyId: "fake-access-key",
secretAccessKey: "fake-secret-key",
sessionToken: "fake-session-token"
}
});
};
describe.each`
basePath
${""}
${"/basepath"}
`(
"Delete old static assets tests for basePath: [$basePath]",
({ basePath }) => {
let consoleWarnSpy: jest.SpyInstance;
const basePathPrefix = basePath === "" ? "" : basePath.slice(1) + "/";
beforeEach(() => {
consoleWarnSpy = jest.spyOn(console, "warn").mockReturnValue();
});
afterEach(() => {
consoleWarnSpy.mockRestore();
});
it("does not delete static assets when no BUILD_ID exists", async () => {
mockGetObjectPromise.mockRejectedValue({
code: "NoSuchKey"
});
await deleteOldAssets(basePath);
expect(mockGetObject).toBeCalledTimes(1);
expect(mockGetObject).toBeCalledWith({
Bucket: "test-bucket-name",
Key: basePathPrefix + "BUILD_ID"
});
expect(mockListObjectsV2).toBeCalledTimes(0);
expect(mockDeleteObjects).toBeCalledTimes(0);
});
it("does not delete static assets when BUILD_ID exists but listed objects are empty", async () => {
mockGetObjectPromise.mockResolvedValue({
Body: "test-build-id"
});
mockListObjectsV2.mockImplementation(() => {
return { promise: () => Promise.resolve({ Contents: [] }) };
});
await deleteOldAssets(basePath);
expect(mockListObjectsV2).toBeCalledTimes(2);
expect(mockDeleteObjects).toBeCalledTimes(0);
});
it("deletes static assets when BUILD_ID exists", async () => {
mockGetObjectPromise.mockResolvedValue({
Body: "test-build-id"
});
mockListObjectsV2.mockImplementation((object) => {
if (object.Prefix === basePathPrefix + "static-pages") {
return {
promise: () =>
Promise.resolve({
Contents: [
{
Key: basePathPrefix + "static-pages/prev-build-id/page.html"
},
{
Key: basePathPrefix + "static-pages/test-build-id/page.html"
},
{ Key: basePathPrefix + "static-pages/page.html" } // Backwards compatibility, never delete existing pages in root
]
})
};
} else if (object.Prefix === basePathPrefix + "_next/data") {
return {
promise: () =>
Promise.resolve({
Contents: [
{
Key: basePathPrefix + "_next/data/prev-build-id/page.json"
},
{ Key: basePathPrefix + "_next/data/test-build-id/page.json" }
]
})
};
}
});
await deleteOldAssets(basePath);
expect(AWS.S3).toBeCalledWith({
accessKeyId: "fake-access-key",
endpoint: "https://s3.us-west-2.amazonaws.com",
s3BucketEndpoint: false,
secretAccessKey: "fake-secret-key",
sessionToken: "fake-session-token",
region: "us-west-2"
});
expect(mockDeleteObjects).toBeCalledTimes(2);
expect(mockListObjectsV2).toBeCalledWith({
Bucket: "test-bucket-name",
Prefix: basePathPrefix + "static-pages",
ContinuationToken: undefined
});
expect(mockListObjectsV2).toBeCalledWith({
Bucket: "test-bucket-name",
Prefix: basePathPrefix + "_next/data",
ContinuationToken: undefined
});
expect(mockDeleteObjects).toBeCalledTimes(2);
// Expect only prev-build-id is deleted, not test-build-id which is in BUILD_ID (current build ID in S3)
expect(mockDeleteObjects).toBeCalledWith({
Bucket: "test-bucket-name",
Delete: {
Objects: [
{ Key: basePathPrefix + "static-pages/prev-build-id/page.html" }
]
}
});
expect(mockDeleteObjects).toBeCalledWith({
Bucket: "test-bucket-name",
Delete: {
Objects: [
{ Key: basePathPrefix + "_next/data/prev-build-id/page.json" }
]
}
});
});
}
);