-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FI-1236 feat: add "full mocks" functionality
fix: pack compilation errors `is not under 'rootDir'`
- Loading branch information
Showing
48 changed files
with
837 additions
and
90 deletions.
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
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
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,30 @@ | ||
import {useContext} from '../useContext'; | ||
import {assertValueIsUndefined} from '../utils/asserts'; | ||
|
||
import type {FullMocksState} from '../types/internal'; | ||
|
||
/** | ||
* Raw versions of `getFullMocksState` and `setFullMocksState`. | ||
* @internal | ||
*/ | ||
const [getFullMocksState, setRawFullMocksState] = useContext<FullMocksState>(); | ||
|
||
/** | ||
* Get state of full mocks. | ||
* @internal | ||
*/ | ||
export {getFullMocksState}; | ||
|
||
/** | ||
* Set state of full mocks (can only be called once). | ||
* @internal | ||
*/ | ||
export const setFullMocksState: typeof setRawFullMocksState = (fullMocksState) => { | ||
const currentFullMocksState = getFullMocksState(); | ||
|
||
assertValueIsUndefined(currentFullMocksState, 'currentFullMocksState is not defined', { | ||
fullMocksState, | ||
}); | ||
|
||
return setRawFullMocksState(fullMocksState); | ||
}; |
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,105 @@ | ||
import type {URL} from 'node:url'; | ||
|
||
import type {Brand} from './brand'; | ||
import type {Method, Request, Response, ResponseWithRequest, StatusCode} from './http'; | ||
import type {TestStaticOptions} from './testRun'; | ||
import type {TestMetaPlaceholder} from './userland'; | ||
|
||
/** | ||
* Options of `getResponseFromFullMocks` function. | ||
*/ | ||
type ResponseFromFullMocksOptions = Readonly<{ | ||
request: Request; | ||
requestKind: RequestKind; | ||
responseWithRequest: ResponseWithRequest | undefined; | ||
testFullMocks: TestFullMocks; | ||
}>; | ||
|
||
/** | ||
* Functions that specify the "full mocks" functionality. | ||
*/ | ||
export type FullMocksConfig<TestMeta = TestMetaPlaceholder> = Readonly<{ | ||
/** | ||
* Filters tests by their static options — | ||
* full mocks will only be applied to tests for which the function returned `true`. | ||
*/ | ||
filterTests: (this: void, testStaticOptions: TestStaticOptions<TestMeta>) => boolean; | ||
|
||
/** | ||
* Get `RequestKind` of request by `method` and `urlObject`. | ||
*/ | ||
getRequestKind: (this: void, method: Method, urlObject: URL) => RequestKind; | ||
|
||
/** | ||
* Get `response` on `request` by `requestKind` and by test full mocks. | ||
*/ | ||
getResponseFromFullMocks: ( | ||
this: void, | ||
options: ResponseFromFullMocksOptions, | ||
) => FullMocksResponse; | ||
|
||
/** | ||
* Get `responseWithRequest` of API request to write to full mocks. | ||
* If it returns `undefined`, the response is not written to full mocks. | ||
*/ | ||
getResponseToWriteToFullMocks: ( | ||
this: void, | ||
requestKind: RequestKind, | ||
responseWithRequest: ResponseWithRequest, | ||
) => ResponseWithRequest | undefined; | ||
|
||
/** | ||
* Reads full mocks of one test by `testId`. | ||
*/ | ||
readTestFullMocks: (this: void, testId: FullMocksTestId) => Promise<TestFullMocks | undefined>; | ||
|
||
/** | ||
* Writes full mocks of one test by `testId`. | ||
*/ | ||
writeTestFullMocks: ( | ||
this: void, | ||
testId: FullMocksTestId, | ||
testFullMocks: TestFullMocks, | ||
) => Promise<void>; | ||
}>; | ||
|
||
/** | ||
* Mocked (generated) `response` for full mocks. | ||
*/ | ||
export type FullMocksResponse = Partial<Response> & Readonly<{statusCode: StatusCode}>; | ||
|
||
/** | ||
* Parameters of special `FullMocksRoute`. | ||
* @internal | ||
*/ | ||
export type FullMocksRouteParams = Readonly<{ | ||
fullMocksState: FullMocksState; | ||
method: Method; | ||
requestKind: RequestKind; | ||
urlObject: URL; | ||
}>; | ||
|
||
/** | ||
* State of full mocks during concrete test. | ||
* @internal | ||
*/ | ||
export type FullMocksState = Readonly<{ | ||
appliedMocks: Record<RequestKind, number> | undefined; | ||
testFullMocks: TestFullMocks; | ||
testId: FullMocksTestId; | ||
}>; | ||
|
||
/** | ||
* Identifier of test (usually the hash of test file content). | ||
*/ | ||
export type FullMocksTestId = Brand<string, 'FullMocksTestId'>; | ||
|
||
/** | ||
* Identifier of request in set of requests for one test (usually just `path` of request url). | ||
*/ | ||
export type RequestKind = Brand<string, 'RequestKind'>; | ||
|
||
/** | ||
* Full mocks of one test. | ||
*/ | ||
export type TestFullMocks = Readonly<Record<RequestKind, readonly ResponseWithRequest[]>>; |
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
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,24 @@ | ||
import {assertValueIsNotNull} from '../asserts'; | ||
import {getFullPackConfig} from '../config'; | ||
import {generalLog} from '../generalLog'; | ||
|
||
import type {FullMocksState} from '../../types/internal'; | ||
|
||
/** | ||
* Writes full mocks of one test. | ||
* @internal | ||
*/ | ||
export const writeFullMocks = async (fullMocksState: FullMocksState): Promise<void> => { | ||
const {fullMocks: fullMocksConfig} = getFullPackConfig(); | ||
|
||
assertValueIsNotNull(fullMocksConfig, 'fullMocksConfig is not null'); | ||
|
||
await fullMocksConfig.writeTestFullMocks(fullMocksState.testId, fullMocksState.testFullMocks); | ||
|
||
generalLog('Full mocks have been written', { | ||
requestKinds: Object.fromEntries( | ||
Object.entries(fullMocksState.testFullMocks).map(([key, value]) => [key, value.length]), | ||
), | ||
testId: fullMocksState.testId, | ||
}); | ||
}; |
Oops, something went wrong.