Skip to content

Commit b8f599c

Browse files
committed
Add support to mock only specific interfaces
1 parent f7c750d commit b8f599c

File tree

7 files changed

+78
-12
lines changed

7 files changed

+78
-12
lines changed

examples/example.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,26 @@
6464
// favoriteNumber: GreatNumbers;
6565
// }
6666

67-
interface Order {
68-
id: string;
67+
// interface Order {
68+
// id: string;
69+
70+
// /** !mockType {lorem.words} */
71+
// name: string;
72+
// }
73+
74+
// interface User {
75+
// orders: Order[];
76+
// }
6977

70-
/** !mockType {lorem.words} */
78+
79+
interface User {
7180
name: string;
7281
}
7382

74-
interface User {
75-
orders: Order[];
83+
interface Person {
84+
age: number;
85+
}
86+
87+
interface Order {
88+
id: string;
7689
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"clean": "rimraf coverage build tmp",
4141
"coverage": "nyc --reporter=html --reporter=text mocha --reporter mocha-multi-reporters --reporter-options configFile=mocha-config.json --no-colors -r ts-node/register test/**/*.spec.ts",
4242
"ci": "npm run format-check && npm run lint && npm run test",
43-
"cli": "npm run build && node ./build/src/cli/index.js --fixed --files ./examples/example.ts",
43+
"cli": "npm run build && node ./build/src/cli/index.js --fixed --files ./examples/example.ts --interfaces 'Person' 'User'",
4444
"build": "tsc -p tsconfig.json",
4545
"dist": "tsc -p tsconfig.json --outDir ./bin",
4646
"watch": "tsc -w -p tsconfig.json",

src/cli/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const optionDefinitions = [
1212
multiple: true,
1313
defaultOption: true
1414
},
15+
{name: 'interfaces', alias: 'i', type: String, multiple: true},
1516
{name: 'outFile', alias: 'o', type: String},
1617
{name: 'lang', alias: 'l', type: String},
1718
{name: 'help', alias: 'h', type: Boolean},
@@ -83,7 +84,8 @@ function main() {
8384
const isFixedMode = options.fixed;
8485

8586
if (lang === 'typescript') {
86-
const intermock = new IntermockTS({files: options.files, isFixedMode});
87+
const intermock = new IntermockTS(
88+
{files: options.files, interfaces: options.interfaces, isFixedMode});
8789

8890
intermock.generate().then((output: any) => {
8991
console.log(JSON.stringify(output));

src/lang/ts/intermock.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {FileTuple, FileTuples} from '../../lib/types';
1010

1111
export interface Options {
1212
files: string[];
13+
interfaces?: string[];
1314
isFixedMode?: boolean;
1415
isOptionalAlwaysEnabled?: boolean;
1516
}
@@ -33,7 +34,7 @@ export class Intermock {
3334
}
3435

3536
mock(property: string, syntaxType: ts.SyntaxKind, mockType: string) {
36-
const smartMockType = _.get(smartProps, property);
37+
const smartMockType = smartProps[property];
3738
const isFixedMode =
3839
this.options.isFixedMode ? this.options.isFixedMode : false;
3940

@@ -226,6 +227,18 @@ export class Intermock {
226227
child => this.traverseInterfaceMembers(child, output, sourceFile));
227228
}
228229

230+
isSpecificInterface(name: string) {
231+
if (!this.options.interfaces) {
232+
return true;
233+
}
234+
235+
if (!_.includes(this.options.interfaces, name)) {
236+
return false;
237+
}
238+
239+
return true;
240+
}
241+
229242
processFile(sourceFile: ts.SourceFile, output: any, propToTraverse?: string) {
230243
const processNode = (node: ts.Node) => {
231244
switch (node.kind) {
@@ -234,9 +247,12 @@ export class Intermock {
234247
* TODO: Handle interfaces that extend others, via checking hertiage
235248
* clauses
236249
*/
250+
const p = (node as ts.InterfaceDeclaration).name.text;
251+
if (!this.isSpecificInterface(p)) {
252+
return;
253+
}
237254
if (propToTraverse) {
238-
const path = (node as ts.InterfaceDeclaration).name.text;
239-
if (path === propToTraverse) {
255+
if (p === propToTraverse) {
240256
this.traverseInterface(node, output, sourceFile, propToTraverse);
241257
}
242258
} else {
@@ -247,6 +263,10 @@ export class Intermock {
247263
const type = (node as ts.TypeAliasDeclaration).type;
248264
const path = (node as ts.TypeAliasDeclaration).name.text;
249265

266+
if (!this.isSpecificInterface(path)) {
267+
return;
268+
}
269+
250270
if (propToTraverse) {
251271
if (path === propToTraverse) {
252272
this.traverseInterface(type, output, sourceFile, propToTraverse);
@@ -278,6 +298,7 @@ export class Intermock {
278298

279299
processNode(sourceFile);
280300
}
301+
281302
async generate() {
282303
const output: any = {};
283304
const fileContents = await this.readFiles();

src/lib/smart-props.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const smartProps = {
1+
export const smartProps: {[index: string]: string} = {
22
firstName: 'name.firstName',
33
middleName: 'name.firstName',
44
lastName: 'name.lastName',

test/ts/generate.spec.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {expectedFlat} from './test-data/flat';
1212
import {expectedMockType} from './test-data/mockType';
1313
import {expectedNested} from './test-data/nestedSingle';
1414
import {expectedOptional1, expectedOptional2} from './test-data/optional';
15+
import {expectedSpecificInterface} from './test-data/specificInterfaces';
1516
import {expectedTypeAlias} from './test-data/typeAlias';
1617

1718
function runTestCase(
@@ -20,7 +21,11 @@ function runTestCase(
2021
const im = new Intermock(imOptions);
2122

2223
return im.generate().then((output: any) => {
23-
expect(_.get(output, outputProp)).to.deep.equal(expected);
24+
if (outputProp) {
25+
expect(_.get(output, outputProp)).to.deep.equal(expected);
26+
} else {
27+
expect(output).to.deep.equal(expected);
28+
}
2429
});
2530
}
2631

@@ -75,4 +80,10 @@ describe('', () => {
7580
return runTestCase(
7681
`${__dirname}/test-data/array.ts`, 'User', expectedArray1.User);
7782
});
83+
84+
it('should generate mock for specific interfaces', () => {
85+
return runTestCase(
86+
`${__dirname}/test-data/specificInterfaces.ts`, '',
87+
expectedSpecificInterface, {interfaces: ['Person', 'User']});
88+
});
7889
});
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
interface User {
2+
name: string;
3+
}
4+
5+
interface Person {
6+
age: number;
7+
}
8+
9+
interface Order {
10+
id: string;
11+
}
12+
13+
export const expectedSpecificInterface = {
14+
'User': {
15+
'name':
16+
'Animi repellat eveniet eveniet dolores quo ullam rerum reiciendis ipsam. Corrupti voluptatem ipsa illum veritatis eligendi sit autem ut quia. Ea sint voluptas impedit ducimus dolores possimus.'
17+
},
18+
'Person': {'age': 86924}
19+
};

0 commit comments

Comments
 (0)