Skip to content

Commit 0b69f0f

Browse files
huseyinbuyukderehuseyinbuyukdere
and
huseyinbuyukdere
authored
feat: add includedTypes and excludedTypes options for selective mock generation (#179)
This update provides flexibility for users to selectively include or exclude types, making mock data generation more efficient and tailored to specific testing needs. --------- Co-authored-by: huseyinbuyukdere <huseyin_buyukdere@epam.com>
1 parent 1a04833 commit 0b69f0f

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,34 @@ Adds `__typename` property to mock data
2626

2727
Changes enums to TypeScript string union types
2828

29+
### includedTypes (`string[]`, defaultValue: `undefined`)
30+
31+
Specifies an array of types to **include** in the mock generation. When provided, only the types listed in this array will have mock data generated.
32+
33+
Example:
34+
35+
```yaml
36+
plugins:
37+
- typescript-mock-data:
38+
includedTypes:
39+
- User
40+
- Avatar
41+
```
42+
43+
### excludedTypes (`string[]`, defaultValue: `undefined`)
44+
45+
Specifies an array of types to **exclude** in the mock generation. When provided, the types listed in this array will not have mock data generated.
46+
47+
Example:
48+
49+
```yaml
50+
plugins:
51+
- typescript-mock-data:
52+
excludedTypes:
53+
- User
54+
- Avatar
55+
```
56+
2957
### terminateCircularRelationships (`boolean | 'immediate'`, defaultValue: `false`)
3058

3159
When enabled, prevents circular relationships from triggering infinite recursion. After the first resolution of a

src/index.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,8 @@ export interface TypescriptMocksPluginConfig {
588588
defaultNullableToNull?: boolean;
589589
useTypeImports?: boolean;
590590
typeNamesMapping?: Record<string, string>;
591+
includedTypes?: string[];
592+
excludedTypes?: string[];
591593
}
592594

593595
interface TypeItem {
@@ -835,7 +837,24 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
835837
// run on the types first
836838
oldVisit(astNode, { leave: typeVisitor });
837839
const result = oldVisit(astNode, { leave: visitor });
838-
const definitions = result.definitions.filter((definition: any) => !!definition);
840+
841+
const { includedTypes, excludedTypes } = config;
842+
const shouldGenerateMockForType = (typeName?: string) => {
843+
if (!typeName) {
844+
return true;
845+
}
846+
if (includedTypes && includedTypes.length > 0) {
847+
return includedTypes.includes(typeName);
848+
}
849+
if (excludedTypes && excludedTypes.length > 0) {
850+
return !excludedTypes.includes(typeName);
851+
}
852+
return true;
853+
};
854+
855+
const definitions = result.definitions.filter(
856+
(definition: any) => !!definition && shouldGenerateMockForType(definition.typeName),
857+
);
839858
const typesFile = config.typesFile ? config.typesFile.replace(/\.[\w]+$/, '') : null;
840859

841860
const typesFileImport = getImportTypes({

tests/__snapshots__/typescript-mock-data.spec.ts.snap

+26
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,32 @@ export const aQuery = (overrides?: Partial<Query>): Query => {
17141714
"
17151715
`;
17161716

1717+
exports[`should generate mock data only for included types 1`] = `
1718+
"
1719+
export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
1720+
return {
1721+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1550ff93-cd31-49b4-a3c3-8ef1cb68bdc3',
1722+
url: overrides && overrides.hasOwnProperty('url') ? overrides.url! : 'consectetur',
1723+
};
1724+
};
1725+
1726+
export const aUser = (overrides?: Partial<User>): User => {
1727+
return {
1728+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 'b5756f00-51a6-422a-81a7-dc13ee6a6375',
1729+
creationDate: overrides && overrides.hasOwnProperty('creationDate') ? overrides.creationDate! : '2021-06-27T14:29:24.774Z',
1730+
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'sordeo',
1731+
avatar: overrides && overrides.hasOwnProperty('avatar') ? overrides.avatar! : anAvatar(),
1732+
status: overrides && overrides.hasOwnProperty('status') ? overrides.status! : Status.Online,
1733+
customStatus: overrides && overrides.hasOwnProperty('customStatus') ? overrides.customStatus! : AbcStatus.HasXyzStatus,
1734+
scalarValue: overrides && overrides.hasOwnProperty('scalarValue') ? overrides.scalarValue! : 'arx',
1735+
camelCaseThing: overrides && overrides.hasOwnProperty('camelCaseThing') ? overrides.camelCaseThing! : aCamelCaseThing(),
1736+
unionThing: overrides && overrides.hasOwnProperty('unionThing') ? overrides.unionThing! : anAvatar(),
1737+
prefixedEnum: overrides && overrides.hasOwnProperty('prefixedEnum') ? overrides.prefixedEnum! : PrefixedEnum.PrefixedValue,
1738+
};
1739+
};
1740+
"
1741+
`;
1742+
17171743
exports[`should generate mock data with PascalCase enum values by default 1`] = `
17181744
"
17191745
export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {

tests/typescript-mock-data.spec.ts

+36
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,39 @@ it('overriding works as expected when defaultNullableToNull is true', async () =
612612

613613
expect(result).toMatchSnapshot();
614614
});
615+
616+
it('should generate mock data only for included types', async () => {
617+
const result = await plugin(testSchema, [], {
618+
includedTypes: ['User', 'Avatar'],
619+
});
620+
621+
expect(result).toMatchSnapshot();
622+
expect(result).toBeDefined();
623+
expect(result).toContain('export const aUser');
624+
expect(result).toContain('export const anAvatar');
625+
expect(result).not.toContain('export const aPrefixedResponse');
626+
expect(result).not.toContain('export const aCamelCaseThing');
627+
});
628+
629+
it('should exclude specified types from mock generation', async () => {
630+
const result = await plugin(testSchema, [], {
631+
excludedTypes: ['User', 'Avatar'],
632+
});
633+
634+
expect(result).toBeDefined();
635+
expect(result).not.toContain('export const aUser');
636+
expect(result).not.toContain('export const anAvatar');
637+
expect(result).toContain('export const aPrefixedResponse');
638+
expect(result).toContain('export const aCamelCaseThing');
639+
});
640+
641+
it('should prioritize includedTypes over excludedTypes if both are specified', async () => {
642+
const result = await plugin(testSchema, [], {
643+
includedTypes: ['User'],
644+
excludedTypes: ['User', 'Avatar'],
645+
});
646+
expect(result).toBeDefined();
647+
expect(result).toContain('export const aUser');
648+
expect(result).not.toContain('export const anAvatar');
649+
expect(result).not.toContain('export const aPrefixedResponse');
650+
});

0 commit comments

Comments
 (0)