-
Notifications
You must be signed in to change notification settings - Fork 4k
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
test(toolkit): watch tests #33040
Open
kaizencc
wants to merge
2
commits into
main
Choose a base branch
from
conroy/watch-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+179
−6
Open
test(toolkit): watch tests #33040
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
// 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 | ||
// 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 { | ||
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, | ||
// we would have to change it only in one place if it ever breaks | ||
const firstCall = mockChokidarWatcherOn.mock.calls[0]; | ||
// let's make sure the first argument is the 'ready' event, | ||
// just to be double safe | ||
expect(firstCall[0]).toBe('ready'); | ||
// the second argument is the callback | ||
return firstCall[1]; | ||
}, | ||
|
||
get fileEventCallback(): ( | ||
event: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir', | ||
path: string, | ||
) => Promise<void> { | ||
expect(mockChokidarWatcherOn.mock.calls.length).toBeGreaterThanOrEqual(2); | ||
const secondCall = mockChokidarWatcherOn.mock.calls[1]; | ||
// let's make sure the first argument is not the 'ready' event, | ||
// just to be double safe | ||
expect(secondCall[0]).not.toBe('ready'); | ||
// the second argument is the callback | ||
return secondCall[1]; | ||
}, | ||
}; | ||
|
||
const mockChokidarWatch = jest.fn(); | ||
jest.mock('chokidar', () => ({ | ||
watch: mockChokidarWatch, | ||
})); | ||
|
||
import { HotswapMode } from '../../lib'; | ||
import { Toolkit } from '../../lib/toolkit'; | ||
import { builderFixture, TestIoHost } from '../_helpers'; | ||
|
||
const ioHost = new TestIoHost(); | ||
const toolkit = new Toolkit({ ioHost }); | ||
jest.spyOn(toolkit, 'rollback').mockResolvedValue(); | ||
|
||
let mockDeployStack = jest.fn().mockResolvedValue({ | ||
type: 'did-deploy-stack', | ||
stackArn: 'arn:aws:cloudformation:region:account:stack/test-stack', | ||
outputs: {}, | ||
noOp: false, | ||
}); | ||
|
||
jest.mock('../../lib/api/aws-cdk', () => { | ||
return { | ||
...jest.requireActual('../../lib/api/aws-cdk'), | ||
Deployments: jest.fn().mockImplementation(() => ({ | ||
deployStack: mockDeployStack, | ||
resolveEnvironment: jest.fn().mockResolvedValue({}), | ||
isSingleAssetPublished: jest.fn().mockResolvedValue(true), | ||
readCurrentTemplate: jest.fn().mockResolvedValue({ Resources: {} }), | ||
})), | ||
}; | ||
}); | ||
|
||
beforeEach(() => { | ||
ioHost.notifySpy.mockClear(); | ||
ioHost.requestSpy.mockClear(); | ||
jest.clearAllMocks(); | ||
|
||
mockChokidarWatch.mockReturnValue(fakeChokidarWatcher); | ||
// on() in chokidar's Watcher returns 'this' | ||
mockChokidarWatcherOn.mockReturnValue(fakeChokidarWatcher); | ||
}); | ||
|
||
describe('watch', () => { | ||
test('no include & no exclude results in error', async () => { | ||
// WHEN | ||
const cx = await builderFixture(toolkit, 'stack-with-role'); | ||
await expect(async () => toolkit.watch(cx, {})).rejects.toThrow(/Cannot use the 'watch' command without specifying at least one directory to monitor. Make sure to add a \"watch\" key to your cdk.json/); | ||
}); | ||
|
||
test('observes cwd as default rootdir', async () => { | ||
// WHEN | ||
const cx = await builderFixture(toolkit, 'stack-with-role'); | ||
ioHost.level = 'debug'; | ||
await toolkit.watch(cx, { | ||
include: [], | ||
}); | ||
|
||
// THEN | ||
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({ | ||
action: 'watch', | ||
level: 'debug', | ||
message: expect.stringContaining(`root directory used for 'watch' is: ${process.cwd()}`), | ||
})); | ||
}); | ||
|
||
test('ignores output dir, dot files, dot directories, node_modules by default', async () => { | ||
// WHEN | ||
const cx = await builderFixture(toolkit, 'stack-with-role'); | ||
ioHost.level = 'debug'; | ||
await toolkit.watch(cx, { | ||
exclude: [], | ||
}); | ||
|
||
// THEN | ||
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({ | ||
action: 'watch', | ||
level: 'debug', | ||
message: expect.stringContaining(`'exclude' patterns for 'watch': ["cdk.out/**","**/.*","**/.*/**","**/node_modules/**"]`), | ||
})); | ||
}); | ||
|
||
test('can include specific files', async () => { | ||
// WHEN | ||
const cx = await builderFixture(toolkit, 'stack-with-role'); | ||
ioHost.level = 'debug'; | ||
await toolkit.watch(cx, { | ||
include: ['index.ts'], | ||
}); | ||
|
||
// THEN | ||
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({ | ||
action: 'watch', | ||
level: 'debug', | ||
message: expect.stringContaining(`'include' patterns for 'watch': ["index.ts"]`), | ||
})); | ||
}); | ||
|
||
test('can exclude specific files', async () => { | ||
// WHEN | ||
const cx = await builderFixture(toolkit, 'stack-with-role'); | ||
ioHost.level = 'debug'; | ||
await toolkit.watch(cx, { | ||
exclude: ['index.ts'], | ||
}); | ||
|
||
// THEN | ||
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({ | ||
action: 'watch', | ||
level: 'debug', | ||
message: expect.stringContaining(`'exclude' patterns for 'watch': ["index.ts"`), | ||
})); | ||
}); | ||
|
||
describe.each([HotswapMode.FALL_BACK, HotswapMode.HOTSWAP_ONLY])('%p mode', (hotswapMode) => { | ||
test('passes through the correct hotswap mode to deployStack()', async () => { | ||
// WHEN | ||
const cx = await builderFixture(toolkit, 'stack-with-role'); | ||
ioHost.level = 'warn'; | ||
await toolkit.watch(cx, { | ||
include: [], | ||
hotswap: hotswapMode, | ||
}); | ||
|
||
fakeChokidarWatcherOn.readyCallback(); | ||
await new Promise(resolve => setTimeout(resolve, 3000)); | ||
|
||
// THEN | ||
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({ | ||
action: 'deploy', | ||
level: 'warn', | ||
message: expect.stringContaining('The --hotswap and --hotswap-fallback flags deliberately'), | ||
})); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm frustrated but this is all i can think of at the moment. the problem is this:
toolkit.deploy
but that just... doesn't work.I can see that
toolkit.deploy
is called normally, and then not called when it is mocked. But jest refuses to show thatmockDeploy
has been called.fakeChokidaorWatcherOn.readyCallback()
triggers watch to in turn invoke deploy. But that all happens asynchronously, andioHost.notifySpy
does not get the right messages since there's nothing to await. Just waiting for 3 seconds cannot be the best thing to do here, but my gut tells me that we want to figure out how to do 1), and then this point will be moot