diff --git a/README.md b/README.md index a084ad6..e4c9783 100644 --- a/README.md +++ b/README.md @@ -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": "/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. diff --git a/jestWithConfig.js b/jestWithConfig.js new file mode 100644 index 0000000..84091e8 --- /dev/null +++ b/jestWithConfig.js @@ -0,0 +1 @@ +module.exports = (options = {}) => require('./lib/jestWithConfig')(options); diff --git a/lib/chai.js b/lib/chai.js index 29049a8..6fa9c28 100644 --- a/lib/chai.js +++ b/lib/chai.js @@ -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; diff --git a/lib/jest.js b/lib/jest.js index 8226a6c..751ef8c 100644 --- a/lib/jest.js +++ b/lib/jest.js @@ -1,4 +1,4 @@ const matcherGenerator = require('./matcherGenerator'); const toMatchEyesScreenshot = require('./toMatchEyesScreenshot'); -module.exports = matcherGenerator(toMatchEyesScreenshot); +module.exports = matcherGenerator(toMatchEyesScreenshot()); diff --git a/lib/jestWithConfig.js b/lib/jestWithConfig.js new file mode 100644 index 0000000..599ecf6 --- /dev/null +++ b/lib/jestWithConfig.js @@ -0,0 +1,4 @@ +const matcherGenerator = require('./matcherGenerator'); +const toMatchEyesScreenshot = require('./toMatchEyesScreenshot'); + +module.exports = options => matcherGenerator(toMatchEyesScreenshot(options)); diff --git a/lib/toMatchEyesScreenshot.js b/lib/toMatchEyesScreenshot.js index 694c3bf..3ea386b 100644 --- a/lib/toMatchEyesScreenshot.js +++ b/lib/toMatchEyesScreenshot.js @@ -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 { @@ -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; diff --git a/tests/__fixtures__/conf.json b/tests/__fixtures__/jest-default/conf.json similarity index 62% rename from tests/__fixtures__/conf.json rename to tests/__fixtures__/jest-default/conf.json index 7a9f6d3..9babac7 100644 --- a/tests/__fixtures__/conf.json +++ b/tests/__fixtures__/jest-default/conf.json @@ -1,5 +1,5 @@ { "preset": "jest-puppeteer", - "testMatch": [ "**/tests/__fixtures__/*.jest.js"], + "testMatch": [ "**/tests/__fixtures__/jest-default/*.jest.js"], "setupTestFrameworkScriptFile": "/setupTestFrameworkScriptFile.js" } diff --git a/tests/__fixtures__/jest-default/setupTestFrameworkScriptFile.js b/tests/__fixtures__/jest-default/setupTestFrameworkScriptFile.js new file mode 100644 index 0000000..172d287 --- /dev/null +++ b/tests/__fixtures__/jest-default/setupTestFrameworkScriptFile.js @@ -0,0 +1 @@ +require('../../../lib/jest'); diff --git a/tests/__fixtures__/test.jest.js b/tests/__fixtures__/jest-default/test.jest.js similarity index 100% rename from tests/__fixtures__/test.jest.js rename to tests/__fixtures__/jest-default/test.jest.js diff --git a/tests/__fixtures__/jest-with-config/conf.json b/tests/__fixtures__/jest-with-config/conf.json new file mode 100644 index 0000000..6d60a01 --- /dev/null +++ b/tests/__fixtures__/jest-with-config/conf.json @@ -0,0 +1,5 @@ +{ + "preset": "jest-puppeteer", + "testMatch": [ "**/tests/__fixtures__/jest-with-config/*.jest.js"], + "setupTestFrameworkScriptFile": "/setupTestFrameworkScriptFile.js" +} diff --git a/tests/__fixtures__/jest-with-config/setupTestFrameworkScriptFile.js b/tests/__fixtures__/jest-with-config/setupTestFrameworkScriptFile.js new file mode 100644 index 0000000..4ca3991 --- /dev/null +++ b/tests/__fixtures__/jest-with-config/setupTestFrameworkScriptFile.js @@ -0,0 +1,3 @@ +require('../../../lib/jestWithConfig')({ + appName: 'match-screenshot-with-config', +}); diff --git a/tests/__fixtures__/jest-with-config/test.jest.js b/tests/__fixtures__/jest-with-config/test.jest.js new file mode 100644 index 0000000..4e52c75 --- /dev/null +++ b/tests/__fixtures__/jest-with-config/test.jest.js @@ -0,0 +1,8 @@ +it(`should work`, async () => { + jest.setTimeout(30000); + await global.page.setContent('
Hi there with config
'); + const screenshot = await global.page.screenshot({fullpage: true}); + await expect(screenshot).toMatchScreenshot({ + key: 'should work with jest', + }); +}); diff --git a/tests/__fixtures__/setupTestFrameworkScriptFile.js b/tests/__fixtures__/setupTestFrameworkScriptFile.js deleted file mode 100644 index 435c085..0000000 --- a/tests/__fixtures__/setupTestFrameworkScriptFile.js +++ /dev/null @@ -1 +0,0 @@ -require('../../lib/jest'); diff --git a/tests/toMatchScreenshotJest.test.js b/tests/toMatchScreenshotJest.test.js index 1057d03..4c509de 100644 --- a/tests/toMatchScreenshotJest.test.js +++ b/tests/toMatchScreenshotJest.test.js @@ -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), ]);