-
Notifications
You must be signed in to change notification settings - Fork 20
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
chore(test): Streaming mock fixes. Mock cleanup. #549
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,10 @@ export const setupMockStreamingProcessor = ( | |
errorTimeoutSeconds: number = 0, | ||
initTimeoutMs: number = 0, | ||
) => { | ||
let initTimeoutHandle: any; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The mock stream was not closing its timer handles so they interfered with other tests. |
||
let patchTimeoutHandle: any; | ||
let deleteTimeoutHandle: any; | ||
|
||
MockStreamingProcessor.mockImplementation( | ||
( | ||
sdkKey: string, | ||
|
@@ -28,26 +32,36 @@ export const setupMockStreamingProcessor = ( | |
) => ({ | ||
start: jest.fn(async () => { | ||
if (shouldError) { | ||
setTimeout(() => { | ||
initTimeoutHandle = setTimeout(() => { | ||
const unauthorized = new Error('test-error') as LDStreamingError; | ||
// @ts-ignore | ||
unauthorized.code = 401; | ||
errorHandler(unauthorized); | ||
}, errorTimeoutSeconds * 1000); | ||
} else { | ||
// execute put which will resolve the identify promise | ||
setTimeout(() => listeners.get('put')?.processJson(putResponseJson), initTimeoutMs); | ||
initTimeoutHandle = setTimeout(() => { | ||
listeners.get('put')?.processJson(putResponseJson); | ||
}, initTimeoutMs); | ||
|
||
if (patchResponseJson) { | ||
setTimeout(() => listeners.get('patch')?.processJson(patchResponseJson)); | ||
patchTimeoutHandle = setTimeout(() => | ||
listeners.get('patch')?.processJson(patchResponseJson), | ||
); | ||
} | ||
|
||
if (deleteResponseJson) { | ||
setTimeout(() => listeners.get('delete')?.processJson(deleteResponseJson)); | ||
deleteTimeoutHandle = setTimeout(() => | ||
listeners.get('delete')?.processJson(deleteResponseJson), | ||
); | ||
} | ||
} | ||
}), | ||
close: jest.fn(), | ||
close: jest.fn(() => { | ||
clearTimeout(initTimeoutHandle); | ||
clearTimeout(patchTimeoutHandle); | ||
clearTimeout(deleteTimeoutHandle); | ||
}), | ||
eventSource: {}, | ||
}), | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,11 +57,11 @@ describe('sdk-client object', () => { | |
defaultPutResponse = clone<Flags>(mockResponseJson); | ||
setupMockStreamingProcessor(false, defaultPutResponse); | ||
mockPlatform.crypto.randomUUID.mockReturnValue('random1'); | ||
const hasher: Hasher = { | ||
update: jest.fn(() => hasher), | ||
const hasher = { | ||
update: jest.fn((): Hasher => hasher), | ||
digest: jest.fn(() => 'digested1'), | ||
}; | ||
mockPlatform.crypto.createHash = jest.fn(() => hasher); | ||
mockPlatform.crypto.createHash.mockReturnValue(hasher); | ||
|
||
ldc = new LDClientImpl(testSdkKey, AutoEnvAttributes.Enabled, mockPlatform, { | ||
logger, | ||
|
@@ -72,7 +72,8 @@ describe('sdk-client object', () => { | |
.mockReturnValue('/stream/path'); | ||
}); | ||
|
||
afterEach(() => { | ||
afterEach(async () => { | ||
await ldc.close(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Close the client so timers and things stop. |
||
jest.resetAllMocks(); | ||
}); | ||
|
||
|
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.
File was redundant.