Skip to content

Commit

Permalink
feat: add an option to configure jest matcher (#19)
Browse files Browse the repository at this point in the history
At this stage, it is only possible to configure appName. Later it will be easier to add batch support.

This is part of #15 effort
  • Loading branch information
yanivefraim authored Jun 25, 2018
1 parent 1ee7613 commit a2a0648
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 23 deletions.
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');
File renamed without changes.
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

0 comments on commit a2a0648

Please sign in to comment.