Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin): make plugin a "no-op" in unsupported browsers #460

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 54 additions & 5 deletions src/Plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,17 @@ describe('Plugin', () => {
expect(result).not.toContain(expectedArgs);
});

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

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

it('should log an error message when the connection is corrupted', async () => {
// arrange
plugin.ensureBrowserFlags(chrome, []);
// act
await plugin.saveHar(options);
// assert
Expand Down Expand Up @@ -400,6 +404,19 @@ describe('Plugin', () => {
verify(harExporterMock.end()).once();
verify(fileManagerMock.removeFile(tempFilePath)).once();
});

it('should be a no-op if an unsupported browser family is used', async () => {
// arrange
plugin.ensureBrowserFlags(firefox, []);
await plugin.recordHar({
rootDir: '/'
});

// act
await plugin.saveHar(options);
// assert
verify(fileManagerMock.createFolder(options.outDir)).never();
});
});

describe('recordHar', () => {
Expand Down Expand Up @@ -493,6 +510,26 @@ describe('Plugin', () => {
// assert
verify(harExporterMock.write(request)).never();
});

it('should be a no-op if an unsupported browser family is used', async () => {
const request = new NetworkRequest(
'1',
'https://example.com',
'https://example.com',
'1'
);
when(harExporterFactoryMock.create(anything())).thenResolve(
resolvableInstance(harExporterMock)
);
when(networkObserverMock.subscribe(anyFunction())).thenCall(callback =>
callback(request)
);
plugin.ensureBrowserFlags(firefox, []);
// act
await plugin.recordHar(options);
// assert
verify(harExporterMock.write(request)).never();
});
});

describe('disposeOfHar', () => {
Expand Down Expand Up @@ -539,5 +576,17 @@ describe('Plugin', () => {
// assert
verify(networkObserverMock.unsubscribe()).never();
});

it('should be a no-op if an unsupported browser family is used', async () => {
// arrange
plugin.ensureBrowserFlags(firefox, []);
await plugin.recordHar({
rootDir: '/'
});
// act
await plugin.disposeOfHar();
// assert
verify(harExporterMock.end()).never();
});
});
});
24 changes: 19 additions & 5 deletions src/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class Plugin {
private networkObservable?: Observer<NetworkRequest>;
private addr?: Addr;
private _connection?: Connection;
private supported?: boolean;

constructor(
private readonly logger: Logger,
Expand All @@ -57,9 +58,10 @@ export class Plugin {
browser: Cypress.Browser,
args: string[]
): string[] {
if (!this.isSupportedBrowser(browser)) {
throw new Error(
`An unsupported browser family was used: ${browser.name}`
this.supported = this.isSupportedBrowser(browser);
if (!this.supported) {
this.logger.warn(
`HAR recording is not supported in this browser: ${browser.name}`
);
}

Expand All @@ -77,14 +79,18 @@ export class Plugin {
}

public async recordHar(options: RecordOptions): Promise<void> {
await this.closeConnection();

if (!this.addr) {
throw new Error(
`Please call the 'ensureBrowserFlags' before attempting to start the recording.`
);
}

if (!this.supported) {
return;
}

await this.closeConnection();

this.exporter = await this.exporterFactory.create(options);
this._connection = this.connectionFactory.create({
...this.addr,
Expand All @@ -99,6 +105,10 @@ export class Plugin {
}

public async saveHar(options: SaveOptions): Promise<void> {
if (!this.supported) {
return;
}

const filePath = join(options.outDir, options.fileName);

if (!this._connection) {
Expand Down Expand Up @@ -130,6 +140,10 @@ export class Plugin {
}

public async disposeOfHar(): Promise<void> {
if (!this.supported) {
return;
}

await this.networkObservable?.unsubscribe();
delete this.networkObservable;

Expand Down