-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathjest.test-management.spec.js
More file actions
3881 lines (3428 loc) · 141 KB
/
Copy pathjest.test-management.spec.js
File metadata and controls
3881 lines (3428 loc) · 141 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict'
const assert = require('node:assert/strict')
const { once } = require('node:events')
const { exec, execSync } = require('child_process')
const path = require('path')
const fs = require('fs')
const { inspect } = require('node:util')
const { assertObjectContains } = require('../helpers')
const {
sandboxCwd,
useSandbox,
getCiVisAgentlessConfig,
getCiVisEvpProxyConfig,
} = require('../helpers')
const { FakeCiVisIntake } = require('../ci-visibility-intake')
const {
TEST_SUITE,
TEST_STATUS,
TEST_SOURCE_FILE,
TEST_IS_NEW,
TEST_IS_RETRY,
TEST_EARLY_FLAKE_ENABLED,
TEST_EARLY_FLAKE_ABORT_REASON,
TEST_NAME,
TEST_RETRY_REASON,
TEST_SESSION_NAME,
DD_TEST_IS_USER_PROVIDED_SERVICE,
TEST_MANAGEMENT_ENABLED,
TEST_MANAGEMENT_IS_DISABLED,
TEST_MANAGEMENT_IS_QUARANTINED,
DD_CAPABILITIES_TEST_IMPACT_ANALYSIS,
DD_CAPABILITIES_EARLY_FLAKE_DETECTION,
DD_CAPABILITIES_AUTO_TEST_RETRIES,
DD_CAPABILITIES_TEST_MANAGEMENT_QUARANTINE,
DD_CAPABILITIES_TEST_MANAGEMENT_DISABLE,
DD_CAPABILITIES_TEST_MANAGEMENT_ATTEMPT_TO_FIX,
DD_CAPABILITIES_FAILED_TEST_REPLAY,
TEST_MANAGEMENT_IS_ATTEMPT_TO_FIX,
TEST_HAS_FAILED_ALL_RETRIES,
TEST_MANAGEMENT_ATTEMPT_TO_FIX_PASSED,
TEST_IS_MODIFIED,
TEST_RETRY_REASON_TYPES,
DD_CAPABILITIES_IMPACTED_TESTS,
TEST_FINAL_STATUS,
GIT_COMMIT_SHA,
GIT_REPOSITORY_URL,
ITR_CORRELATION_ID,
TEST_PARAMETERS,
} = require('../../packages/dd-trace/src/plugins/util/test')
const { TELEMETRY_COVERAGE_UPLOAD } = require('../../packages/dd-trace/src/ci-visibility/telemetry')
const { ERROR_MESSAGE } = require('../../packages/dd-trace/src/constants')
const { DD_MAJOR } = require('../../version')
const { getBabelDependencies } = require('./babel-dependencies')
const runTestsCommand = 'node ./ci-visibility/run-jest.js'
const requestedJestVersion = process.env.JEST_VERSION || 'latest'
const oldestJestVersion = DD_MAJOR >= 6 ? '28.0.0' : '24.8.0'
const JEST_VERSION = requestedJestVersion === 'oldest' ? oldestJestVersion : requestedJestVersion
const onlyLatestIt = JEST_VERSION === 'latest' ? it : it.skip
const shouldInstallJestEnvironmentJsdom = JEST_VERSION === 'latest' || Number(JEST_VERSION.split('.')[0]) >= 28
// TODO: add ESM tests
describe(`jest@${JEST_VERSION} commonJS`, () => {
let receiver
let childProcess
let cwd
useSandbox([
`jest@${JEST_VERSION}`,
`jest-jasmine2@${JEST_VERSION}`,
`babel-jest@${JEST_VERSION}`,
// jest-environment-jsdom is included in older versions of jest
shouldInstallJestEnvironmentJsdom ? `jest-environment-jsdom@${JEST_VERSION}` : '',
// jest-circus is not included in older versions of jest
JEST_VERSION !== 'latest' ? `jest-circus@${JEST_VERSION}` : '',
...getBabelDependencies(JEST_VERSION),
'@happy-dom/jest-environment',
'office-addin-mock',
'winston',
'jest-image-snapshot',
].filter(Boolean), true)
before(function () {
cwd = sandboxCwd()
})
beforeEach(async function () {
receiver = await new FakeCiVisIntake().start()
})
afterEach(async () => {
childProcess.kill()
await receiver.stop()
})
context('lage', () => {
it('uses the Lage package name as the test session name', async () => {
receiver.setInfoResponse({ endpoints: ['/evp_proxy/v4'] })
receiver.setKnownTests({
jest: {
'ci-visibility/test/ci-visibility-test.js': ['ci visibility can report tests'],
},
})
receiver.setSettings({
early_flake_detection: {
enabled: false,
},
known_tests_enabled: true,
})
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const metadataDicts = payloads.flatMap(({ payload }) => payload.metadata)
metadataDicts.forEach(metadata => {
assert.strictEqual(metadata.test_levels[TEST_SESSION_NAME], 'my-lage-package')
})
})
childProcess = exec(
runTestsCommand,
{
cwd,
env: {
...getCiVisEvpProxyConfig(receiver.port),
DD_ENABLE_LAGE_PACKAGE_NAME: 'true',
LAGE_PACKAGE_NAME: 'my-lage-package',
TESTS_TO_RUN: 'test/ci-visibility-test',
},
}
)
const [[exitCode]] = await Promise.all([
once(childProcess, 'exit'),
eventsPromise,
])
assert.strictEqual(exitCode, 0)
})
it('updates the test session name across repeated jest.runCLI calls in the same process', async () => {
receiver.setInfoResponse({ endpoints: ['/evp_proxy/v4'] })
receiver.setKnownTests({
jest: {
'ci-visibility/test/ci-visibility-test.js': ['ci visibility can report tests'],
'ci-visibility/test/ci-visibility-test-2.js': ['ci visibility 2 can report tests 2'],
},
})
receiver.setSettings({
early_flake_detection: {
enabled: false,
},
known_tests_enabled: true,
})
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const metadataDicts = payloads.flatMap(({ payload }) => payload.metadata)
assert.ok(
metadataDicts.some(metadata => metadata.test_levels?.[TEST_SESSION_NAME] === 'my-lage-package-a'),
`Got: ${inspect(metadataDicts)}`
)
assert.ok(
metadataDicts.some(metadata => metadata.test_levels?.[TEST_SESSION_NAME] === 'my-lage-package-b'),
`Got: ${inspect(metadataDicts)}`
)
})
childProcess = exec(
'node ./ci-visibility/run-jest-lage-multi.js',
{
cwd,
env: {
...getCiVisEvpProxyConfig(receiver.port),
DD_ENABLE_LAGE_PACKAGE_NAME: 'true',
LAGE_PACKAGE_NAME: 'my-initial-lage-package',
},
}
)
const [[exitCode]] = await Promise.all([
once(childProcess, 'exit'),
eventsPromise,
])
assert.strictEqual(exitCode, 0)
})
it('clears test optimization policies when a later settings request fails', async () => {
const itrCorrelationId = '4321'
receiver.setInfoResponse({ endpoints: ['/evp_proxy/v4'] })
receiver.setItrCorrelationId(itrCorrelationId)
receiver.setKnownTests({
jest: {
'ci-visibility/test/ci-visibility-test.js': ['ci visibility can report tests'],
'ci-visibility/test/ci-visibility-test-2.js': ['ci visibility 2 can report tests 2'],
},
})
receiver.setSettings({
early_flake_detection: {
enabled: true,
},
flaky_test_retries_enabled: true,
itr_enabled: true,
known_tests_enabled: true,
test_management: {
enabled: true,
attempt_to_fix_retries: 2,
},
tests_skipping: true,
})
receiver.setSettingsResponseStatusCodes([200, 404])
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)
const testSessions = []
const testSuites = []
for (const event of events) {
if (event.type === 'test_session_end') {
testSessions.push(event.content)
} else if (event.type === 'test_suite_end') {
testSuites.push(event.content)
}
}
const [firstSession, secondSession] = testSessions
const [firstSuite, secondSuite] = testSuites
assert.ok(firstSession, inspect(testSessions))
assert.ok(secondSession, inspect(testSessions))
assert.ok(firstSuite, inspect(testSuites))
assert.ok(secondSuite, inspect(testSuites))
assert.strictEqual(firstSession.meta[TEST_EARLY_FLAKE_ENABLED], 'true')
assert.strictEqual(firstSession.meta[TEST_MANAGEMENT_ENABLED], 'true')
assert.strictEqual(firstSuite[ITR_CORRELATION_ID], itrCorrelationId)
assert.ok(!(TEST_EARLY_FLAKE_ENABLED in secondSession.meta), inspect(secondSession.meta))
assert.ok(!(TEST_EARLY_FLAKE_ABORT_REASON in secondSession.meta), inspect(secondSession.meta))
assert.ok(!(TEST_MANAGEMENT_ENABLED in secondSession.meta), inspect(secondSession.meta))
assert.ok(!(ITR_CORRELATION_ID in secondSuite), inspect(secondSuite))
})
childProcess = exec(
'node ./ci-visibility/run-jest-lage-multi.js',
{
cwd,
env: {
...getCiVisEvpProxyConfig(receiver.port),
DD_CIVISIBILITY_FLAKY_RETRY_COUNT: '2',
DD_ENABLE_LAGE_PACKAGE_NAME: 'true',
LAGE_PACKAGE_NAME: 'my-initial-lage-package',
},
}
)
const [[exitCode]] = await Promise.all([
once(childProcess, 'exit'),
eventsPromise,
])
assert.strictEqual(exitCode, 0)
})
})
it('sets _dd.test.is_user_provided_service to true if DD_SERVICE is used', (done) => {
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)
const tests = events.filter(event => event.type === 'test').map(event => event.content)
tests.forEach(test => {
assert.strictEqual(test.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'true')
})
})
childProcess = exec(
runTestsCommand,
{
cwd,
env: {
...getCiVisEvpProxyConfig(receiver.port),
TESTS_TO_RUN: 'test/ci-visibility-test',
DD_SERVICE: 'my-service',
},
}
)
childProcess.on('exit', () => {
eventsPromise.then(() => {
done()
}).catch(done)
})
})
context('test management', () => {
context('attempt to fix', () => {
beforeEach(() => {
receiver.setTestManagementTests({
jest: {
suites: {
'ci-visibility/test-management/test-attempt-to-fix-1.js': {
tests: {
'attempt to fix tests can attempt to fix a test': {
properties: {
attempt_to_fix: true,
},
},
},
},
},
},
})
})
const getTestAssertions = ({
isAttemptToFix,
isParallel,
isQuarantined,
isDisabled,
shouldAlwaysPass,
shouldFailSometimes,
}) =>
receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)
const tests = events.filter(event => event.type === 'test').map(event => event.content)
const testSession = events.find(event => event.type === 'test_session_end').content
if (isAttemptToFix) {
assert.strictEqual(testSession.meta[TEST_MANAGEMENT_ENABLED], 'true')
} else {
assert.ok(!(TEST_MANAGEMENT_ENABLED in testSession.meta))
}
const resourceNames = tests.map(span => span.resource)
assertObjectContains(resourceNames,
[
'ci-visibility/test-management/test-attempt-to-fix-1.js.attempt to fix tests can attempt to fix a test',
]
)
if (isParallel) {
// Parallel mode in jest requires more than a single test suite
// Here we check that the second test suite is actually running,
// so we can be sure that parallel mode is on
const parallelTestName = 'ci-visibility/test-management/test-attempt-to-fix-2.js.' +
'attempt to fix tests 2 can attempt to fix a test'
assertObjectContains(resourceNames, [parallelTestName])
}
const retriedTests = tests.filter(
test => test.meta[TEST_NAME] === 'attempt to fix tests can attempt to fix a test'
)
for (let i = 0; i < retriedTests.length; i++) {
const test = retriedTests[i]
if (!isAttemptToFix) {
assert.ok(!(TEST_MANAGEMENT_IS_ATTEMPT_TO_FIX in test.meta))
assert.ok(!(TEST_IS_RETRY in test.meta))
assert.ok(!(TEST_RETRY_REASON in test.meta))
continue
}
if (isQuarantined) {
assert.strictEqual(test.meta[TEST_MANAGEMENT_IS_QUARANTINED], 'true')
}
if (isDisabled) {
assert.strictEqual(test.meta[TEST_MANAGEMENT_IS_DISABLED], 'true')
}
const isFirstAttempt = i === 0
const isLastAttempt = i === retriedTests.length - 1
assert.strictEqual(test.meta[TEST_MANAGEMENT_IS_ATTEMPT_TO_FIX], 'true')
if (isFirstAttempt) {
assert.ok(!(TEST_IS_RETRY in test.meta))
assert.ok(!(TEST_RETRY_REASON in test.meta))
} else {
assert.strictEqual(test.meta[TEST_IS_RETRY], 'true')
assert.strictEqual(test.meta[TEST_RETRY_REASON], TEST_RETRY_REASON_TYPES.atf)
}
if (isLastAttempt) {
if (shouldAlwaysPass) {
assert.strictEqual(test.meta[TEST_MANAGEMENT_ATTEMPT_TO_FIX_PASSED], 'true')
assert.strictEqual(test.meta[TEST_FINAL_STATUS], 'pass')
} else if (shouldFailSometimes) {
assert.ok(!(TEST_HAS_FAILED_ALL_RETRIES in test.meta))
assert.strictEqual(test.meta[TEST_MANAGEMENT_ATTEMPT_TO_FIX_PASSED], 'false')
assert.strictEqual(test.meta[TEST_FINAL_STATUS], 'fail')
} else {
assert.strictEqual(test.meta[TEST_HAS_FAILED_ALL_RETRIES], 'true')
assert.strictEqual(test.meta[TEST_MANAGEMENT_ATTEMPT_TO_FIX_PASSED], 'false')
assert.strictEqual(test.meta[TEST_FINAL_STATUS], 'fail')
}
} else {
assert.ok(!(TEST_FINAL_STATUS in test.meta))
}
}
})
/**
* @param {() => void} done
* @param {{
* isAttemptToFix?: boolean,
* isQuarantined?: boolean,
* isDisabled?: boolean,
* shouldAlwaysPass?: boolean,
* shouldFailSometimes?: boolean,
* extraEnvVars?: Record<string, string>,
* isParallel?: boolean
* }} [options]
*/
const runAttemptToFixTest = (done, {
isAttemptToFix,
isQuarantined,
isDisabled,
shouldAlwaysPass,
shouldFailSometimes,
extraEnvVars = {},
isParallel = false,
} = {}) => {
let stdout = ''
const testAssertionsPromise = getTestAssertions({
isAttemptToFix,
isParallel,
isQuarantined,
isDisabled,
shouldAlwaysPass,
shouldFailSometimes,
})
childProcess = exec(
runTestsCommand,
{
cwd,
env: {
...getCiVisAgentlessConfig(receiver.port),
TESTS_TO_RUN: 'test-management/test-attempt-to-fix-1',
SHOULD_CHECK_RESULTS: '1',
...(shouldAlwaysPass ? { SHOULD_ALWAYS_PASS: '1' } : {}),
...(shouldFailSometimes ? { SHOULD_FAIL_SOMETIMES: '1' } : {}),
...extraEnvVars,
},
}
)
childProcess.stderr?.on('data', (chunk) => {
stdout += chunk.toString()
})
childProcess.stdout?.on('data', (chunk) => {
stdout += chunk.toString()
})
childProcess.on('exit', exitCode => {
testAssertionsPromise.then(() => {
assert.match(stdout, /I am running when attempt to fix/)
if (isAttemptToFix) {
assert.match(
stdout,
/Datadog Test Optimization: attempting to fix .*attempt to fix tests can attempt to fix a test/
)
assert.strictEqual(
(stdout.match(
/Datadog Test Optimization: attempting to fix .*attempt to fix tests can attempt to fix a test/g
) || []).length,
1
)
assert.match(stdout, /Datadog Test Optimization/)
if (shouldAlwaysPass) {
assert.match(stdout, /Attempt to fix passed: all 4 execution\(s\) passed for 1 test\(s\)\./)
} else {
const numFailedExecutions = shouldFailSometimes ? 2 : 4
assert.match(
stdout,
new RegExp(
`Attempt to fix failed: ${numFailedExecutions} of 4 execution\\(s\\) failed ` +
'across 1 of 1 test\\(s\\)\\.'
)
)
assert.doesNotMatch(stdout, /execution(?:s)? [\d, -]+:/)
}
if (isQuarantined || isDisabled) {
assert.doesNotMatch(stdout, /Errors are suppressed because this test is/)
assert.doesNotMatch(stdout, /test failure\(s\) were ignored/)
}
if (isQuarantined) {
assert.match(
stdout,
/Test was marked as quarantined but was not quarantined because it is attempt to fix\./
)
}
if (isDisabled) {
assert.match(stdout, /Test was marked as disabled but was run because it is attempt to fix\./)
}
}
if (shouldAlwaysPass) {
assert.strictEqual(exitCode, 0)
} else {
assert.strictEqual(exitCode, 1)
}
done()
}).catch(done)
})
}
it('reports skipped and todo attempt to fix tests', async () => {
const testSuite = 'ci-visibility/test-management/test-attempt-to-fix-skip.js'
let testOutput = ''
receiver.setSettings({ test_management: { enabled: true, attempt_to_fix_retries: 3 } })
receiver.setTestManagementTests({
jest: {
suites: {
[testSuite]: {
tests: {
'skipped attempt to fix tests can skip': {
properties: { attempt_to_fix: true },
},
'skipped attempt to fix tests can be todo': {
properties: { attempt_to_fix: true },
},
},
},
},
},
})
childProcess = exec(
runTestsCommand,
{
cwd,
env: {
...getCiVisAgentlessConfig(receiver.port),
TESTS_TO_RUN: 'test-management/test-attempt-to-fix-skip',
},
}
)
childProcess.stdout?.on('data', chunk => { testOutput += chunk.toString() })
childProcess.stderr?.on('data', chunk => { testOutput += chunk.toString() })
const [[exitCode]] = await Promise.all([
once(childProcess, 'exit'),
once(childProcess.stdout, 'end'),
once(childProcess.stderr, 'end'),
])
assert.strictEqual(exitCode, 0)
assert.match(
testOutput,
/Attempt to fix passed: all 2 execution\(s\) passed for 2 test\(s\)\./
)
})
it('can attempt to fix and mark last attempt as failed if every attempt fails', (done) => {
receiver.setSettings({ test_management: { enabled: true, attempt_to_fix_retries: 3 } })
runAttemptToFixTest(done, { isAttemptToFix: true })
})
it('can attempt to fix when a custom environment returns an async add_test result', (done) => {
receiver.setSettings({ test_management: { enabled: true, attempt_to_fix_retries: 3 } })
runAttemptToFixTest(done, {
isAttemptToFix: true,
extraEnvVars: {
CUSTOM_TEST_ENVIRONMENT: './ci-visibility/jestEnvironmentAsyncAddTest.js',
},
})
})
it('can attempt to fix and mark last attempt as passed if every attempt passes', (done) => {
receiver.setSettings({ test_management: { enabled: true, attempt_to_fix_retries: 3 } })
runAttemptToFixTest(done, { isAttemptToFix: true, shouldAlwaysPass: true })
})
it('can attempt to fix and not mark last attempt if attempts both pass and fail', (done) => {
receiver.setSettings({ test_management: { enabled: true, attempt_to_fix_retries: 3 } })
runAttemptToFixTest(done, { isAttemptToFix: true, shouldFailSometimes: true })
})
it('does not attempt to fix tests if test management is not enabled', (done) => {
receiver.setSettings({ test_management: { enabled: false, attempt_to_fix_retries: 3 } })
runAttemptToFixTest(done)
})
it('does not enable attempt to fix tests if DD_TEST_MANAGEMENT_ENABLED is set to false', (done) => {
receiver.setSettings({ test_management: { enabled: true, attempt_to_fix_retries: 3 } })
runAttemptToFixTest(done, { extraEnvVars: { DD_TEST_MANAGEMENT_ENABLED: '0' } })
})
it('attempt to fix takes precedence over ATR', async () => {
receiver.setSettings({
test_management: { enabled: true, attempt_to_fix_retries: 2 },
flaky_test_retries_enabled: true,
})
receiver.setTestManagementTests({
jest: {
suites: {
'ci-visibility/jest-flaky/flaky-fails.js': {
tests: {
'test-flaky-test-retries can retry failed tests': {
properties: {
attempt_to_fix: true,
},
},
},
},
},
},
})
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)
const tests = events.filter(event => event.type === 'test').map(event => event.content)
assert.strictEqual(tests.length, 3)
const atfRetries = tests.filter(t => t.meta[TEST_RETRY_REASON] === TEST_RETRY_REASON_TYPES.atf)
const atrRetries = tests.filter(t => t.meta[TEST_RETRY_REASON] === TEST_RETRY_REASON_TYPES.atr)
assert.strictEqual(atfRetries.length, 2)
assert.strictEqual(atrRetries.length, 0)
})
childProcess = exec(
runTestsCommand,
{
cwd,
env: {
...getCiVisAgentlessConfig(receiver.port),
TESTS_TO_RUN: 'jest-flaky/flaky-fails.js',
},
}
)
await Promise.all([
once(childProcess, 'exit'),
eventsPromise,
])
})
it('preserves test errors when ATR retry suppression is active due to attempt to fix', async () => {
receiver.setSettings({
test_management: { enabled: true, attempt_to_fix_retries: 2 },
flaky_test_retries_enabled: true,
})
receiver.setTestManagementTests({
jest: {
suites: {
'ci-visibility/jest-flaky/flaky-fails.js': {
tests: {
'test-flaky-test-retries can retry failed tests': {
properties: {
attempt_to_fix: true,
},
},
},
},
},
},
})
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)
const tests = events.filter(event => event.type === 'test').map(event => event.content)
const failingTests = tests.filter(test => test.meta[TEST_STATUS] === 'fail')
// Verify that all failing tests have error messages preserved
// even though ATR retry suppression is active (due to attempt to fix)
failingTests.forEach(test => {
assert.ok(
ERROR_MESSAGE in test.meta,
'Test error message should be preserved when ATR retry suppression is active due to attempt to fix'
)
assert.ok(test.meta[ERROR_MESSAGE].length > 0, 'Test error message should not be empty')
// The error should contain information about the assertion failure
assert.match(test.meta[ERROR_MESSAGE], /deepStrictEqual|Expected|actual/i)
})
// Verify attempt to fix is active (ATR should be suppressed)
const atfRetries = tests.filter(t => t.meta[TEST_RETRY_REASON] === TEST_RETRY_REASON_TYPES.atf)
const atrRetries = tests.filter(t => t.meta[TEST_RETRY_REASON] === TEST_RETRY_REASON_TYPES.atr)
assert.strictEqual(atfRetries.length, 2)
assert.strictEqual(atrRetries.length, 0)
})
childProcess = exec(
runTestsCommand,
{
cwd,
env: {
...getCiVisAgentlessConfig(receiver.port),
TESTS_TO_RUN: 'jest-flaky/flaky-fails.js',
},
}
)
await Promise.all([
once(childProcess, 'exit'),
eventsPromise,
])
})
it('attempt to fix takes precedence over EFD for new tests', async () => {
const NUM_RETRIES_EFD = 2
receiver.setKnownTests({ jest: {} })
receiver.setSettings({
test_management: { enabled: true, attempt_to_fix_retries: 2 },
early_flake_detection: {
enabled: true,
slow_test_retries: {
'5s': NUM_RETRIES_EFD,
},
faulty_session_threshold: 100,
},
known_tests_enabled: true,
})
receiver.setTestManagementTests({
jest: {
suites: {
'ci-visibility/jest-flaky/flaky-fails.js': {
tests: {
'test-flaky-test-retries can retry failed tests': {
properties: {
attempt_to_fix: true,
},
},
},
},
},
},
})
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)
const tests = events.filter(event => event.type === 'test').map(event => event.content)
assert.strictEqual(tests.length, 3)
const atfRetries = tests.filter(t => t.meta[TEST_RETRY_REASON] === TEST_RETRY_REASON_TYPES.atf)
const efdRetries = tests.filter(t => t.meta[TEST_RETRY_REASON] === TEST_RETRY_REASON_TYPES.efd)
assert.strictEqual(atfRetries.length, 2)
assert.strictEqual(efdRetries.length, 0)
})
childProcess = exec(
runTestsCommand,
{
cwd,
env: {
...getCiVisAgentlessConfig(receiver.port),
TESTS_TO_RUN: 'jest-flaky/flaky-fails.js',
},
}
)
await Promise.all([
once(childProcess, 'exit'),
eventsPromise,
])
})
it('does not tag known attempt to fix tests as new', async () => {
receiver.setKnownTests({
jest: {
'ci-visibility/jest-flaky/flaky-fails.js': [
'test-flaky-test-retries can retry failed tests',
],
},
})
receiver.setSettings({
test_management: { enabled: true, attempt_to_fix_retries: 2 },
early_flake_detection: {
enabled: true,
slow_test_retries: {
'5s': 2,
},
faulty_session_threshold: 100,
},
known_tests_enabled: true,
})
receiver.setTestManagementTests({
jest: {
suites: {
'ci-visibility/jest-flaky/flaky-fails.js': {
tests: {
'test-flaky-test-retries can retry failed tests': {
properties: {
attempt_to_fix: true,
},
},
},
},
},
},
})
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)
const tests = events.filter(event => event.type === 'test').map(event => event.content)
const atfTests = tests.filter(
t => t.meta[TEST_MANAGEMENT_IS_ATTEMPT_TO_FIX] === 'true'
)
assert.ok(atfTests.length > 0, `Expected ${atfTests.length} > 0`)
for (const test of atfTests) {
assert.ok(
!(TEST_IS_NEW in test.meta),
'ATF test that is in known tests should not be tagged as new'
)
}
})
childProcess = exec(
runTestsCommand,
{
cwd,
env: {
...getCiVisAgentlessConfig(receiver.port),
TESTS_TO_RUN: 'jest-flaky/flaky-fails.js',
},
}
)
await Promise.all([
once(childProcess, 'exit'),
eventsPromise,
])
})
it('does not tag unknown attempt to fix tests as new', async () => {
receiver.setKnownTests({ jest: {} })
receiver.setSettings({
test_management: { enabled: true, attempt_to_fix_retries: 2 },
early_flake_detection: {
enabled: true,
slow_test_retries: {
'5s': 2,
},
faulty_session_threshold: 100,
},
known_tests_enabled: true,
})
receiver.setTestManagementTests({
jest: {
suites: {
'ci-visibility/jest-flaky/flaky-fails.js': {
tests: {
'test-flaky-test-retries can retry failed tests': {
properties: {
attempt_to_fix: true,
},
},
},
},
},
},
})
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)
const tests = events.filter(event => event.type === 'test').map(event => event.content)
assert.strictEqual(tests.length, 3)
for (const test of tests) {
assert.strictEqual(test.meta[TEST_MANAGEMENT_IS_ATTEMPT_TO_FIX], 'true')
assert.ok(
!(TEST_IS_NEW in test.meta),
'attempt to fix takes precedence over early flake detection, so the test is not reported as new'
)
}
})
childProcess = exec(
runTestsCommand,
{
cwd,
env: {
...getCiVisAgentlessConfig(receiver.port),
TESTS_TO_RUN: 'jest-flaky/flaky-fails.js',
},
}
)
await Promise.all([
once(childProcess, 'exit'),
eventsPromise,
])
})
it('resets mock state between attempt to fix retries', async () => {
const NUM_RETRIES = 3
receiver.setSettings({ test_management: { enabled: true, attempt_to_fix_retries: NUM_RETRIES } })
receiver.setTestManagementTests({
jest: {
suites: {
'ci-visibility/test-management/test-attempt-to-fix-with-mock.js': {
tests: {
'attempt to fix tests with mock resets mock state between retries': {
properties: {
attempt_to_fix: true,
},
},
},
},
},
},
})
let stdout = ''
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)
const tests = events.filter(event => event.type === 'test').map(event => event.content)
// Should have 1 original + NUM_RETRIES retry attempts
const mockTests = tests.filter(
test => test.meta[TEST_NAME] === 'attempt to fix tests with mock resets mock state between retries'
)
assert.strictEqual(mockTests.length, NUM_RETRIES + 1)
// All tests should pass because mock state is reset between retries
for (const test of mockTests) {
assert.strictEqual(test.meta[TEST_STATUS], 'pass')
}
// Last attempt should be marked as attempt_to_fix_passed
const lastTest = mockTests[mockTests.length - 1]
assert.strictEqual(lastTest.meta[TEST_MANAGEMENT_ATTEMPT_TO_FIX_PASSED], 'true')
})
childProcess = exec(
runTestsCommand,
{
cwd,
env: {
...getCiVisAgentlessConfig(receiver.port),
TESTS_TO_RUN: 'test-management/test-attempt-to-fix-with-mock',
},
}
)
childProcess.stdout?.on('data', (chunk) => {
stdout += chunk.toString()
})
childProcess.stderr?.on('data', (chunk) => {
stdout += chunk.toString()
})
const [exitCode] = await Promise.all([
once(childProcess, 'exit'),
eventsPromise,
])
// Verify the test actually ran
assert.match(stdout, /I am running attempt to fix with mock/)
// All retries should pass, so exit code should be 0
assert.strictEqual(exitCode[0], 0)
})
onlyLatestIt('preserves concurrent each parameters between attempt to fix retries', async () => {
const NUM_RETRIES = 3
receiver.setSettings({ test_management: { enabled: true, attempt_to_fix_retries: NUM_RETRIES } })
receiver.setTestManagementTests({
jest: {
suites: {
'ci-visibility/test-management/test-concurrent-attempt-to-fix-each.js': {
tests: {
'concurrent attempt to fix each tests parameterized row can pass normally': {
properties: {
attempt_to_fix: true,
},
},
},
},
},
},
})
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)
const tests = events.filter(event => event.type === 'test').map(event => event.content)
const concurrentEachTests = tests.filter(test =>
test.meta[TEST_NAME] === 'concurrent attempt to fix each tests parameterized row can pass normally'
)
assert.strictEqual(concurrentEachTests.length, NUM_RETRIES + 1)