Skip to content

Commit

Permalink
feat(msw): create an index file exporting msw for split tags mode (#1803
Browse files Browse the repository at this point in the history
)

* feat(msw): create an index file exporting msw for split tags mode

* fix: rename index file

* feat: add setting for indexing mock files

* fix: move setting

* Update docs/src/pages/reference/configuration/output.md

Co-authored-by: Shodai Suzuki <[email protected]>

---------

Co-authored-by: Shodai Suzuki <[email protected]>
  • Loading branch information
AllieJonsson and soartec-lab authored Jan 13, 2025
1 parent 9643099 commit 6938ec7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
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',
},
},
});

0 comments on commit 6938ec7

Please sign in to comment.