Skip to content

Commit

Permalink
Remove faker.js (#1527)
Browse files Browse the repository at this point in the history
  • Loading branch information
samselikoff committed Feb 21, 2019
1 parent 9e6d1c7 commit 55bd320
Show file tree
Hide file tree
Showing 17 changed files with 155 additions and 162 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Expand Up @@ -27,7 +27,6 @@ module.exports = {
'no-unused-vars': ['error', { 'args': 'none' }]
},
globals: {
faker: true,
server: true
},
overrides: [
Expand Down
27 changes: 0 additions & 27 deletions addon/faker.js

This file was deleted.

2 changes: 0 additions & 2 deletions addon/index.js
Expand Up @@ -2,7 +2,6 @@ import Factory from './factory';
import trait from './trait';
import association from './association';
import Response from './response';
import faker from './faker';
import Model from './orm/model';
import Collection from './orm/collection';
import Serializer from './serializer';
Expand Down Expand Up @@ -32,7 +31,6 @@ export {
trait,
association,
Response,
faker,
Model,
Collection,
Serializer,
Expand Down
3 changes: 0 additions & 3 deletions index.js
Expand Up @@ -20,9 +20,6 @@ module.exports = {
}),
'fake-xml-http-request': npmAsset({
import: ['fake_xml_http_request.js']
}),
'faker': npmAsset({
import: ['build/build/faker.js']
})
}
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -48,7 +48,6 @@
"ember-get-config": "^0.2.2",
"ember-inflector": "^2.0.0 || ^3.0.0",
"fake-xml-http-request": "^2.0.0",
"faker": "^3.0.0",
"lodash": "^4.17.11",
"pretender": "2.1.1",
"route-recognizer": "^0.3.4"
Expand Down Expand Up @@ -98,6 +97,7 @@
"eslint-plugin-ember": "^5.2.0",
"eslint-plugin-ember-suave": "^1.0.0",
"eslint-plugin-node": "^7.0.1",
"faker": "^4.1.0",
"fastboot": "^1.2.0",
"js-yaml": "^3.12.1",
"jsdom": "^13.1.0",
Expand Down
4 changes: 3 additions & 1 deletion test-projects/01-basic-app/ember-cli-build.js
Expand Up @@ -4,7 +4,9 @@ const EmberApp = require('ember-cli/lib/broccoli/ember-app');

module.exports = function(defaults) {
let app = new EmberApp(defaults, {
// Add options here
autoImport: {
exclude: ['qunit']
}
});

// Use `app.import` to add additional libraries to the generated
Expand Down
6 changes: 6 additions & 0 deletions test-projects/01-basic-app/mirage/factories/user.js
@@ -1,4 +1,10 @@
import { Factory } from 'ember-cli-mirage';
import faker from 'faker';

export default Factory.extend({

age() {
return faker.random.number({ min: 32, max: 32 });
}

});
4 changes: 3 additions & 1 deletion test-projects/01-basic-app/package.json
Expand Up @@ -21,6 +21,7 @@
"@ember/jquery": "*",
"broccoli-asset-rev": "*",
"ember-ajax": "*",
"ember-auto-import": "*",
"ember-cli": "*",
"ember-cli-babel": "*",
"ember-cli-dependency-checker": "*",
Expand All @@ -43,7 +44,8 @@
"jsdom": "*",
"loader.js": "*",
"qunit": "*",
"qunit-dom": "*"
"qunit-dom": "*",
"faker": "*"
},
"engines": {
"node": ">= 8.*"
Expand Down
15 changes: 15 additions & 0 deletions test-projects/01-basic-app/tests/acceptance/faker-test.js
@@ -0,0 +1,15 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';

module('Acceptance | Faker', function(hooks) {
setupTest(hooks);
setupMirage(hooks);

test('it works', function(assert) {
let user = this.server.create('user');

assert.equal(user.age, 32);
});

});
42 changes: 22 additions & 20 deletions tests/dummy/app/pods/docs/advanced/factories/template.md
Expand Up @@ -90,11 +90,13 @@ relationships (as we'll see in a moment):

```js
// mirage/factories/contact.js
import { Factory, faker } from 'ember-cli-mirage';
import { Factory } from 'ember-cli-mirage';

export default Factory.extend({

isAdmin: faker.random.boolean,
isAdmin(i) {
return Math.random() > 0.5;
},

afterCreate(contact, server) {
// Only allow a max of 5 admins to be created
Expand Down Expand Up @@ -172,10 +174,13 @@ You can also use the `afterCreate()` hook (for both `hasMany` and `belongsTo` re

```js
// mirage/factories/author.js
import { Factory, faker } from 'ember-cli-mirage';
import { Factory } from 'ember-cli-mirage';

export default Factory.extend({
firstName: faker.name.firstName,
firstName(i) {
return `Author ${i}`;
},

afterCreate(author, server) {
server.create('post', { author });
}
Expand Down Expand Up @@ -269,11 +274,21 @@ Traits improve your test suite by pulling unnecessary knowledge about data setup

## Using Faker.js

The [Faker.js](https://github.com/marak/Faker.js/) library is included with Mirage, and its methods work nicely with factory definitions:
The [Faker.js](https://github.com/marak/Faker.js/) library pairs nicely with with Mirage factories. It used to be directly included with Mirage, since import npm libraries was a hassle, but with the advent of [Ember Auto Import](https://github.com/ef4/ember-auto-import/issues) it is easier than ever for apps and addons to include it on their own.

To use Faker, make sure your app or addon has Auto Import installed, then install `faker` from npm:

```sh
ember install ember-auto-import
yarn add -D faker
```

Now you can use Faker's methods in your Mirage factories:

```js
// mirage/factories/user.js
import { Factory, faker } from 'ember-cli-mirage';
import { Factory } from 'ember-cli-mirage';
import faker from 'faker';

export default Factory.extend({
firstName() {
Expand All @@ -288,21 +303,8 @@ export default Factory.extend({
});
```

We've also added two methods on the `faker` namespace, `list.cycle` and `list.random`, which are useful if you have a set of data you want your factories to iterate through:

```js
// mirage/factories/subject.js
import { Factory, faker } from 'ember-cli-mirage';

export default Factory.extend({
name: faker.list.cycle('Economics', 'Philosophy', 'English', 'History', 'Mathematics'),
students: faker.list.random(100, 200, 300, 400, 500)
});
```

`cycle` loops through the data in order, while `random` chooses a random element from the list each time an object is created.
You of course can use alternatives to Faker, like [Chance.js](https://chancejs.com/), in a similar way.

View [Faker's docs](https://github.com/marak/Faker.js/) for the full faker API.

## Extending factories

Expand Down
16 changes: 6 additions & 10 deletions tests/dummy/app/pods/docs/getting-started/quickstart/template.md
Expand Up @@ -79,11 +79,11 @@ Let's create a factory for our author with
$ ember g mirage-factory author
```

Mirage also includes the Faker.js library. Let's use this in the Factory and add some properties to it:
We can then define some properties on our Factory. They can be simple types like Booleans, Strings or Numbers, or functions that return dynamic data:

```js
// mirage/factories/author.js
import { Factory, faker } from 'ember-cli-mirage';
import { Factory } from 'ember-cli-mirage';

export default Factory.extend({

Expand All @@ -93,10 +93,8 @@ export default Factory.extend({

age: 28,

admin: false,

avatar() {
return faker.internet.avatar();
admin() {
return Math.random() > 0.5;
}

});
Expand All @@ -108,14 +106,12 @@ This factory creates objects like
[{
name: 'Person 1',
age: 28,
admin: false,
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/bergmartin/128.jpg'
admin: false
},
{
name: 'Person 2',
age: 28,
admin: false,
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/nemanjaivanovic/128.jpg'
admin: true
}]
```

Expand Down
97 changes: 89 additions & 8 deletions tests/dummy/app/pods/docs/getting-started/upgrading/template.md
Expand Up @@ -5,21 +5,102 @@
To install a new version of Mirage, run

```sh
npm install ember-cli-mirage@X.X.X --save-dev
yarn install -D ember-cli-mirage@X.X.X
ember g ember-cli-mirage
```

The `ember g ember-cli-mirage` command ensures all of Mirage's Bower dependencies are added to your project. It runs during `ember install`, and it's always a good idea to run it when upgrading.

Use `ember-cli-mirage@beta` to get the latest beta.

## Changelog

You can view Mirage's full Changelog here:
You can view all of Mirage's release notes on [our Releases page](https://github.com/samselikoff/ember-cli-mirage/releases).

[https://github.com/samselikoff/ember-cli-mirage/blob/master/CHANGELOG.md](https://github.com/samselikoff/ember-cli-mirage/blob/master/CHANGELOG.md)
## 0.4.x → 1.0 Upgrade guide

## 0.3.x > 0.4 Upgrade guide
A few breaking changes were made in the 1.0 release.

**Faker.js**

When Mirage was first released, including npm libraries into Ember CLI apps was difficult. You needed to generate a vendor shim and call `app.import` in order to use the library in your application code.

Because of all this ceremony, it was common for addons to do that work for you, and bundle related packages. This is exactly what Mirage did for [Faker.js](https://github.com/Marak/faker.js), a useful library to have alongside your Mirage factory definitions.

There's a few problems with this, most notably that users _had_ to use the version of Faker that was bundled with Mirage. It was frustrating not being able to take advantage of new Faker features until Mirage upgraded its bundled version.

Now, thanks to [Ember Auto Import](https://github.com/ef4/ember-auto-import), this is no longer the case. Using dependencies directly from npm is painless – just `yarn/npm install` them and `import` them directly from your ES6 classes. Thanks to Auto Import, all that ceremony is taken care of for you.

This also means that users can easily manage their project's version of Faker (and other similar dependencies) independently of the version of Mirage they're using.

For this reason, in 1.0 we are no longer bundling Faker.js with Mirage. This is a breaking change.

Here are the steps you'll need to take to fix this:

1. Install Ember Auto Import (if it's not already installed)

```sh
ember install ember-auto-import
```

2. Install Faker.js directly from npm:

```sh
yarn add -D faker

# or npm install --save-dev faker
```

3. Change all imports of `faker` from the `ember-cli-packge` to import directly from `faker`:

```diff
- import { Factory, faker } from 'ember-cli-mirage';
+ import { Factory } from 'ember-cli-mirage';
+ import faker from 'faker';
```

[There is a codemod](https://github.com/caseywatts/ember-cli-mirage-faker-codemod) that will do this for you, thanks to the gracious work of [Casey Watts](https://github.com/caseywatts).

Additionally, when I originally bundled Faker, I monkey-patched it with some methods that I thought would be "useful" additions. I thought this was a good idea at the time... it wasn't. 🙈

You can look at [the module from v0.4.15](https://github.com/samselikoff/ember-cli-mirage/blob/v0.4.15/addon/faker.js) to see that we added the `faker.list.random`, `faker.list.cycle` and `faker.random.number.range` methods, so if you use these methods too, you'll need to refactor them.

Fortunately, two of them have been added to recent versions of Faker, and one can be replaced with some simple JS:

For `faker.list.random`, use `faker.random.arrayElement`:

```diff
countries() {
- return faker.list.random([ 'United States of America', 'Canada', 'Mexico' ]);
+ return faker.random.arrayElement([ 'United States of America', 'Canada', 'Mexico' ]);
}
```

For `faker.list.cycle`, use the remainder (modulo) operator:

```diff
countries(i) {
- return faker.list.cycle([ 'United States of America', 'Canada', 'Mexico' ]);

+ let countries = [ 'United States of America', 'Canada', 'Mexico' ];
+
+ return countries[i % countries.length];
}
```

For `faker.random.number.range`, use `faker.random.number` with min and max options:

```diff
age() {
- return faker.random.number.range(18, 65);
+ return faker.random.number({ min: 18, max: 65 });
}
```

After that, you should be on your own with respect to Faker! Thanks to Auto Import, you can change versions, or even try out other libraries like [Chance.js](https://chancejs.com/), and rest easy knowing Mirage is a big slimmer and one less thing is beyond your control.



## 0.3.x → 0.4 Upgrade guide

There is one primary change in 0.4 that could break your 0.3 app.

Expand Down Expand Up @@ -90,7 +171,7 @@ export default JSONAPISerializer.extend({
If you do this, I would recommend looking closely at how your real server behaves when serializing resources' relationships and whether it uses resource `links` or resource linkage `data`, and to update your Mirage code accordingly to give you the most faithful representation of your server.


## 0.2.x > 0.3 Upgrade guide
## 0.2.x 0.3 Upgrade guide

The main change from 0.2.x to 0.3.x is that relationships are now one-way. This better matches the semantics of both Ember Data and common HTTP transfer protocols like JSON:API.

Expand Down Expand Up @@ -144,7 +225,7 @@ Conceptually this change should be straightforward, as its making existing impli

For more information on the motivation behind change, please read the [0-3 beta series release blog post](http://www.ember-cli-mirage.com/blog/2017/01/09/0-3-0-beta-series/).

## 0.1.x > 0.2 Upgrade guide
## 0.1.x 0.2 Upgrade guide

If you're upgrading your Mirage server from v0.1.x to v0.2.x, here's what you need to know:

Expand Down Expand Up @@ -272,4 +353,4 @@ If you're upgrading your Mirage server from v0.1.x to v0.2.x, here's what you ne

---

You can always view the [full changelog](https://github.com/samselikoff/ember-cli-mirage/blob/master/CHANGELOG.md) to see everything that's changed. If you think this guide missed a critical part of the upgrade path, please [open an issue](https://github.com/samselikoff/ember-cli-mirage/issues/new) or [help improve it](https://github.com/samselikoff/ember-cli-mirage/edit/gh-pages/docs/v0.3.x/upgrading.md)!
You can always view the [full changelog](https://github.com/samselikoff/ember-cli-mirage/blob/master/CHANGELOG.md) to see everything that's changed. If you think this guide missed a critical part of the upgrade path, please [open an issue](https://github.com/samselikoff/ember-cli-mirage/issues/new)!

0 comments on commit 55bd320

Please sign in to comment.