Skip to content

test: cdk watch integ tests for cli and toolkit lib#1139

Merged
iankhou merged 15 commits intomainfrom
iankhou-cdk-watch-integ
Feb 25, 2026
Merged

test: cdk watch integ tests for cli and toolkit lib#1139
iankhou merged 15 commits intomainfrom
iankhou-cdk-watch-integ

Conversation

@iankhou
Copy link
Contributor

@iankhou iankhou commented Feb 12, 2026

Tests #1134

Description of tests

cdk watch in CLI

  • Test: Detects file changes for watched files
  • Test: Does NOT detect file changes for unwatched files

Toolkit.watch()

  • Test: Detects file changes for watched files
  • Test: Does NOT detect file changes for unwatched files

We used AI 🤖 to help with this contribution.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@codecov-commenter
Copy link

codecov-commenter commented Feb 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.76%. Comparing base (755842d) to head (a5bde3e).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1139   +/-   ##
=======================================
  Coverage   87.75%   87.76%           
=======================================
  Files          72       72           
  Lines       10137    10137           
  Branches     1339     1338    -1     
=======================================
+ Hits         8896     8897    +1     
+ Misses       1216     1215    -1     
  Partials       25       25           
Flag Coverage Δ
suite.unit 87.76% <ø> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

iankhou and others added 4 commits February 13, 2026 16:14
Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Comment on lines +78 to +79
// Use separate output dir to avoid conflicts with lingering watch process
await fixture.cdkDestroy('test-1', { options: ['--output', 'cdk-destroy.out'] });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to manually delete. The rest harness is doing this for us.

Suggested change
// Use separate output dir to avoid conflicts with lingering watch process
await fixture.cdkDestroy('test-1', { options: ['--output', 'cdk-destroy.out'] });

}),
);

integTest(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one test per file. Pls move to separate file.

Comment on lines +84 to +110
async function waitForOutput(getOutput: () => string, searchString: string, timeoutMs: number): Promise<void> {
const startTime = Date.now();
return new Promise((resolve, reject) => {
const check = () => {
if (getOutput().includes(searchString)) return resolve();
if (Date.now() - startTime > timeoutMs) {
return reject(new Error(`Timeout waiting for: "${searchString}"`));
}
setTimeout(check, 1000);
};
check();
});
}

async function waitForCondition(condition: () => boolean, timeoutMs: number, description: string): Promise<void> {
const startTime = Date.now();
return new Promise((resolve, reject) => {
const check = () => {
if (condition()) return resolve();
if (Date.now() - startTime > timeoutMs) {
return reject(new Error(`Timeout waiting for ${description}`));
}
setTimeout(check, 1000);
};
check();
});
}
Copy link
Contributor

@mrgrain mrgrain Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there really no existing helpers for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, I couldn't find any that poll like this. I'm adding a negative test, though, so moving these to a shared file under the watch dir.

});

try {
await waitForOutput(() => output, "Triggering initial 'cdk deploy'", 120000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did you come up with these timeout durations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really based on anything in particular. The initial deployment takes longer than subsequent ones, so I was generous here.

);

integTest(
'toolkit watch excludes node_modules and dotfiles by default',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this test not part of the cli integ tests also?

Copy link
Contributor Author

@iankhou iankhou Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it will add more time to the test, while offering limited utility IMO. Probably another minute or so. See my PR description where I addressed this discrepancy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont understand why this would be the case. i think you need to
a) touch node_modules / dotfiles
b) assert that we do not see Detected change to '<file>' -- this happens nearly instantly if the file is being watched, so i dont see why it would take another minute.

as for whether this offers utility, i think its worse if we accidentally don't exclude files from watch than if we accidentally don't include files.

