-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathartifacts.test.ts
More file actions
59 lines (45 loc) · 2.08 KB
/
artifacts.test.ts
File metadata and controls
59 lines (45 loc) · 2.08 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
import * as fs from 'fs';
import * as path from 'path';
import { attachFile } from '@currents/jest';
const artifactsDir = path.join(__dirname, '..', '..', 'artifacts');
if (!fs.existsSync(artifactsDir)) {
fs.mkdirSync(artifactsDir, { recursive: true });
}
describe('Artifacts Test', () => {
it('should upload spec, test, and attempt level artifacts', () => {
// Prepare artifact files
const specArtifact = path.join(artifactsDir, 'spec-artifact.txt');
fs.writeFileSync(specArtifact, 'Spec level artifact content\n', 'utf8');
const testArtifact = path.join(artifactsDir, 'test-artifact.txt');
fs.writeFileSync(testArtifact, 'Test level artifact content\n', 'utf8');
const attemptArtifact = path.join(artifactsDir, 'attempt-artifact.txt');
fs.writeFileSync(attemptArtifact, 'Attempt level artifact content\n', 'utf8');
attachFile(specArtifact, undefined, 'spec');
attachFile(testArtifact, undefined, 'test');
attachFile(attemptArtifact);
expect(true).toBe(true);
});
// Jest retry support
// Jest.retryTimes(n) allows retrying failed tests.
// We can simulate failure on first attempt and success on second.
// We need to persist state between retries. Since Jest runs tests in isolation, we use a file.
it('generates artifacts for multiple attempts', () => {
const artifactsDir = path.join(__dirname, '..', '..', 'artifacts');
const attemptFile = path.join(artifactsDir, 'jest-attempt-count.txt');
let attempt = 0;
if (fs.existsSync(attemptFile)) {
attempt = parseInt(fs.readFileSync(attemptFile, 'utf8'), 10);
}
attempt++;
fs.writeFileSync(attemptFile, attempt.toString(), 'utf8');
const artifactPath = path.join(artifactsDir, `jest-attempt-${attempt}.txt`);
fs.writeFileSync(artifactPath, `Jest Artifact for attempt ${attempt}`, 'utf8');
// Attach artifact using helper (default level is attempt)
attachFile(artifactPath);
if (attempt < 2) {
throw new Error('Simulated Jest failure for attempt ' + attempt);
}
expect(true).toBe(true);
});
});
jest.retryTimes(2);