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

Add jest matcher #150

Merged
merged 7 commits into from
May 11, 2018
Merged

Add jest matcher #150

merged 7 commits into from
May 11, 2018

Conversation

a-xin
Copy link
Contributor

@a-xin a-xin commented May 9, 2018

Adding jest matchers to conveniently use targaryen with jest.

See #129

Usage:

const { getDatabase, toAllowRead, toAllowUpdate, toAllowWrite, json } = require('targaryen/plugins/jest');

expect.extend({
  toAllowRead,
  toAllowUpdate,
  toAllowWrite,
});

const RULES_PATH = 'database.rules.json';
const rules = json.loadSync(RULES_PATH);
const initialData = {};

const database = getDatabase(rules, initialData);

test('basic', () => {
  expect(database.as(null)).not.toAllowRead('/user');
  expect(database.as(null)).toAllowRead('/public');
  expect(database.as({ uid: '1234'})).toAllowWrite('/user/1234', {
    name: 'Anna',
  });
});

or using the generic matcher:

const { getDebugDatabase, toBeAllowed, json } = require('targaryen/plugins/jest');

expect.extend({
  toBeAllowed,
});

const RULES_PATH = 'database.rules.json';
const rules = json.loadSync(RULES_PATH);
const initialData = {};

// NOTE: Create a database with debug set to true for detailed errors
const database = getDebugDatabase(rules, initialData);

test('generic', () => {
  expect(database.as(null).read('/user')).not.toBeAllowed();
  expect(database.as({ uid: '1234' }).write('/user/1234', {
    name: 'Anna',
  })).toBeAllowed();
});

Adding jest matchers to conveniently use targaryen with jest.

Usage:
```
const { getDatabase, toAllowRead, toAllowUpdate, toAllowWrite, json } = require('targaryen/plugins/jest');

expect.extend({
  toAllowRead,
  toAllowUpdate,
  toAllowWrite,
});

const RULES_PATH = 'database.rules.json';
const rules = json.loadSync(RULES_PATH);
const initialData = {};

const database = getDatabase(rules, initialData);

test('basic', () => {
  expect(database.as(null)).not.toAllowRead('/user');
  expect(database.as(null)).toAllowRead('/public');
  expect(database.as({ uid: '1234'})).toAllowWrite('/user/1234', {
    name: 'Anna',
  });
});

```

or using the generic matcher:
```
const { getDebugDatabase, toBeAllowed, json } = require('targaryen/plugins/jest');

expect.extend({
  toBeAllowed,
});

const RULES_PATH = 'database.rules.json';
const rules = json.loadSync(RULES_PATH);
const initialData = {};

// NOTE: Create a database with debug set to true for detailed errors
const database = getDebugDatabase(rules, initialData);

test('generic', () => {
  expect(database.as(null).read('/user')).not.toBeAllowed();
  expect(database.as({ uid: '1234' }).write('/user/1234', {
    name: 'Anna',
  })).toBeAllowed();
});

```
@a-xin
Copy link
Contributor Author

a-xin commented May 9, 2018

@dinoboff What do you think of the approach and the interfaces? Happy to add tests and docs if the code looks fine

@coveralls
Copy link

Coverage Status

Coverage remained the same at 95.404% when pulling 4f3fc3b on a-xin:add-jest-matcher into f3281a0 on goldibex:master.

@coveralls
Copy link

coveralls commented May 9, 2018

Coverage Status

Coverage remained the same at 95.404% when pulling 396bd0d on a-xin:add-jest-matcher into f3281a0 on goldibex:master.

@dinoboff
Copy link
Collaborator

dinoboff commented May 9, 2018

Thanks, I like the interface. I will include once there are integration test and documentation similar to the Jasmine plugin.

Note that targaryen still support node 4.

@dinoboff
Copy link
Collaborator

dinoboff commented May 9, 2018

Also, in v4, I might move the plugins to their own packages. Core and plugins will still be hosted together in that repo.

@a-xin
Copy link
Contributor Author

a-xin commented May 9, 2018

Good shout about node 4! With jest supporting only 6+ out of the box, it would make sense to put the plugin into a separate package right away and have it require node 6+.
Otherwise, as it's not the plugin itself but only its tests that will require node 6+ I could add them to this PR and make the jest tests only run when on a supported node version (as a temporary hack until the plugins get extracted)

Copy link
Collaborator

@dinoboff dinoboff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good.

Thanks!

.travis.yml Outdated
@@ -11,3 +11,9 @@ script:
fi
- npm run coveralls
- npm run lint
- >
if [[ $(node --version | cut -c 2-) < 6 ]]; then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please change the version test. Either use grep or change the previous test to use cut. cut is probably better.

Using grep is less future-proof but the better option, as comparing
using `<` can be incorrect.
@dinoboff dinoboff merged commit bb579bc into goldibex:master May 11, 2018
@dinoboff dinoboff added this to the 3.1.0 milestone May 13, 2018
@dinoboff
Copy link
Collaborator

Thanks @a-xin. Published with 3.1.0.

@a-xin a-xin deleted the add-jest-matcher branch May 13, 2018 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants