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

feat: add an option to configure jest matcher #19

Merged
merged 5 commits into from
Jun 25, 2018
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,29 @@ When you change production code implementation, Eyes will break, and you will ha

`version` <[string]> (optional) Used to create a new baseline. See [Creating a new baseline](https://github.com/wix-incubator/match-screenshot#creating-a-new-baseline) for more details. Default value: 'v1.0.0'.


#### jestWithConfig([options])

Configure your matcher with global options.

Set the matcher:

```js
"jest": {
"setupTestFrameworkScriptFile": "<rootDir>/setupTestFrameworkScriptFile.js"
},
```

Inside `setupTestFrameworkScriptFile.js` you can then:

```js
require('match-screenshot/jestWithConfig')(options);
```

- options

`appName` <[string]> Application name. Will be used inside Applitools as part of test title

## How does it work

Everytime you use `toMatchScreenshot` matcher, a screenshot will be sent to [Applitools Eyes](https://applitools.com/), which will compare the new screenshot with the baseline. The test will fail if they are not equal.
Expand Down
1 change: 1 addition & 0 deletions jestWithConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = (options = {}) => require('./lib/jestWithConfig')(options);
2 changes: 1 addition & 1 deletion lib/chai.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const toMatchEyesScreenshot = require('./toMatchEyesScreenshot.js');

async function toMatchScreenshot(options) {
await toMatchEyesScreenshot(this._obj, options);
await toMatchEyesScreenshot()(this._obj, options);
}
module.exports = toMatchScreenshot;
2 changes: 1 addition & 1 deletion lib/jest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const matcherGenerator = require('./matcherGenerator');
const toMatchEyesScreenshot = require('./toMatchEyesScreenshot');

module.exports = matcherGenerator(toMatchEyesScreenshot);
module.exports = matcherGenerator(toMatchEyesScreenshot());
4 changes: 4 additions & 0 deletions lib/jestWithConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const matcherGenerator = require('./matcherGenerator');
const toMatchEyesScreenshot = require('./toMatchEyesScreenshot');

module.exports = options => matcherGenerator(toMatchEyesScreenshot(options));
34 changes: 17 additions & 17 deletions lib/toMatchEyesScreenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ const {Eyes} = require('eyes.images');
const path = require('path');
require('dotenv').config();

async function toMatchScreenshot(instance, {key, version = 'v1.0.0'}) {
const eyes = new Eyes();
if (process.env.EYES_API_KEY) {
eyes.setOs(process.platform);
eyes.setApiKey(process.env.EYES_API_KEY);
}
const appName = require(path.join(process.cwd(), 'package.json')).name;
module.exports = (options = {}) =>
async function toMatchScreenshot(instance, {key, version = 'v1.0.0'}) {
const eyes = new Eyes();
if (process.env.EYES_API_KEY) {
eyes.setOs(process.platform);
eyes.setApiKey(process.env.EYES_API_KEY);
}
const appName =
options.appName || require(path.join(process.cwd(), 'package.json')).name;

await sendToEyes({
eyes,
img: instance,
appName,
specName: key,
version,
});
}
await sendToEyes({
eyes,
img: instance,
appName,
specName: key,
version,
});
};

const sendToEyes = async ({eyes, img, appName, specName, version}) => {
try {
Expand All @@ -33,5 +35,3 @@ const sendToEyes = async ({eyes, img, appName, specName, version}) => {
await eyes.close();
console.log(`eyes comparison succeed for test "${specName} ${version}"`);
};

module.exports = toMatchScreenshot;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"preset": "jest-puppeteer",
"testMatch": [ "**/tests/__fixtures__/*.jest.js"],
"testMatch": [ "**/tests/__fixtures__/jest-default/*.jest.js"],
"setupTestFrameworkScriptFile": "<rootDir>/setupTestFrameworkScriptFile.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('../../../lib/jest');
5 changes: 5 additions & 0 deletions tests/__fixtures__/jest-with-config/conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"preset": "jest-puppeteer",
"testMatch": [ "**/tests/__fixtures__/jest-with-config/*.jest.js"],
"setupTestFrameworkScriptFile": "<rootDir>/setupTestFrameworkScriptFile.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require('../../../lib/jestWithConfig')({
appName: 'match-screenshot-with-config',
});
8 changes: 8 additions & 0 deletions tests/__fixtures__/jest-with-config/test.jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
it(`should work`, async () => {
jest.setTimeout(30000);
await global.page.setContent('<div>Hi there with config</div>');
const screenshot = await global.page.screenshot({fullpage: true});
await expect(screenshot).toMatchScreenshot({
key: 'should work with jest',
});
});
1 change: 0 additions & 1 deletion tests/__fixtures__/setupTestFrameworkScriptFile.js

This file was deleted.

22 changes: 20 additions & 2 deletions tests/toMatchScreenshotJest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,30 @@ describe('EYES', () => {
describe('Log', () => {
test('should log after eyes success', async () => {
const options = {
config: require.resolve('./__fixtures__/conf.json'),
config: require.resolve('./__fixtures__/jest-default/conf.json'),
};

const res = await execa('node', [
require.resolve('jest/bin/jest'),
require.resolve('./__fixtures__/test.jest'),
require.resolve('./__fixtures__/jest-default/test.jest'),
dargs(options),
]);

expect(res.stdout).toContain(
'eyes comparison succeed for test "should work with jest v1.0.0"',
);
});
});

describe('should works with configurations', () => {
test('should log after eyes success', async () => {
const options = {
config: require.resolve('./__fixtures__/jest-with-config/conf.json'),
};

const res = await execa('node', [
require.resolve('jest/bin/jest'),
require.resolve('./__fixtures__/jest-with-config/test.jest'),
dargs(options),
]);

Expand Down