Skip to content

Commit 2c2f3ca

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 a560686 commit 2c2f3ca

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
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', () => {
@@ -400,6 +402,19 @@ describe('Plugin', () => {
400402
verify(harExporterMock.end()).once();
401403
verify(fileManagerMock.removeFile(tempFilePath)).once();
402404
});
405+
406+
it('should be a no-op if an unsupported browser family is used', async () => {
407+
// arrange
408+
plugin.ensureBrowserFlags(firefox, []);
409+
await plugin.recordHar({
410+
rootDir: '/'
411+
});
412+
413+
// act
414+
await plugin.saveHar(options);
415+
// assert
416+
verify(fileManagerMock.createFolder(options.outDir)).never();
417+
});
403418
});
404419

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

498533
describe('disposeOfHar', () => {
@@ -539,5 +574,17 @@ describe('Plugin', () => {
539574
// assert
540575
verify(networkObserverMock.unsubscribe()).never();
541576
});
577+
578+
it('should be a no-op if an unsupported browser family is used', async () => {
579+
// arrange
580+
plugin.ensureBrowserFlags(firefox, []);
581+
await plugin.recordHar({
582+
rootDir: '/'
583+
});
584+
// act
585+
await plugin.disposeOfHar();
586+
// assert
587+
verify(harExporterMock.end()).never();
588+
});
542589
});
543590
});

src/Plugin.ts

+16-2
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 browser?: Cypress.Browser;
4748

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

@@ -77,6 +79,10 @@ export class Plugin {
7779
}
7880

7981
public async recordHar(options: RecordOptions): Promise<void> {
82+
if (this.browser && !this.isSupportedBrowser(this.browser)) {
83+
return;
84+
}
85+
8086
await this.closeConnection();
8187

8288
if (!this.addr) {
@@ -99,6 +105,10 @@ export class Plugin {
99105
}
100106

101107
public async saveHar(options: SaveOptions): Promise<void> {
108+
if (this.browser && !this.isSupportedBrowser(this.browser)) {
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.browser && !this.isSupportedBrowser(this.browser)) {
144+
return;
145+
}
146+
133147
await this.networkObservable?.unsubscribe();
134148
delete this.networkObservable;
135149

0 commit comments

Comments
 (0)