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): create an index file exporting msw for split tags mode #1803

Merged
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
24 changes: 24 additions & 0 deletions docs/src/pages/reference/configuration/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,30 @@ Default Value: `en`.

Give you the possibility to set the locale for the mock generation. It is used by faker, see the list of available options [here](https://fakerjs.dev/guide/localization.html#available-locales). It should also be strongly typed using `defineConfig`.

### indexMockFiles

Type: `Boolean`

Default Value: `false`.

When `true`, adds a `index.msw.ts` file which exports all mock functions.
This is only valid when `mode` is `tags-split`.

Example:

```js
module.exports = {
petstore: {
output: {
mode: 'tags-split',
mock: {
indexMockFiles: true,
},
},
},
};
```

### docs

Type: `Boolean | Object`.
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ export type GlobalMockOptions = {
baseUrl?: string;
// This is used to set the locale of the faker library
locale?: keyof typeof allLocales;
indexMockFiles?: boolean;
};

export type OverrideMockOptions = Partial<GlobalMockOptions> & {
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/writers/split-tags-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ export const writeSplitTagsMode = async ({
output.tsconfig,
);

const indexFilePath =
output.mock && !isFunction(output.mock) && output.mock.indexMockFiles
? upath.join(
dirname,
'index.' + getMockFileExtensionByTypeName(output.mock!) + extension,
)
: undefined;
if (indexFilePath) {
await fs.outputFile(indexFilePath, '');
}

const generatedFilePathsArray = await Promise.all(
Object.entries(target).map(async ([tag, target]) => {
try {
Expand Down Expand Up @@ -172,6 +183,14 @@ export const writeSplitTagsMode = async ({

if (mockPath) {
await fs.outputFile(mockPath, mockData);
if (indexFilePath) {
const localMockPath = upath.joinSafe(
'./',
tag,
tag + '.' + getMockFileExtensionByTypeName(output.mock!),
);
fs.appendFile(indexFilePath, `export * from '${localMockPath}'\n`);
}
}

return [
Expand Down
15 changes: 15 additions & 0 deletions tests/configs/default.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,19 @@ export default defineConfig({
target: '../specifications/petstore.yaml',
},
},
indexMockFiles: {
output: {
target: '../generated/default/index-mock-file/endpoints.ts',
schemas: '../generated/default/index-mock-file/model',
client: 'fetch',
mock: {
type: 'msw',
indexMockFiles: true,
},
mode: 'tags-split',
},
input: {
target: '../specifications/petstore.yaml',
},
},
});
Loading