Skip to content

Commit 22e6cdf

Browse files
committed
fix: make plugin a "no-op" in unsupported browsers
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.
1 parent a436d67 commit 22e6cdf

File tree

2 files changed

+69
-8
lines changed

2 files changed

+69
-8
lines changed

src/Plugin.spec.ts

+52-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', () => {
@@ -402,6 +404,19 @@ describe('Plugin', () => {
402404
verify(harExporterMock.end()).once();
403405
verify(fileManagerMock.removeFile(tempFilePath)).once();
404406
});
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+
});
405420
});
406421

407422
describe('recordHar', () => {
@@ -495,6 +510,26 @@ describe('Plugin', () => {
495510
// assert
496511
verify(harExporterMock.write(request)).never();
497512
});
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+
});
498533
});
499534

500535
describe('disposeOfHar', () => {
@@ -541,5 +576,17 @@ describe('Plugin', () => {
541576
// assert
542577
verify(networkObserverMock.unsubscribe()).never();
543578
});
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+
});
544591
});
545592
});

src/Plugin.ts

+17-3
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

@@ -83,6 +85,10 @@ export class Plugin {
8385
);
8486
}
8587

88+
if (!this.supported) {
89+
return;
90+
}
91+
8692
await this.closeConnection();
8793

8894
this.exporter = await this.exporterFactory.create(options);
@@ -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)