|
6 | 6 | describe, |
7 | 7 | expect, |
8 | 8 | test, |
9 | | - vi |
| 9 | + vi, |
| 10 | + afterEach |
10 | 11 | } from "vitest" |
11 | 12 | import { |
12 | 13 | checkDestructiveChanges, |
@@ -102,43 +103,34 @@ describe("checkDestructiveChanges", () => { |
102 | 103 | }) |
103 | 104 |
|
104 | 105 | describe("checkDestructiveChangeSet", () => { |
| 106 | + const logSpy = vi.spyOn(console, "log").mockImplementation(() => undefined) |
| 107 | + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => undefined) |
| 108 | + afterEach(() => { |
| 109 | + logSpy.mockReset() |
| 110 | + errorSpy.mockReset() |
| 111 | + }) |
| 112 | + |
105 | 113 | test("logs success when no destructive changes are present", async () => { |
106 | 114 | mockCloudFormationSend.mockResolvedValueOnce(safeChangeSet) |
107 | 115 |
|
108 | | - const logSpy = vi.spyOn(console, "log").mockImplementation(() => undefined) |
109 | | - const errorSpy = vi.spyOn(console, "error").mockImplementation(() => undefined) |
110 | | - |
111 | | - try { |
112 | | - await expect(checkDestructiveChangeSet("cs", "stack", "eu-west-2")).resolves.toBeUndefined() |
| 116 | + await expect(checkDestructiveChangeSet("cs", "stack", "eu-west-2")).resolves.toBeUndefined() |
113 | 117 |
|
114 | | - expect(mockCloudFormationSend).toHaveBeenCalledTimes(1) |
115 | | - const command = mockCloudFormationSend.mock.calls[0][0] as { input: { ChangeSetName: string; StackName: string } } |
116 | | - expect(command.input).toEqual({ChangeSetName: "cs", StackName: "stack"}) |
117 | | - expect(logSpy).toHaveBeenCalledWith("Change set cs for stack stack has no destructive changes.") |
118 | | - expect(errorSpy).not.toHaveBeenCalled() |
119 | | - } finally { |
120 | | - logSpy.mockRestore() |
121 | | - errorSpy.mockRestore() |
122 | | - } |
| 118 | + expect(mockCloudFormationSend).toHaveBeenCalledTimes(1) |
| 119 | + const command = mockCloudFormationSend.mock.calls[0][0] as { input: { ChangeSetName: string; StackName: string } } |
| 120 | + expect(command.input).toEqual({ChangeSetName: "cs", StackName: "stack"}) |
| 121 | + expect(logSpy).toHaveBeenCalledWith("Change set cs for stack stack has no destructive changes.") |
| 122 | + expect(errorSpy).not.toHaveBeenCalled() |
123 | 123 | }) |
124 | 124 |
|
125 | 125 | test("logs details and throws when destructive changes exist", async () => { |
126 | 126 | mockCloudFormationSend.mockResolvedValueOnce(destructiveChangeSet) |
127 | 127 |
|
128 | | - const logSpy = vi.spyOn(console, "log").mockImplementation(() => undefined) |
129 | | - const errorSpy = vi.spyOn(console, "error").mockImplementation(() => undefined) |
130 | | - |
131 | | - try { |
132 | | - await expect(checkDestructiveChangeSet("cs", "stack", "eu-west-2")) |
133 | | - .rejects.toThrow("Change set cs contains destructive changes") |
| 128 | + await expect(checkDestructiveChangeSet("cs", "stack", "eu-west-2")) |
| 129 | + .rejects.toThrow("Change set cs contains destructive changes") |
134 | 130 |
|
135 | | - expect(mockCloudFormationSend).toHaveBeenCalledTimes(1) |
136 | | - expect(logSpy).not.toHaveBeenCalled() |
137 | | - expect(errorSpy).toHaveBeenCalledWith("Resources that require attention:") |
138 | | - } finally { |
139 | | - logSpy.mockRestore() |
140 | | - errorSpy.mockRestore() |
141 | | - } |
| 131 | + expect(mockCloudFormationSend).toHaveBeenCalledTimes(1) |
| 132 | + expect(logSpy).not.toHaveBeenCalled() |
| 133 | + expect(errorSpy).toHaveBeenCalledWith("Resources that require attention:") |
142 | 134 | }) |
143 | 135 |
|
144 | 136 | test("allows matching destructive changes when waiver is active", async () => { |
@@ -169,21 +161,13 @@ describe("checkDestructiveChangeSet", () => { |
169 | 161 | } |
170 | 162 | ] |
171 | 163 |
|
172 | | - const logSpy = vi.spyOn(console, "log").mockImplementation(() => undefined) |
173 | | - const errorSpy = vi.spyOn(console, "error").mockImplementation(() => undefined) |
174 | | - |
175 | | - try { |
176 | | - await expect(checkDestructiveChangeSet("cs", "stack", "eu-west-2", allowedChanges)) |
177 | | - .resolves.toBeUndefined() |
| 164 | + await expect(checkDestructiveChangeSet("cs", "stack", "eu-west-2", allowedChanges)) |
| 165 | + .resolves.toBeUndefined() |
178 | 166 |
|
179 | | - expect(mockCloudFormationSend).toHaveBeenCalledTimes(1) |
180 | | - expect(logSpy).toHaveBeenCalledWith(expect.stringContaining("Allowing destructive change ResourceToRemove")) |
181 | | - expect(logSpy).toHaveBeenCalledWith("Change set cs for stack stack has no destructive changes.") |
182 | | - expect(errorSpy).not.toHaveBeenCalled() |
183 | | - } finally { |
184 | | - logSpy.mockRestore() |
185 | | - errorSpy.mockRestore() |
186 | | - } |
| 167 | + expect(mockCloudFormationSend).toHaveBeenCalledTimes(1) |
| 168 | + expect(logSpy).toHaveBeenCalledWith(expect.stringContaining("Allowing destructive change ResourceToRemove")) |
| 169 | + expect(logSpy).toHaveBeenCalledWith("Change set cs for stack stack has no destructive changes.") |
| 170 | + expect(errorSpy).not.toHaveBeenCalled() |
187 | 171 | }) |
188 | 172 |
|
189 | 173 | test("throws when waiver expired before change set creation", async () => { |
@@ -214,19 +198,11 @@ describe("checkDestructiveChangeSet", () => { |
214 | 198 | } |
215 | 199 | ] |
216 | 200 |
|
217 | | - const logSpy = vi.spyOn(console, "log").mockImplementation(() => undefined) |
218 | | - const errorSpy = vi.spyOn(console, "error").mockImplementation(() => undefined) |
| 201 | + await expect(checkDestructiveChangeSet("cs", "stack", "eu-west-2", allowedChanges)) |
| 202 | + .rejects.toThrow("Change set cs contains destructive changes") |
219 | 203 |
|
220 | | - try { |
221 | | - await expect(checkDestructiveChangeSet("cs", "stack", "eu-west-2", allowedChanges)) |
222 | | - .rejects.toThrow("Change set cs contains destructive changes") |
223 | | - |
224 | | - expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("Waiver for ResourceToRemove")) |
225 | | - expect(errorSpy).toHaveBeenCalledWith("Resources that require attention:") |
226 | | - expect(logSpy).not.toHaveBeenCalledWith("Change set cs for stack stack has no destructive changes.") |
227 | | - } finally { |
228 | | - logSpy.mockRestore() |
229 | | - errorSpy.mockRestore() |
230 | | - } |
| 204 | + expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("Waiver for ResourceToRemove")) |
| 205 | + expect(errorSpy).toHaveBeenCalledWith("Resources that require attention:") |
| 206 | + expect(logSpy).not.toHaveBeenCalledWith("Change set cs for stack stack has no destructive changes.") |
231 | 207 | }) |
232 | 208 | }) |
0 commit comments