Skip to content

Commit

Permalink
fix(core): support target without watch option (#2735)
Browse files Browse the repository at this point in the history
This commit checks if the target dev server supports the watch option before forwarding it to, this is relevant for running Cypress against dev server target that does not support this option, for instance "@nguniversal/builders:ssr-dev-server".
  • Loading branch information
edbzn authored and vsavkin committed May 13, 2021
1 parent f4ea0df commit 5340986
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
37 changes: 37 additions & 0 deletions packages/cypress/src/executors/cypress/cypress.impl.spec.ts
Expand Up @@ -29,6 +29,9 @@ describe('Cypress builder', () => {
ReturnType<typeof installedCypressVersion>
> = installedCypressVersion as any;
mockContext = { root: '/root', workspace: { projects: {} } } as any;
(devkit as any).readTargetOptions = jest.fn().mockReturnValue({
watch: true,
});
let runExecutor: any;

beforeEach(async () => {
Expand Down Expand Up @@ -285,4 +288,38 @@ describe('Cypress builder', () => {
})
);
});

it('should not forward watch option to devServerTarget when not supported', async () => {
// Simulate a dev server target that does not support watch option.
(devkit as any).readTargetOptions = jest.fn().mockReturnValue({});

const { success } = await cypressExecutor(cypressOptions, mockContext);

expect(success).toEqual(true);
expect((devkit as any).readTargetOptions.mock.calls[0][0]).toEqual(
expect.objectContaining({
project: 'my-app',
target: 'serve',
})
);
expect(Object.keys(runExecutor.mock.calls[0][1])).not.toContain('watch');
});

it('should forward watch option to devServerTarget when supported', async () => {
// Simulate a dev server target that support watch option.
(devkit as any).readTargetOptions = jest
.fn()
.mockReturnValue({ watch: true });

const { success } = await cypressExecutor(cypressOptions, mockContext);

expect(success).toEqual(true);
expect((devkit as any).readTargetOptions.mock.calls[0][0]).toEqual(
expect.objectContaining({
project: 'my-app',
target: 'serve',
})
);
expect(Object.keys(runExecutor.mock.calls[0][1])).toContain('watch');
});
});
16 changes: 13 additions & 3 deletions packages/cypress/src/executors/cypress/cypress.impl.ts
Expand Up @@ -4,6 +4,7 @@ import {
ExecutorContext,
logger,
parseTargetString,
readTargetOptions,
runExecutor,
stripIndents,
} from '@nrwl/devkit';
Expand Down Expand Up @@ -115,14 +116,23 @@ async function* startDevServer(
const { project, target, configuration } = parseTargetString(
opts.devServerTarget
);
const devServerTargetOpts = readTargetOptions(
{ project, target, configuration },
context
);
const targetSupportsWatchOpt = Object.keys(devServerTargetOpts).includes(
'watch'
);

for await (const output of await runExecutor<{
success: boolean;
baseUrl?: string;
}>(
{ project, target, configuration },
{
watch: opts.watch,
},
// @NOTE: Do not forward watch option if not supported by the target dev server,
// this is relevant for running Cypress against dev server target that does not support this option,
// for instance @nguniversal/builders:ssr-dev-server.
targetSupportsWatchOpt ? { watch: opts.watch } : {},
context
)) {
if (!output.success && !opts.watch)
Expand Down

0 comments on commit 5340986

Please sign in to comment.