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

refactor: split into logical packages #46

Merged
merged 2 commits into from
Jul 15, 2021
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
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ node_modules/*
coverage/*
dist
dist/*
jest.config.js
jest.config.js
commitlint.config.js
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ lerna-debug.log*
junit.xml
.gitconfig
.circleci
!.husky
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn commitlint --edit $1
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Install the package alongside `faker` and `@types/faker` peer dependencies:

```bash
npm i mockingbird faker @types/faker
npm i generator faker @types/faker
```

## Usage
Expand Down
17 changes: 17 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const {
utils: { getPackages },
} = require('@commitlint/config-lerna-scopes');

module.exports = {
extends: ['@commitlint/config-conventional', '@commitlint/config-lerna-scopes'],
rules: {
'scope-enum': async (ctx) => [
2,
'always',
[
...(await getPackages(ctx)),
'release',
],
],
},
};
6 changes: 6 additions & 0 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
"version": "independent",
"command": {
"publish": {
"allowBranch": ["master"],
"conventionalCommits": true
}
},
"useWorkspaces": true,
"npmClient": "yarn",
"packages": [
Expand Down
15 changes: 2 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@
"dependencies": {
"@commitlint/cli": "^12.1.4",
"@commitlint/config-conventional": "^12.1.4",
"@commitlint/config-lerna-scopes": "^12.1.4",
"@manypkg/cli": "^0.18.0",
"@types/faker": "^5.5.6",
"@types/faker": "^5.5.7",
"@types/jest": "26.0.24",
"@types/node": "^12.20.15",
"@typescript-eslint/eslint-plugin": "^4.28.2",
Expand Down Expand Up @@ -90,23 +91,11 @@
"eslint --ext .ts --fix"
]
},
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
"pre-commit": "lint-staged",
"pre-push": "npm test"
}
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"commitlint": {
"extends": [
"@commitlint/config-conventional"
]
},
"workspaces": [
"packages/*"
]
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions packages/generator/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
moduleFileExtensions: ['js', 'json', 'ts'],
rootDir: '.',
testRegex: '.test.ts$',
transform: {
'^.+\\.ts$': 'ts-jest',
},
testEnvironment: 'node',
};
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,12 @@
"index.js",
"README.md"
],
"peerDependencies": {
"@types/faker": "^5.0.0 || ^4.0.0",
"faker": "^5.0.0 || ^4.0.0"
},
"dependencies": {
"@plumier/reflect": "^1.0.0-rc.5",
"@plumier/reflect": "^1.0.3",
"reflect-metadata": "^0.1.13",
"tslib": "^2.3.0"
},
"devDependencies": {
"@types/faker": "^5.5.6",
"faker": "^5.5.3",
"jest": "27.0.6",
"rimraf": "^3.0.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/generator/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './lib/factories';
export * from './types/mock-decorator-factory-options.interface';
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import faker from 'faker';
import { ClassProcessor } from '../lib/class-processor';
import { Class } from '../types/mock-options.type';
import { MockDecoratorFactoryOptions } from '../types';
import { ClassReflector } from '../lib/class-reflector';
import { Class, Faker } from '@mockinbird/types';
import { ClassProcessor } from '@mockinbird/parser';
import { ClassReflector } from '@mockinbird/reflect';
import { MockDecoratorFactoryOptions } from '../../types/mock-decorator-factory-options.interface';

export class MockGenerator {
/**
Expand Down Expand Up @@ -46,7 +45,7 @@ export class MockGenerator {
options?: MockDecoratorFactoryOptions
): TClass | TClass[] {
const { count = 1, locale = ClassProcessor.DEFAULT_LOCALE } = options || {};
const processor = new ClassProcessor<TClass>(faker, new ClassReflector(), locale);
const processor = new ClassProcessor<TClass>(Faker, new ClassReflector(), locale);

if (!count || count === 1) {
return processor.process(targetClass);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MockGenerator } from '../factories';
import { Class, ClassLiteral, GeneratedMock } from '../types';
import { Class, ClassLiteral } from '@mockinbird/types';
import { GeneratedMock } from '@mockinbird/reflect';
import { MockPersistent } from './mock-persistent';
import { MockGenerator } from './factories';

export class MockBuilder<TClass extends Class> {
private doPersist = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import { Class } from '../types';
import * as fs from 'fs';
import { Class } from '@mockinbird/types';
import { MockBuilder } from './mock-builder';
import { MockPersistent } from './mock-persistent';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fileSystem from 'fs';
import { GeneratedMock } from '../types';
import { GeneratedMock } from '@mockinbird/reflect';

export interface PersistentDto {
baseMock: GeneratedMock;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Faker } from '@mockinbird/types';

export interface MockDecoratorFactoryOptions {
count: number;
locale?: Faker['locale'];
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Mock } from '../src/decorators/mock.decorator';
import { MockGenerator } from '../src/factories/mock-generator';
import { Mock } from '@mockinbird/reflect';
import { MockGenerator } from '../src/lib/factories/mock-generator';

describe('Mock Factory - circular class-type', () => {
describe('with single class circular mock', () => {
Expand Down
File renamed without changes.
18 changes: 0 additions & 18 deletions packages/mockingbird/jest.config.js

This file was deleted.

11 changes: 0 additions & 11 deletions packages/mockingbird/src/handlers/abstract-value-handler.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/mockingbird/src/types/index.ts

This file was deleted.

This file was deleted.

3 changes: 3 additions & 0 deletions packages/parser/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../.eslintrc"
}
Empty file added packages/parser/CHANGELOG.md
Empty file.
Empty file added packages/parser/README.md
Empty file.
1 change: 1 addition & 0 deletions packages/parser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src';
9 changes: 9 additions & 0 deletions packages/parser/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
moduleFileExtensions: ['js', 'json', 'ts'],
rootDir: '.',
testRegex: '.test.ts$',
transform: {
'^.+\\.ts$': 'ts-jest',
},
testEnvironment: 'node',
};
46 changes: 46 additions & 0 deletions packages/parser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@mockinbird/parser",
"version": "1.0.0",
"license": "MIT",
"description": "Mockingbird Parser Package",
"contributors": [
{
"name": "Omer Morad",
"email": "omer.moradd@gmail.com"
},
{
"name": "Idan Ptichi",
"email": "idanpt@gmail.com"
}
],
"repository": {
"type": "git",
"url": "https://github.com/omermorad/mockingbird-ts.git"
},
"bugs": {
"url": "https://github.com/omermorad/mockingbird-ts/issues"
},
"readme": "https://github.com/omermorad/mockingbird-ts/blob/master/packages/core/README.md",
"scripts": {
"build": "tsc",
"watch": "tsc --watch",
"test": "jest --runInBand --verbose",
"lint": "eslint '{src,test}/**/*.ts'",
"lint:fix": "eslint '{src,test}/**/*.ts' --fix"
},
"files": [
"index.ts"
],
"dependencies": {
"@mockinbird/reflect": "^1.0.0"
},
"devDependencies": {
"@mockinbird/types": "^1.0.0",
"jest": "27.0.6",
"ts-jest": "^27.0.3",
"ts-loader": "^6.2.2",
"ts-node": "8.10.2",
"tsconfig-paths": "^3.9.0",
"typescript": "^3.9.7"
}
}
6 changes: 6 additions & 0 deletions packages/parser/src/handlers/abstract-value-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Class, Faker } from '@mockinbird/types';
import { ClassProcessor } from '../lib/class-processor';

