Skip to content
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

feat(msw): define the return value type in the msw mock function. #1171

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions packages/core/src/writers/split-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,7 @@ export const writeSplitMode = async ({
});
mockData += builder.importsMock({
implementation: implementationMock,
imports: [
{
exports: importsMock,
dependency: relativeSchemasPath,
},
],
imports: [{ exports: imports, dependency: relativeSchemasPath }],
specsName,
hasSchemaDir: !!output.schemas,
isAllowSyntheticDefaultImports,
Expand Down
7 changes: 1 addition & 6 deletions packages/core/src/writers/split-tags-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ export const writeSplitTagsMode = async ({
});
mockData += builder.importsMock({
implementation: implementationMock,
imports: [
{
exports: importsMock,
dependency: relativeSchemasPath,
},
],
imports: importsForBuilder,
specsName,
hasSchemaDir: !!output.schemas,
isAllowSyntheticDefaultImports,
Expand Down
24 changes: 12 additions & 12 deletions packages/core/src/writers/tags-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,20 @@ export const writeTagsMode = async ({
? upath.relativeSafe(dirname, getFileInfo(output.schemas).dirname)
: './' + filename + '.schemas';

const importsForBuilder = [
{
exports: imports.filter(
(imp) =>
!importsMock.some((impMock) => imp.name === impMock.name),
),
dependency: schemasPathRelative,
},
];

data += builder.imports({
client: output.client,
implementation,
imports: [
{
exports: imports.filter(
(imp) =>
!importsMock.some((impMock) => imp.name === impMock.name),
),
dependency: schemasPathRelative,
},
],
imports: importsForBuilder,
specsName,
hasSchemaDir: !!output.schemas,
isAllowSyntheticDefaultImports,
Expand All @@ -74,9 +76,7 @@ export const writeTagsMode = async ({
if (output.mock) {
data += builder.importsMock({
implementation: implementationMock,
imports: [
{ exports: importsMock, dependency: schemasPathRelative },
],
imports: importsForBuilder,
specsName,
hasSchemaDir: !!output.schemas,
isAllowSyntheticDefaultImports,
Expand Down
4 changes: 3 additions & 1 deletion packages/mock/src/msw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export const generateMSW = (

const isResponseOverridable = value.includes(overrideVarName);

const returnType = response.definition.success;

const isTextPlain = response.contentTypes.includes('text/plain');

const functionName = `get${pascal(operationId)}Mock`;
Expand Down Expand Up @@ -104,7 +106,7 @@ export const ${handlerName} = http.${verb}('${route}', async () => {
implementation: {
function:
value && value !== 'undefined'
? `export const ${functionName} = (${isResponseOverridable ? `overrideResponse?: any` : ''}) => (${value})\n\n`
? `export const ${functionName} = (${isResponseOverridable ? `overrideResponse?: any` : ''}): ${returnType} => (${value})\n\n`
: '',
handlerName: handlerName,
handler: handlerImplementation,
Expand Down