Skip to content

Commit

Permalink
chokidar mock and breadcrumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgrain committed Jan 24, 2025
1 parent 24ef91b commit fed760f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/toolkit/lib/actions/watch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface WatchOptions extends BaseDeployOptions {
* The output directory to write CloudFormation template to
*
* @deprecated this should be grabbed from the cloud assembly itself
*
*
* @default 'cdk.out'
*/
readonly outdir?: string;
Expand Down
15 changes: 8 additions & 7 deletions packages/@aws-cdk/toolkit/lib/toolkit/toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
* Implies hotswap deployments.
*/
public async watch(cx: ICloudAssemblySource, options: WatchOptions): Promise<void> {
// const assembly = this.assemblyFromSource(cx, false);
const ioHost = withAction(this.ioHost, 'watch');
const rootDir = options.watchDir ?? process.cwd();
await ioHost.notify(debug(`root directory used for 'watch' is: ${rootDir}`));
Expand Down Expand Up @@ -565,7 +566,9 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
latch = 'deploying';
// cloudWatchLogMonitor?.deactivate();

await this.invokeDeployFromWatch(cx, options);
await this.invokeDeployFromWatch(cx, options,
// assembly
);

// If latch is still 'deploying' after the 'await', that's fine,
// but if it's 'queued', that means we need to deploy again
Expand All @@ -584,7 +587,6 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
.watch(watchIncludes, {
ignored: watchExcludes,
cwd: rootDir,
// ignoreInitial: true,
})
.on('ready', async () => {
latch = 'open';
Expand Down Expand Up @@ -728,10 +730,6 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
const deployOptions: DeployOptions = {
...options,
requireApproval: RequireApproval.NEVER,
// if 'watch' is called by invoking 'cdk deploy --watch',
// we need to make sure to not call 'deploy' with 'watch' again,
// as that would lead to a cycle
// watch: false,
// cloudWatchLogMonitor,
// cacheCloudAssembly: false,
hotswap: options.hotswap,
Expand All @@ -740,7 +738,10 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
};

try {
await this.deploy(cx, deployOptions);
// await this._deploy(
await this.deploy(
// assembly,
cx, deployOptions);
} catch {
// just continue - deploy will show the error
}
Expand Down
19 changes: 9 additions & 10 deletions packages/@aws-cdk/toolkit/test/actions/watch.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// We need to mock the chokidar library, used by 'cdk watch'
// This needs to happen ABOVE the import statements due to quirks with how jest works
// Apparently, they hoist jest.mock commands just below the import statements so we
// Apparently, they hoist jest.mock commands just below the import statements so we
// need to make sure that the constants they access are initialized before the imports.
const mockChokidarWatcherOn = jest.fn();
const fakeChokidarWatcher = {
on: mockChokidarWatcherOn,
};
const fakeChokidarWatcherOn = {
get readyCallback(): () => void {
get readyCallback(): () => Promise<void> {
expect(mockChokidarWatcherOn.mock.calls.length).toBeGreaterThanOrEqual(1);
// The call to the first 'watcher.on()' in the production code is the one we actually want here.
// This is a pretty fragile, but at least with this helper class,
Expand All @@ -21,8 +21,8 @@ const fakeChokidarWatcherOn = {
},

get fileEventCallback(): (
event: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir',
path: string,
event: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir',
path: string,
) => Promise<void> {
expect(mockChokidarWatcherOn.mock.calls.length).toBeGreaterThanOrEqual(2);
const secondCall = mockChokidarWatcherOn.mock.calls[1];
Expand Down Expand Up @@ -111,7 +111,7 @@ describe('watch', () => {
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({
action: 'watch',
level: 'debug',
message: expect.stringContaining(`'exclude' patterns for 'watch': ["cdk.out/**","**/.*","**/.*/**","**/node_modules/**"]`),
message: expect.stringContaining('\'exclude\' patterns for \'watch\': ["cdk.out/**","**/.*","**/.*/**","**/node_modules/**"]'),
}));
});

Expand All @@ -127,7 +127,7 @@ describe('watch', () => {
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({
action: 'watch',
level: 'debug',
message: expect.stringContaining(`'include' patterns for 'watch': ["index.ts"]`),
message: expect.stringContaining('\'include\' patterns for \'watch\': ["index.ts"]'),
}));
});

Expand All @@ -143,7 +143,7 @@ describe('watch', () => {
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({
action: 'watch',
level: 'debug',
message: expect.stringContaining(`'exclude' patterns for 'watch': ["index.ts"`),
message: expect.stringContaining('\'exclude\' patterns for \'watch\': ["index.ts"'),
}));
});

Expand All @@ -157,8 +157,7 @@ describe('watch', () => {
hotswap: hotswapMode,
});

fakeChokidarWatcherOn.readyCallback();
await new Promise(resolve => setTimeout(resolve, 3000));
await fakeChokidarWatcherOn.readyCallback();

// THEN
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({
Expand All @@ -170,4 +169,4 @@ describe('watch', () => {
});
});

// @todo unit test watch with file events
// @todo unit test watch with file events
1 change: 0 additions & 1 deletion packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,6 @@ export class CdkToolkit {
.watch(watchIncludes, {
ignored: watchExcludes,
cwd: rootDir,
// ignoreInitial: true,
})
.on('ready', async () => {
latch = 'open';
Expand Down

0 comments on commit fed760f

Please sign in to comment.