Skip to content

Commit

Permalink
Add linting, unit tests and ignoreExtraArrayItems & Travis integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Meinaart van Straalen committed Oct 16, 2018
1 parent 82f7242 commit 8339813
Show file tree
Hide file tree
Showing 19 changed files with 523 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package-lock.json
.DS_Store
node_modules
node_modules
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/node_modules/
package-lock.json
.travis.yml
__tests__
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: node_js

node_js:
- 10.8
cache:
directories:
- ~/.npm
- ~/.cache
notifications:
email: false
install:
- npm ci
script:
- npm run lint
- npm test
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
1.0.2 / 2018-10-16
==================

* Add `ignoreExtraArrayItems` property to configuration
* Add [Travis](https://travis-ci.org/) integration
* Add linter config
* Add Jest unit tests

1.0.1 / 2018-10-15
==================

Expand Down
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Build Status][build-badge]][build]

# cypress-plugin-snapshots
Plugin for snapshot tests in [Cypress.io](https://www.cypress.io/).

Expand Down Expand Up @@ -66,16 +68,17 @@ Add the configuration below to your `cypress.json` file to make changes to the d
```javascript
"env": {
"cypress-plugin-snapshots": {
"autoCleanUp": false, // Automatically remove snapshots that are not used by test
"autopassNewSnapshots": true, // Automatically save & pass new/non-existing snapshots
"diffLines": 3, // How many lines to include in the diff modal
"excludeFields": [], // Array of fieldnames that should be excluded from snapshot
"ignoreExtraFields": false, // Ignore extra fields that are not in `snapshot`
"normalizeJson": true, // Alphabetically sort keys in JSON
"serverEnabled": true, // Enable "update snapshot" server and button in diff modal
"serverHost": "localhost", // Hostname for "update snapshot server"
"serverPort": 2121, // Port number for "update snapshot server"
"updateSnapshots": false, // Automatically update snapshots, useful if you have lots of changes
"autoCleanUp": false, // Automatically remove snapshots that are not used by test
"autopassNewSnapshots": true, // Automatically save & pass new/non-existing snapshots
"diffLines": 3, // How many lines to include in the diff modal
"excludeFields": [], // Array of fieldnames that should be excluded from snapshot
"ignoreExtraArrayItems": false, // Ignore if there are extra array items in result
"ignoreExtraFields": false, // Ignore extra fields that are not in `snapshot`
"normalizeJson": true, // Alphabetically sort keys in JSON
"serverEnabled": true, // Enable "update snapshot" server and button in diff modal
"serverHost": "localhost", // Hostname for "update snapshot server"
"serverPort": 2121, // Port number for "update snapshot server"
"updateSnapshots": false, // Automatically update snapshots, useful if you have lots of changes
}
}
```
Expand All @@ -84,15 +87,15 @@ Add the configuration below to your `cypress.json` file to make changes to the d
Below is a list of functionality that is under consideration for implementing in a next version.

- Add basic Cypress test for demonstration
- Run basic Cypress test with [Travis](https://travis-ci.org/) based on [Cypress Travis example](https://github.com/cypress-io/cypress-example-kitchensink/blob/master/.travis.yml)
- Add screenshots to README
- Make `toMatchSnapshot` work for DOM elements
- Disable "update snapshots" server in headless mode
- Contact Cypress team to be included in [official plugin list on Cypress.io](https://docs.cypress.io/plugins/index.html)
- Add unit tests
- Add more unit tests
- Consider moving configuration to `initPlugin`.
- Extract CSS and javascript to separate files
- Add [JSDoc](http://usejsdoc.org/) documentation
- Add [Travis](https://travis-ci.org/) integration based on [Cypress Travis example](https://github.com/cypress-io/cypress-example-kitchensink/blob/master/.travis.yml)
- Investigate code coverage tests with [Coveralls](https://coveralls.io/) and [Istanbul](http://gotwarlost.github.io/istanbul/)
- Consider implementing visual snapshots with [jest-image-snapshot](https://github.com/americanexpress/jest-image-snapshot)

Expand Down
107 changes: 107 additions & 0 deletions __tests__/__snapshots__/plugin.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`plugin keepKeysFromExpected ignoreExtraFields: false, ignoreExtraArrayItems: false 1`] = `
Object {
"extra": "extra",
"field": "value",
"items": Array [
Object {
"items": Array [
Object {
"extra": "extra1",
"name": "subitem1",
},
Object {
"extra": "extra2",
"name": "subitem2",
},
],
"name": "item1",
},
Object {
"name": "item2",
},
Object {
"name": "item3",
},
Object {
"name": "item4",
},
],
}
`;

exports[`plugin keepKeysFromExpected ignoreExtraFields: false, ignoreExtraArrayItems: true 1`] = `
Object {
"extra": "extra",
"field": "value",
"items": Array [
Object {
"items": Array [
Object {
"extra": "extra1",
"name": "subitem1",
},
],
"name": "item1",
},
Object {
"name": "item2",
},
Object {
"name": "item3",
},
],
}
`;

exports[`plugin keepKeysFromExpected ignoreExtraFields: true, ignoreExtraArrayItems: false 1`] = `
Object {
"field": "value",
"items": Array [
Object {
"items": Array [
Object {
"name": "subitem1",
},
Object {
"extra": "extra2",
"name": "subitem2",
},
],
"name": "item1",
},
Object {
"name": "item2",
},
Object {
"name": "item3",
},
Object {
"name": "item4",
},
],
}
`;

exports[`plugin keepKeysFromExpected ignoreExtraFields: true, ignoreExtraArrayItems: true 1`] = `
Object {
"field": "value",
"items": Array [
Object {
"items": Array [
Object {
"name": "subitem1",
},
],
"name": "item1",
},
Object {
"name": "item2",
},
Object {
"name": "item3",
},
],
}
`;
16 changes: 16 additions & 0 deletions __tests__/__snapshots__/snapshot.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`snapshot subjectToSnapshot normalizes 1`] = `
Object {
"bar": "foo",
"foo": "bar",
"sub": Object {
"asubsub": Object {
"bar": "foo",
"foo": "bar",
},
"bar": "foo",
"foo": "bar",
},
}
`;
27 changes: 27 additions & 0 deletions __tests__/command.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { initCommands } = require('../commands');

global.Cypress = {
env: () => {},
config: () => {},
Commands: {
add: jest.fn(),
},
};

global.cy = {};

describe('command', () => {
it('should create command', () => {
global.before = jest.fn();
global.after = jest.fn();

initCommands();
global.cy.task = jest.fn().mockResolvedValue({ pass: true });

expect(global.Cypress.Commands.add).toBeCalled();
expect(global.Cypress.Commands.add.mock.calls.length).toEqual(1);
expect(global.Cypress.Commands.add.mock.calls[0][0]).toEqual('toMatchSnapshot');
expect(global.after).toBeCalled();
expect(global.before).toBeCalled();
});
});
108 changes: 108 additions & 0 deletions __tests__/plugin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const configModule = require('../config');

jest.mock("../config.js");

jest.spyOn(configModule, 'initConfig')
.mockImplementation((config) => config);

global.Cypress = {
env: () => {},
config: () => {},
Commands: {
add: jest.fn(),
},
};

global.cy = {};

describe('plugin', () => {
it('initPlugin', () => {
const globalConfig = {
env: {
"cypress-plugin-snapshots": {
"serverEnabled": false,
}
}
};
jest.spyOn(configModule, 'getConfig')
.mockImplementation(() => globalConfig.env['cypress-plugin-snapshots']);
const on = jest.fn();

const { initPlugin } = require('../plugin');

initPlugin(on, globalConfig);
expect(on).toBeCalledTimes(1);
});

describe('keepKeysFromExpected', () => {
const expected = {
field: "value",
items: [
{name: "item1", "items": [{name: "subitem1"}]},
{name: "item2"},
{name: "item3"},
]
};

const actual = {
field: "value",
extra: "extra",
items: [
{name: "item1", "items": [{name: "subitem1", extra: 'extra1'}, {name: "subitem2", extra: 'extra2'}]},
{name: "item2"},
{name: "item3"},
{name: "item4"},
]
};

it('ignoreExtraFields: false, ignoreExtraArrayItems: false', () => {
const config = {
ignoreExtraFields: false,
ignoreExtraArrayItems: false,
};
jest.spyOn(configModule, 'getConfig').mockImplementation(() => config);

const { keepKeysFromExpected } = require('../plugin');

const result = keepKeysFromExpected(actual, expected);
expect(result).toMatchSnapshot();
});

it('ignoreExtraFields: true, ignoreExtraArrayItems: false', () => {
const config = {
ignoreExtraFields: true,
ignoreExtraArrayItems: false,
};
jest.spyOn(configModule, 'getConfig').mockImplementation(() => config);

const { keepKeysFromExpected } = require('../plugin');
const result = keepKeysFromExpected(actual, expected);
expect(result).toMatchSnapshot();
});

it('ignoreExtraFields: true, ignoreExtraArrayItems: true', () => {
const config = {
ignoreExtraFields: true,
ignoreExtraArrayItems: true,
};
jest.spyOn(configModule, 'getConfig').mockImplementation(() => config);
const { keepKeysFromExpected } = require('../plugin');

const result = keepKeysFromExpected(actual, expected);
expect(result).toMatchSnapshot();
});

it('ignoreExtraFields: false, ignoreExtraArrayItems: true', () => {
const config = {
ignoreExtraFields: false,
ignoreExtraArrayItems: true,
};
jest.spyOn(configModule, 'getConfig').mockImplementation(() => config);

const { keepKeysFromExpected } = require('../plugin');

const result = keepKeysFromExpected(actual, expected);
expect(result).toMatchSnapshot();
});
});
});
22 changes: 22 additions & 0 deletions __tests__/snapshot.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { subjectToSnapshot } = require('../snapshot');

describe('snapshot', () => {
describe('subjectToSnapshot', () => {
it('normalizes', () => {
const normalized = subjectToSnapshot({
foo: 'bar',
bar: 'foo',
sub: {
foo: 'bar',
bar: 'foo',
asubsub: {
foo: 'bar',
bar: 'foo',
}
}
}, true);

expect(normalized).toMatchSnapshot();
});
});
});
Loading

0 comments on commit 8339813

Please sign in to comment.