fix(core): support target without watch option (#2735)

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".
This commit is contained in:
Edouard Bozon 2021-05-07 10:20:08 +02:00 committed by Victor Savkin
parent f4ea0df51d
commit 5340986722
2 changed files with 50 additions and 3 deletions

View File

@ -29,6 +29,9 @@ describe('Cypress builder', () => {
ReturnType<typeof installedCypressVersion> ReturnType<typeof installedCypressVersion>
> = installedCypressVersion as any; > = installedCypressVersion as any;
mockContext = { root: '/root', workspace: { projects: {} } } as any; mockContext = { root: '/root', workspace: { projects: {} } } as any;
(devkit as any).readTargetOptions = jest.fn().mockReturnValue({
watch: true,
});
let runExecutor: any; let runExecutor: any;
beforeEach(async () => { beforeEach(async () => {
@ -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');
});
}); });

View File

@ -4,6 +4,7 @@ import {
ExecutorContext, ExecutorContext,
logger, logger,
parseTargetString, parseTargetString,
readTargetOptions,
runExecutor, runExecutor,
stripIndents, stripIndents,
} from '@nrwl/devkit'; } from '@nrwl/devkit';
@ -115,14 +116,23 @@ async function* startDevServer(
const { project, target, configuration } = parseTargetString( const { project, target, configuration } = parseTargetString(
opts.devServerTarget opts.devServerTarget
); );
const devServerTargetOpts = readTargetOptions(
{ project, target, configuration },
context
);
const targetSupportsWatchOpt = Object.keys(devServerTargetOpts).includes(
'watch'
);
for await (const output of await runExecutor<{ for await (const output of await runExecutor<{
success: boolean; success: boolean;
baseUrl?: string; baseUrl?: string;
}>( }>(
{ project, target, configuration }, { project, target, configuration },
{ // @NOTE: Do not forward watch option if not supported by the target dev server,
watch: opts.watch, // 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 context
)) { )) {
if (!output.success && !opts.watch) if (!output.success && !opts.watch)