export class AbstractValueHandler {
public constructor(protected readonly faker?: Faker, protected readonly classProcessor?: ClassProcessor<Class>) {}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Property, PropertyDecoratorValue } from '@mockinbird/reflect';
import { ArrayValueHandler } from './array-value-handler';
import { ClassProcessor } from '../lib/class-processor';
import { MultiClass } from '../types/mock-options.type';
import FakerStatic = Faker.FakerStatic;
import { Property } from '../lib/property';
import { PropertyDecoratorValue } from '../lib/property-decorator-value';
import { Faker, MultiClass } from '@mockinbird/types';

describe('ArrayValueHandler Unit', () => {
const DTO_CLASS_VALUE = class TestClass {};
Expand All @@ -15,10 +12,6 @@ describe('ArrayValueHandler Unit', () => {
return new Property('testPropertyName', 'TestClass', new PropertyDecoratorValue(mockValue));
}

const classProcessorMock = {
process: jest.fn(),
} as unknown as ClassProcessor<any>;

const fakerMock = {
random: {
alpha: jest.fn(),
Expand All @@ -29,11 +22,11 @@ describe('ArrayValueHandler Unit', () => {
date: {
recent: jest.fn(),
},
} as unknown as FakerStatic;
} as unknown as Faker;

describe('given a ArrayValueHandler', () => {
beforeAll(() => {
handler = new ArrayValueHandler(fakerMock, classProcessorMock);
handler = new ArrayValueHandler();
});

describe("when calling 'shouldHandle' with type 'object' and decoratorValue of multi class ({ type: ClassType })", () => {
Expand Down Expand Up @@ -102,12 +95,12 @@ describe('ArrayValueHandler Unit', () => {
});
});
});

describe('and decoratorValue type is an actual class (none a primitive)', () => {
test("then call 'process' with 'count' times", () => {
handler.produceValue(createProperty({ type: DTO_CLASS_VALUE, count: DEFAULT_COUNT_FOR_DTO }));
expect(classProcessorMock.process).toHaveBeenCalledTimes(DEFAULT_COUNT_FOR_DTO);
});
});
//
// describe('and decoratorValue type is an actual class (not a primitive)', () => {
// test("then call 'process' with 'count' times", () => {
// handler.produceValue(createProperty({ type: DTO_CLASS_VALUE, count: DEFAULT_COUNT_FOR_DTO }));
// expect(classProcessorMock.process).toHaveBeenCalledTimes(DEFAULT_COUNT_FOR_DTO);
// });
// });
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ValueHandler } from '../types/value-handler.interface';
import { ExactValue, MultiClass } from '../types/mock-options.type';
import { Property } from '@mockinbird/reflect';
import { ExactValue, MultiClass } from '@mockinbird/types';
import { PrimitiveHandlerAbstract } from './primitive-handler-abstract';
import { Property } from '../lib/property';
import { ValueHandler } from '../types/value-handler.interface';
import { isPrimitive } from '../common/is-primitive';

// TODO: refactor (2nd phase). All other mock options should be wrapped with 'multiple' functionality
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Property, PropertyDecoratorValue } from '@mockinbird/reflect';
import { Faker } from '@mockinbird/types';
import { CallbackValueHandler } from './callback-value-handler';
import FakerStatic = Faker.FakerStatic;
import { Property } from '../lib/property';
import { PropertyDecoratorValue } from '../lib/property-decorator-value';

describe('CallbackValueInspector Unit', () => {
let handler: CallbackValueHandler;

const fakerMock = { internet: { email: jest.fn() } } as unknown as FakerStatic;
const fakerMock = { internet: { email: jest.fn() } } as unknown as Faker;

describe('given a CallbackValueInspector', () => {
beforeAll(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ValueHandler } from '../types/value-handler.interface';
import { Property } from '../lib/property';
import { Callback } from '../types/mock-options.type';
import { Property } from '@mockinbird/reflect';
import { Callback } from '@mockinbird/types';
import { AbstractValueHandler } from './abstract-value-handler';
import { ValueHandler } from '../types/value-handler.interface';

export class CallbackValueHandler extends AbstractValueHandler implements ValueHandler {
public shouldHandle(property: Property): boolean {
Expand Down
Loading