const cdkJsonPath = path.join(fixture.integTestDir, 'cdk.json');
const cdkJson = JSON.parse(fs.readFileSync(cdkJsonPath, 'utf-8'));
cdkJson.watch = {
include: ['**/*.ts', '**/*.js'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: including the .js files is a bad idea for some use cases (say you're also auto-compiling ts changes, now you're double counting). doesn't seem necessary in your test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I concur, removed **/*.js.

const cdkJson = JSON.parse(fs.readFileSync(cdkJsonPath, 'utf-8'));
cdkJson.watch = {
include: ['**/*.ts', '**/*.js'],
exclude: ['node_modules/**', 'cdk.out/**', '**/*.d.ts'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part isn't tested. in this test or a new one, we should be testing that changes to exclude files do not trigger deployment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I included this to model a default config, but you're right that we don't need it. Removed.

iankhou and others added 4 commits February 24, 2026 13:30
Comment on lines +47 to +57
await waitForOutput(() => output, "Triggering initial 'cdk deploy'", 120000);
fixture.log('✓ Watch started');

await waitForOutput(() => output, 'deployment time', 300000);
fixture.log('✓ Initial deployment completed');

// Modify the test file to trigger a watch event
fs.writeFileSync(testFile, 'export const modified = true;');

await waitForOutput(() => output, 'Detected change to', 60000);
fixture.log('✓ Watch detected file change');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need individual timeout expectations for each of these? Feels like a top-level timeout would be enough to ensure it doesn't go overboard.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed individual timeouts, just left the file-level one.

fixture.log('✓ Initial deployment completed');

// Modify the test file to trigger a watch event
fs.writeFileSync(testFile, 'export const modified = true;');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also just touch the file: child_process.spawn('touch', [testFile])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to this approach.

});

await waitForOutput(() => output, "Triggering initial 'cdk deploy'", 120000);
fixture.log('✓ Watch started');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Suggested change
fixture.log('✓ Watch started');
fixture.log('✓ Watch start detected');


await waitForOutput(() => output, 'Detected change to', 60000);
fixture.log('✓ Watch detected file change');
}),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we not checking that the second deployment runs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add a check.

await waitForOutput(() => output, 'Detected change to', 60000);
fixture.log('✓ Watch detected file change');
}),
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is watch terminated here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not, I'll end the process.

Comment on lines +69 to +71
if (detectedExcluded) {
throw new Error('Watch should NOT have detected changes to excluded file');
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not testing primitives like expect etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will use expect instead, oops

// Verify deployment count hasn't increased
const deploymentsAfter = (output.match(/deployment time/g) || []).length;
if (deploymentsAfter > deploymentsBefore) {
throw new Error(`Unexpected deployment triggered. Before: ${deploymentsBefore}, After: ${deploymentsAfter}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect?

Comment on lines +29 to +46
// Start cdk watch with detached process group for clean termination
const watchProcess = child_process.spawn('cdk', [
'watch', '--hotswap', '-v', fixture.fullStackName('test-1'),
], {
cwd: fixture.integTestDir,
shell: true,
detached: true,
env: { ...process.env, ...fixture.cdkShellEnv() },
});

watchProcess.stdout?.on('data', (data) => {
output += data.toString();
fixture.log(data.toString());
});
watchProcess.stderr?.on('data', (data) => {
output += data.toString();
fixture.log(data.toString());
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this could also be a helper on the fixture, similar to cdkDeploy

fixture.log('Created excluded file: should-be-ignored.excluded.ts');

// Wait a reasonable time for any potential (unwanted) detection
await sleep(5000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine 😢

Comment on lines +66 to +67
const detectedExcluded = output.includes('Detected change to') &&
output.includes('excluded');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const detectedExcluded = output.includes('Detected change to') &&
output.includes('excluded');
const detectedExcluded = output.includes('Detected change to') && output.includes('excluded');

const configMsg = configMessages.find(m => m.includes("'exclude' patterns"));

if (!configMsg) {
throw new Error('Did not receive exclude patterns configuration message');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect etc


// Check that default excludes are present
if (!configMsg.includes('node_modules')) {
throw new Error('Default excludes should include node_modules');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect etc

throw new Error('Default excludes should include node_modules');
}
if (!configMsg.includes('.*')) {
throw new Error('Default excludes should include dotfiles (.*)');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect etc

);

if (!hasReadyOrObserving) {
throw new Error('Watcher did not emit ready/observing events');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect etc

iankhou and others added 2 commits February 25, 2026 14:33
Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants