Skip to content

Commit 8c2dc4e

Browse files
committed
refactor: extract shared setup in PrinterObserver location tests
Use beforeEach and startTest helper to deduplicate appendSpy, writer, printer, observer creation and testStarted+mockClear boilerplate.
1 parent af0f0e3 commit 8c2dc4e

1 file changed

Lines changed: 20 additions & 38 deletions

File tree

packages/extension/src/Observers/PrinterObserver.test.ts

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -197,22 +197,32 @@ describe.each(writers)('PrinterObserver with $writerName', ({ createWriter }) =>
197197
});
198198

199199
describe('PrinterObserver passes location and testId to writer', () => {
200-
it('testFinished passes file line 1 as location', () => {
201-
const appendSpy = vi.fn();
202-
const writer: OutputWriter = { append: appendSpy, appendLine: () => {} };
203-
const printer = new Printer(new PHPUnitXML(), PRESET_PROGRESS);
204-
const observer = new PrinterObserver(writer, printer);
200+
let appendSpy: ReturnType<typeof vi.fn>;
201+
let observer: PrinterObserver;
202+
203+
beforeEach(() => {
204+
appendSpy = vi.fn();
205+
const writer: OutputWriter = {
206+
append: appendSpy as OutputWriter['append'],
207+
appendLine: () => {},
208+
};
209+
observer = new PrinterObserver(writer, new Printer(new PHPUnitXML(), PRESET_PROGRESS));
210+
});
205211

212+
function startTest(name: string, id: string) {
206213
observer.testStarted({
207214
event: TeamcityEvent.testStarted,
208-
name: 'test_passed',
215+
name,
209216
locationHint: '',
210217
flowId: 1,
211-
id: 'App\\Tests\\MyTest::test_passed',
218+
id,
212219
file: '/app/tests/MyTest.php',
213220
});
214-
215221
appendSpy.mockClear();
222+
}
223+
224+
it('testFinished passes file line 1 as location', () => {
225+
startTest('test_passed', 'App\\Tests\\MyTest::test_passed');
216226

217227
observer.testFinished({
218228
event: TeamcityEvent.testFinished,
@@ -232,21 +242,7 @@ describe('PrinterObserver passes location and testId to writer', () => {
232242
});
233243

234244
it('testFailed passes detail location when available', () => {
235-
const appendSpy = vi.fn();
236-
const writer: OutputWriter = { append: appendSpy, appendLine: () => {} };
237-
const printer = new Printer(new PHPUnitXML(), PRESET_PROGRESS);
238-
const observer = new PrinterObserver(writer, printer);
239-
240-
observer.testStarted({
241-
event: TeamcityEvent.testStarted,
242-
name: 'test_fail',
243-
locationHint: '',
244-
flowId: 1,
245-
id: 'App\\Tests\\MyTest::test_fail',
246-
file: '/app/tests/MyTest.php',
247-
});
248-
249-
appendSpy.mockClear();
245+
startTest('test_fail', 'App\\Tests\\MyTest::test_fail');
250246

251247
observer.testFailed({
252248
event: TeamcityEvent.testFailed,
@@ -271,21 +267,7 @@ describe('PrinterObserver passes location and testId to writer', () => {
271267
});
272268

273269
it('testIgnored passes file line 1 as location', () => {
274-
const appendSpy = vi.fn();
275-
const writer: OutputWriter = { append: appendSpy, appendLine: () => {} };
276-
const printer = new Printer(new PHPUnitXML(), PRESET_PROGRESS);
277-
const observer = new PrinterObserver(writer, printer);
278-
279-
observer.testStarted({
280-
event: TeamcityEvent.testStarted,
281-
name: 'test_skipped',
282-
locationHint: '',
283-
flowId: 1,
284-
id: 'App\\Tests\\MyTest::test_skipped',
285-
file: '/app/tests/MyTest.php',
286-
});
287-
288-
appendSpy.mockClear();
270+
startTest('test_skipped', 'App\\Tests\\MyTest::test_skipped');
289271

290272
observer.testIgnored({
291273
event: TeamcityEvent.testIgnored,

0 commit comments

Comments
 (0)