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

chore(repo): update docs and sample #62

Merged
merged 1 commit into from
Jul 23, 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
64 changes: 34 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
[![ISC license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
[![npm version](http://img.shields.io/npm/v/mockingbird-ts.svg?style=flat)](https://npmjs.org/package/mockingbird-ts "View this project on npm")
[![Codecov Coverage](https://img.shields.io/codecov/c/github/omermorad/mockingbird-ts/master.svg?style=flat-square)](https://codecov.io/gh/omermorad/mockingbird-ts)
[![Master](https://github.com/omermorad/mockingbird-ts/actions/workflows/release.yml/badge.svg?branch=release)](https://github.com/omermorad/mockingbird-ts/actions)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lerna.js.org/)

<p align="center">
<img width="450" src="https://raw.githubusercontent.com/omermorad/mockingbird-ts/master/docs/logo.png" alt="Mockingbird Logo" />

<h1 align="center">Mockingbird</h1>

<h3 align="center">
Decorator Powered TypeScript Library for Creating Mocks
The First TypeScript Mocking Framework
</h3>

<h4 align="center">
Expand All @@ -19,10 +18,9 @@
</p>

## Installation
Install the package alongside `faker` and `@types/faker` peer dependencies:

```bash
npm i generator faker @types/faker
npm i -D mockingbird-ts
```

## Usage
Expand All @@ -35,58 +33,64 @@ import { Mock, MockFactory } from 'mockingbird-ts';
class Dog {
@Mock(faker => faker.name.firstName())
readonly name: string;

@Mock()
readonly birthday: Date; // Will generate a recent date

@Mock()
readonly goodPoints: number; // Will generate a random number
}

const result = MockFactory.create<Dog>(Dog);
const oneDog = MockFactory(Dog).one();
const lotsOfDogs = MockFactory(Dog).many(3);
```

## Documentation

There are many more options that you can use with the `@Mock` decorator (and also the `MockFactory`). \
[Click here to jump to the full documentation and explore the full API](https://github.com/omermorad/faker.ts/blob/master/docs/README.md)

**Besides, we have also created a full working example for you; [you can find it under
the sample folder](https://github.com/omermorad/mockingbird-ts/tree/master/sample)**

**Besides, we have also created a full working example for
you; [you can find it under the sample folder](https://github.com/omermorad/mockingbird-ts/tree/master/sample)**

## Playground
**We have created a [REPL Playground](https://repl.it/@omermorad/Mockingbird-Playground) where you can see Mockingbird in action!**

**We have created a [REPL Playground](https://repl.it/@omermorad/Mockingbird-Playground) where you can see Mockingbird
in action!**

## Use Cases
- Generate fake (but reasonable) data for your integration tests
- Prepare as many of different fixtures for your tests
- Persist the

## Motivation
When it comes to writing unit tests of large projects containing different and
diverse entities, mocks are widely used to simulate real data.

Creating mocks can be a tedious and cumbersome process and is usually created
manually or by using libraries like Faker or Chance, which also do not offer a complete solution,
especially not when deciding to develop in TypeScript and most of the code becomes object oriented.
When it comes to writing tests of large projects containing different and diverse entities, mocks (sometimes called "
fixtures") are widely used to simulate real data.

Therefore, we thought of a convenient and efficient solution that allows the use
of only one decorator, Mock decorator that allows to create mocks by placing it above the properties of the class.
Creating mocks (or fixtures) can be a tedious and cumbersome process and is usually created manually or by using
libraries like Faker or Chance, which also do not offer a complete solution, especially not when you write TypeScript
only, and most of the code is object-oriented and arranged with classes.

Mockingbird offers several options for creating mocks, including the use of the
well-known library Faker, which allows you to create information such as a fake email, a fake username,
a fake address and more.
We came up with a simple (and efficient) yet super convenient solution: all you have to do to get fixtures out of the
box is to decorate your classes (whether it's an entity, or a model representing the database layer) and generate simple
or complex fixtures.

### What is faker.js (aka Faker)?
For those of you who are unfamiliar with `faker.js`, it is an old library written
with pure JavaScript, which is used to "generate massive amounts of fake data in
the browser and Node".
Mockingbird offers several options for creating mocks, including the use of the well-known library Faker, which allows
you to create data such as a fake email, a fake username, a fake address and more.u to create information such as a fake
email, a fake username, a fake address and more.

Fake data is usually needed for testing purposes, to assist in the development process itself,
and sometimes, also for the purpose of demonstrations and training.
### What is `faker.js` (aka Faker)?

Mockingbird uses Faker under the hood and making it possible to use faker.js in
a "TypeScript" way, and thereby allows to create mocks that are meaningful like
email, first name, address and many more.
For those of you who are unfamiliar with `faker.js`, it is a library which is used to "generate massive amounts of fake data in the browser and Node".

Mockingbird uses `faker.js` under the hood, making it possible to use Faker in it's "TypeScript" way, and thereby allows
to create mocks that are meaningful like email, first name, address and many more.

## License

Distributed under the MIT License. See `LICENSE` for more information.

## Acknowledgements

[faker.js](https://github.com/marak/Faker.js)
122 changes: 98 additions & 24 deletions packages/generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</p>

## Installation

```bash
npm i -D mockingbird-ts
```
Expand All @@ -32,7 +33,7 @@ import { Mock, MockFactory } from 'mockingbird-ts';
class Dog {
@Mock(faker => faker.name.firstName())
readonly name: string;

@Mock()
readonly birthday: Date; // Will generate a recent date

Expand All @@ -49,42 +50,115 @@ const lotsOfDogs = MockFactory(Dog).many(3);
There are many more options that you can use with the `@Mock` decorator (and also the `MockFactory`). \
[Click here to jump to the full documentation and explore the full API](https://github.com/omermorad/faker.ts/blob/master/docs/README.md)

**Besides, we have also created a full working example for you; [you can find it under
the sample folder](https://github.com/omermorad/mockingbird-ts/tree/master/sample)**

**Besides, we have also created a full working example for
you; [you can find it under the sample folder](https://github.com/omermorad/mockingbird-ts/tree/master/sample)**

## Playground
**We have created a [REPL Playground](https://repl.it/@omermorad/Mockingbird-Playground) where you can see Mockingbird in action!**

**We have created a [REPL Playground](https://repl.it/@omermorad/Mockingbird-Playground) where you can see Mockingbird
in action!**

## Use Cases
- Generate fake (but reasonable) data for your integration tests
- Generate fake (but reasonable) data for your seeding a database
- Prepare as many unique fixtures as you need for your tests

#### How can Mockingbird help me?
Consider the following snippets:

**`dog-model.ts`**

```typescript
import { Mock, MockFactory } from 'mockingbird-ts';

export interface DogModel {
name: string;
birthday: Date;
goodPoints: number;
}

export class DogModel {
@Mock(faker => faker.name.firstName())
name: string;

// Will generate a recent date
@Mock()
birthday: Date;

// Will generate a random number
@Mock()
goodPoints: number;
}
```

**`dogs-integration.test.ts`**

```typescript
import { DogsApiService } from './dogs-service';
import { DogModel } from './dog-model';

describe('Dogs API Integration Test', () => {
// Assume we have dogs-service.ts that fetches from some API
const apiService: jest.Mocked<DogsApiService> = {
fetch: jest.fn(),
};

let dogs: DogModel[];

beforeAll(() => {
// Persist will return the same dogs every time
dogsMockRef = MockFactory<DogModel>(DogModel).persist();
});

test('Test something you want', async () => {
dogs = dogsMockRef.many(3); // Generate 3 different dogs
apiService.fetch.mockResolvedValue(dogs);

const resultFromApi = await apiService.fetch();
expect(resultFromApi).toEqual(dogs);
});

test('Test something else you want', async () => {
dogsWithZeroPoints = dogsMockRef.stub({ goodPoints: 0 }).many(3);
apiService.fetch.mockResolvedValue(dogsWithZeroPoints);

const resultFromApi = await apiService.fetch();

expect(resultFromApi).toEqual(dogsWithZeroPoints);
expect(dogsWithZeroPoints[0].goodPoints).toBe(0);
});
});
```


## Motivation
When it comes to writing unit tests of large projects containing different and
diverse entities, mocks are widely used to simulate real data.

Creating mocks can be a tedious and cumbersome process and is usually created
manually or by using libraries like Faker or Chance, which also do not offer a complete solution,
especially not when deciding to develop in TypeScript and most of the code becomes object oriented.
When it comes to writing tests of large projects containing different and diverse entities, mocks (sometimes called "
fixtures") are widely used to simulate real data.

Creating mocks (or fixtures) can be a tedious and cumbersome process and is usually created manually or by using
libraries like Faker or Chance, which also do not offer a complete solution, especially not when you write TypeScript
only, and most of the code is object-oriented and arranged with classes.

Therefore, we thought of a convenient and efficient solution that allows the use
of only one decorator, Mock decorator that allows to create mocks by placing it above the properties of the class.
We came up with a simple (and efficient) yet super convenient solution: all you have to do to get fixtures out of the
box is to decorate your classes (whether it's an entity, or a model representing the database layer) and generate simple
or complex fixtures.

Mockingbird offers several options for creating mocks, including the use of the
well-known library Faker, which allows you to create information such as a fake email, a fake username,
a fake address and more.
Mockingbird offers several options for creating mocks, including the use of the well-known library Faker, which allows
you to create data such as a fake email, a fake username, a fake address and more.u to create information such as a fake
email, a fake username, a fake address and more.

### What is faker.js (aka Faker)?
For those of you who are unfamiliar with `faker.js`, it is an old library written
with pure JavaScript, which is used to "generate massive amounts of fake data in
the browser and Node".
### What is `faker.js` (aka Faker)?

Fake data is usually needed for testing purposes, to assist in the development process itself,
and sometimes, also for the purpose of demonstrations and training.
For those of you who are unfamiliar with `faker.js`, it is a library which is used to "generate massive amounts of fake data in the browser and Node".

Mockingbird uses Faker under the hood and making it possible to use faker.js in
a "TypeScript" way, and thereby allows to create mocks that are meaningful like
email, first name, address and many more.
Mockingbird uses `faker.js` under the hood, making it possible to use Faker in it's "TypeScript" way, and thereby allows
to create mocks that are meaningful like email, first name, address and many more.

## License

Distributed under the MIT License. See `LICENSE` for more information.

## Acknowledgements

[faker.js](https://github.com/marak/Faker.js)
9 changes: 9 additions & 0 deletions sample/mockingbird-typeorm/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',
};
2 changes: 1 addition & 1 deletion sample/mockingbird-typeorm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"body-parser": "^1.19.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mockingbird-ts": "^1.1.0-next.1",
"mockingbird-ts": "^2.0.0-alpha.2",
"mysql2": "^2.2.5",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
Expand Down
2 changes: 1 addition & 1 deletion sample/mockingbird-typeorm/src/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function UserController(router: Router, repository: Repository<User>): Ro
});

router.get('/users/random', async (req: Request, res: Response) => {
const newUser = MockFactory.create(UserEntity);
const newUser = MockFactory(UserEntity).one();
const user = await repository.create(newUser);
const result = await repository.save(user);

Expand Down
2 changes: 1 addition & 1 deletion sample/mockingbird-typeorm/test/app-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('Users App e2e Test', () => {
})) as Connection;

app = await applicationFactory(connection);
seededData = MockFactory.create<User>(UserEntity, { count: 3 });
seededData = MockFactory<User>(UserEntity).toPlain().many(3);

await connection.getRepository(UserEntity).insert(seededData);
});
Expand Down