-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathdetailed-collector.ts
152 lines (130 loc) · 3.45 KB
/
detailed-collector.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
import {Socket} from 'net';
import {platform} from 'os';
import * as path from 'path';
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Cypress {
interface Chainable {
logDetail: typeof logDetail;
logAction: typeof logAction;
}
}
}
export interface Message {
type: string;
content: string;
}
export interface DetailedCollector {
beginScope(title: string): void;
endScope(): void;
expectation(message: string): void;
action(message: string): void;
}
class DetailedCollectorImpl implements DetailedCollector {
static _instance: DetailedCollector;
private _isWindows: boolean;
constructor() {
this._isWindows = platform() === 'win32';
}
action(message: string): void {
this.sendToReporter({
type: 'action',
content: message,
});
}
beginScope(title: string) {
this.sendToReporter({
type: 'scope:begin',
content: title,
});
}
endScope() {
this.sendToReporter({
type: 'scope:end',
content: '',
});
}
expectation(message: string) {
this.sendToReporter({
type: 'expectation',
content: message,
});
}
sendToReporter(data: Message) {
const client = new Socket();
client.connect(this.getServerPath(), () => {
client.write(JSON.stringify(data));
client.destroy();
});
}
private getServerPath() {
// on Windows, the server path must be a named pipe.
// on other platforms, a file path is used to open a local socket.
return this._isWindows
? '\\\\.\\pipe\\detailed-reporter'
: path.join(process.cwd(), 'ipc.sock');
}
static getInstance() {
if (!DetailedCollectorImpl._instance) {
DetailedCollectorImpl._instance = new DetailedCollectorImpl();
}
return DetailedCollectorImpl._instance;
}
}
class VoidDetailedCollector implements DetailedCollector {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
beginScope(title: string) {}
endScope() {}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
expectation(message: string) {}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
action(message: string) {}
}
export function getCollector(enable: boolean): DetailedCollector {
return enable
? DetailedCollectorImpl.getInstance()
: new VoidDetailedCollector();
}
export function registerDetailedReporterPlugin(
on: Cypress.PluginEvents,
config: Cypress.PluginConfigOptions
) {
const enable = path.basename(config.reporter) === 'detailed-reporter.js';
const collector = getCollector(enable);
on('task', {
detailedScopeBegin(title) {
collector.beginScope(title);
return null;
},
detailedScopeEnd() {
collector.endScope();
return null;
},
detailedExpectation(message: string) {
collector.expectation(message);
return null;
},
detailedAction(message: string) {
collector.action(message);
return null;
},
});
}
export function logDetail(message: string) {
cy.task('detailedExpectation', message);
}
export function logAction(message: string) {
cy.task('detailedAction', message);
}
export function registerDetailedReporterCommands() {
Cypress.Commands.add('logDetail', logDetail);
Cypress.Commands.add('logAction', logAction);
}
export function scope(title: string, delegate: () => void): void {
try {
cy.task('detailedScopeBegin', title);
delegate();
} finally {
cy.task('detailedScopeEnd');
}
}