Skip to content

Commit 3592682

Browse files
authored
feat(plugin): make plugin a "no-op" in unsupported browsers (#460)
Previously the plugin would throw an error when it was running in an unsupported browser. This was not great for plugin consumers who run their cypress tests in both supported and unsupported browsers and are fine with only having HAR recording in supported browsers: It forced them to conditionally enable this plugin. To address this, we're changing the plugin to do nothing except `warn` that HAR recording is not supported when running in an unsupported browser. relates-to: #5 (comment)
1 parent a560686 commit 3592682

File tree

2 files changed

+73
-10
lines changed

2 files changed

+73
-10
lines changed

src/Plugin.spec.ts

+54-5
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,17 @@ describe('Plugin', () => {
193193
expect(result).not.toContain(expectedArgs);
194194
});
195195

196-
it('should throw an error if an unsupported browser family is used', () => {
196+
it('should log a warning if an unsupported browser family is used', () => {
197197
// arrange
198198
const args = ['--flag1', '--flag2'];
199199
// act
200-
const act = () => plugin.ensureBrowserFlags(firefox, args);
200+
plugin.ensureBrowserFlags(firefox, args);
201201
// assert
202-
expect(act).toThrowError(
203-
`An unsupported browser family was used: ${firefox.name}`
204-
);
202+
verify(
203+
loggerMock.warn(
204+
`HAR recording is not supported in this browser: ${firefox.name}`
205+
)
206+
).once();
205207
});
206208

207209
it('should throw an error when Electron is used and switches are missed', () => {
@@ -236,6 +238,8 @@ describe('Plugin', () => {
236238
} as SaveOptions;
237239

238240
it('should log an error message when the connection is corrupted', async () => {
241+
// arrange
242+
plugin.ensureBrowserFlags(chrome, []);
239243
// act
240244
await plugin.saveHar(options);
241245
// assert
@@ -400,6 +404,19 @@ describe('Plugin', () => {
400404
verify(harExporterMock.end()).once();
401405
verify(fileManagerMock.removeFile(tempFilePath)).once();
402406
});
407+
408+
it('should be a no-op if an unsupported browser family is used', async () => {
409+
// arrange
410+
plugin.ensureBrowserFlags(firefox, []);
411+
await plugin.recordHar({
412+
rootDir: '/'
413+
});
414+
415+
// act
416+
await plugin.saveHar(options);
417+
// assert
418+
verify(fileManagerMock.createFolder(options.outDir)).never();
419+
});
403420
});
404421

405422
describe('recordHar', () => {
@@ -493,6 +510,26 @@ describe('Plugin', () => {
493510
// assert
494511
verify(harExporterMock.write(request)).never();
495512
});
513+
514+
it('should be a no-op if an unsupported browser family is used', async () => {
515+
const request = new NetworkRequest(
516+
'1',
517+
'https://example.com',
518+
'https://example.com',
519+
'1'
520+
);
521+
when(harExporterFactoryMock.create(anything())).thenResolve(
522+
resolvableInstance(harExporterMock)
523+
);
524+
when(networkObserverMock.subscribe(anyFunction())).thenCall(callback =>
525+
callback(request)
526+
);
527+
plugin.ensureBrowserFlags(firefox, []);
528+
// act
529+
await plugin.recordHar(options);
530+
// assert
531+
verify(harExporterMock.write(request)).never();
532+
});
496533
});
497534

498535
describe('disposeOfHar', () => {
@@ -539,5 +576,17 @@ describe('Plugin', () => {
539576
// assert
540577
verify(networkObserverMock.unsubscribe()).never();
541578
});
579+
580+
it('should be a no-op if an unsupported browser family is used', async () => {
581+
// arrange
582+
plugin.ensureBrowserFlags(firefox, []);
583+
await plugin.recordHar({
584+
rootDir: '/'
585+
});
586+
// act
587+
await plugin.disposeOfHar();
588+
// assert
589+
verify(harExporterMock.end()).never();
590+
});
542591
});
543592
});

src/Plugin.ts

+19-5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export class Plugin {
4444
private networkObservable?: Observer<NetworkRequest>;
4545
private addr?: Addr;
4646
private _connection?: Connection;
47+
private supported?: boolean;
4748

4849
constructor(
4950
private readonly logger: Logger,
@@ -57,9 +58,10 @@ export class Plugin {
5758
browser: Cypress.Browser,
5859
args: string[]
5960
): string[] {
60-
if (!this.isSupportedBrowser(browser)) {
61-
throw new Error(
62-
`An unsupported browser family was used: ${browser.name}`
61+
this.supported = this.isSupportedBrowser(browser);
62+
if (!this.supported) {
63+
this.logger.warn(
64+
`HAR recording is not supported in this browser: ${browser.name}`
6365
);
6466
}
6567

@@ -77,14 +79,18 @@ export class Plugin {
7779
}
7880

7981
public async recordHar(options: RecordOptions): Promise<void> {
80-
await this.closeConnection();
81-
8282
if (!this.addr) {
8383
throw new Error(
8484
`Please call the 'ensureBrowserFlags' before attempting to start the recording.`
8585
);
8686
}
8787

88+
if (!this.supported) {
89+
return;
90+
}
91+
92+
await this.closeConnection();
93+
8894
this.exporter = await this.exporterFactory.create(options);
8995
this._connection = this.connectionFactory.create({
9096
...this.addr,
@@ -99,6 +105,10 @@ export class Plugin {
99105
}
100106

101107
public async saveHar(options: SaveOptions): Promise<void> {
108+
if (!this.supported) {
109+
return;
110+
}
111+
102112
const filePath = join(options.outDir, options.fileName);
103113

104114
if (!this._connection) {
@@ -130,6 +140,10 @@ export class Plugin {
130140
}
131141

132142
public async disposeOfHar(): Promise<void> {
143+
if (!this.supported) {
144+
return;
145+
}
146+
133147
await this.networkObservable?.unsubscribe();
134148
delete this.networkObservable;
135149

0 commit comments

Comments
 (0)