Skip to content

Commit 57355ba

Browse files
authored
Make Differencify run with vanilla node JS (#81)
1 parent 49f697c commit 57355ba

File tree

9 files changed

+43
-34
lines changed

9 files changed

+43
-34
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## [1.3.4] - 2018-6-9
2+
### Fixed
3+
- Export module Differencify
4+
- Make Differencify running with vanila node
5+
- Fix screenshot path issue for non jest
6+
17
## [1.3.3] - 2018-6-6
28
### Fixed
39
- Bug fixed for custom test path

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ npm install differencify
2828
```
2929
## Usage
3030
```js
31-
import Differencify from 'differencify';
31+
const Differencify = require('differencify');
3232
const differencify = new Differencify(GlobalOptions);
3333
```
3434

@@ -43,7 +43,7 @@ Differencify matches [Puppeteer](https://github.com/GoogleChrome/puppeteer/blob/
4343
.newPage()
4444
.setViewport({ width: 1600, height: 1200 })
4545
.goto('https://github.com/NimaSoroush/differencify')
46-
.wait(1000)
46+
.waitFor(1000)
4747
.screenshot()
4848
.toMatchSnapshot()
4949
.result((result) => {
@@ -59,7 +59,7 @@ Differencify matches [Puppeteer](https://github.com/GoogleChrome/puppeteer/blob/
5959
const page = await target.newPage();
6060
await page.setViewport({ width: 1600, height: 1200 });
6161
await page.goto('https://github.com/NimaSoroush/differencify');
62-
await page.wait(1000);
62+
await page.waitFor(1000);
6363
const image = await page.screenshot();
6464
const result = await page.toMatchSnapshot(image)
6565
await page.close();
@@ -74,7 +74,6 @@ See more examples [here](API.md)
7474

7575
Only need to wrap your steps into `it()` function
7676
```js
77-
import Differencify from 'differencify';
7877
const differencify = new Differencify();
7978
describe('tests differencify', () => {
8079
it('validate github page appear correctly', async () => {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "differencify",
3-
"version": "1.3.3",
3+
"version": "1.3.4",
44
"description": "Perceptual diffing tool",
55
"main": "dist/index.js",
66
"scripts": {

src/compareImage.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ const getSnapshotsDir = (testConfig, globalConfig) => {
1919
if (testConfig.isJest) {
2020
testRoot = path.dirname(testConfig.testPath);
2121
} else {
22-
const rootPath = path.join(__dirname, '../');
23-
testRoot = path.resolve(__dirname, rootPath, globalConfig.imageSnapshotPath);
22+
testRoot = path.resolve(globalConfig.imageSnapshotPath);
2423
if (!fs.existsSync(testRoot)) {
2524
fs.mkdirSync(testRoot);
2625
}
@@ -32,7 +31,7 @@ const compareImage = async (capturedImage, globalConfig, testConfig) => {
3231
const prefixedLogger = logger.prefix(testConfig.testName);
3332

3433
const snapshotsDir = globalConfig.imageSnapshotPathProvided ?
35-
globalConfig.imageSnapshotPath :
34+
path.resolve(globalConfig.imageSnapshotPath) :
3635
getSnapshotsDir(testConfig, globalConfig);
3736

3837
const snapshotPath = path.join(snapshotsDir, `${testConfig.testName}.snap.${testConfig.imageType || 'png'}`);

src/compareImage.test.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jest.mock('fs', () => ({
1818
jest.mock('path', () => ({
1919
dirname: jest.fn(() => '/parent'),
2020
join: jest.fn((a, b) => `${a}/${b}`),
21-
resolve: jest.fn(() => 'dir'),
21+
resolve: jest.fn(path => path),
2222
}));
2323

2424
const mockLog = jest.fn();
@@ -100,13 +100,12 @@ describe('Compare Image', () => {
100100
isUpdate: false,
101101
isJest: false,
102102
testName: 'test',
103-
testPath: '/src/test.js',
104103
imageType: 'png',
105104
});
106105
expect(result).toEqual({ added: true });
107106
expect(fs.writeFileSync)
108107
.toHaveBeenCalledWith(
109-
'dir/__image_snapshots__/test.snap.png',
108+
'./differencify_report/__image_snapshots__/test.snap.png',
110109
Object,
111110
);
112111
});
@@ -115,13 +114,12 @@ describe('Compare Image', () => {
115114
isUpdate: true,
116115
isJest: false,
117116
testName: 'test',
118-
testPath: '/src/test.js',
119117
imageType: 'png',
120118
});
121119
expect(result).toEqual({ updated: true });
122120
expect(fs.writeFileSync)
123121
.toHaveBeenCalledWith(
124-
'dir/__image_snapshots__/test.snap.png',
122+
'./differencify_report/__image_snapshots__/test.snap.png',
125123
Object,
126124
);
127125
});

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,5 @@ export default class Differencify {
8080
}
8181
}
8282
}
83+
84+
module.exports = Differencify;

src/integration.tests/integration.test.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('Differencify', () => {
2020
.toMatchSnapshot()
2121
.close()
2222
.end();
23-
}, 20000);
23+
}, 30000);
2424
it('simple unchained', async () => {
2525
const target = differencify.init({ chain: false });
2626
const page = await target.newPage();
@@ -31,7 +31,7 @@ describe('Differencify', () => {
3131
const result = await target.toMatchSnapshot(image);
3232
await page.close();
3333
expect(result).toEqual(true);
34-
}, 20000);
34+
}, 30000);
3535
it('Launch new browser per test', async () => {
3636
await differencify
3737
.init()
@@ -44,7 +44,7 @@ describe('Differencify', () => {
4444
.toMatchSnapshot()
4545
.close()
4646
.end();
47-
}, 20000);
47+
}, 30000);
4848
it('Launch new browser per test when unchained', async () => {
4949
const target = differencify.init({ chain: false });
5050
await target.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
@@ -57,7 +57,7 @@ describe('Differencify', () => {
5757
await page.close();
5858
await target.close();
5959
expect(result).toEqual(true);
60-
}, 20000);
60+
}, 30000);
6161
it('Using result function', async () => {
6262
await differencify
6363
.init()
@@ -73,7 +73,7 @@ describe('Differencify', () => {
7373
.toMatchSnapshot()
7474
.close()
7575
.end();
76-
}, 20000);
76+
}, 30000);
7777
it('Context switching when chained', async () => {
7878
await differencify
7979
.init()
@@ -93,7 +93,7 @@ describe('Differencify', () => {
9393
.toMatchSnapshot()
9494
.close()
9595
.end();
96-
}, 20000);
96+
}, 30000);
9797
it('Calling Puppeteer specific functions when chained: console', async () => {
9898
await differencify
9999
.init()
@@ -106,7 +106,7 @@ describe('Differencify', () => {
106106
.evaluate(() => console.log('hello'))
107107
.close()
108108
.end();
109-
}, 20000);
109+
}, 30000);
110110
it('Calling Puppeteer specific functions when chained: dialog', async () => {
111111
await differencify
112112
.init()
@@ -118,7 +118,7 @@ describe('Differencify', () => {
118118
.evaluate(() => alert('1'))
119119
.close()
120120
.end();
121-
}, 20000);
121+
}, 30000);
122122
it('Continue on chained object', async () => {
123123
await differencify
124124
.init()
@@ -132,7 +132,7 @@ describe('Differencify', () => {
132132
})
133133
.close()
134134
.end();
135-
}, 20000);
135+
}, 30000);
136136
it('Multiple toMatchSnapshot on chained object', async () => {
137137
await differencify
138138
.init()
@@ -153,7 +153,7 @@ describe('Differencify', () => {
153153
})
154154
.close()
155155
.end();
156-
}, 20000);
156+
}, 30000);
157157
it('Multiple toMatchSnapshot when unchained', async () => {
158158
const target = differencify.init({ chain: false });
159159
const page = await target.newPage();
@@ -170,7 +170,7 @@ describe('Differencify', () => {
170170
await page.close();
171171
expect(result).toEqual(true);
172172
expect(result2).toEqual(true);
173-
}, 20000);
173+
}, 30000);
174174
it('Custom test name', async () => {
175175
const target = differencify.init({
176176
testName: 'test1',
@@ -184,7 +184,7 @@ describe('Differencify', () => {
184184
const result = await target.toMatchSnapshot(image);
185185
await page.close();
186186
expect(result).toEqual(true);
187-
}, 20000);
187+
}, 30000);
188188
it('Custom test path', async () => {
189189
const customDifferencify = new Differencify({
190190
imageSnapshotPath: './src/integration.tests/__image_snapshots__/custom_test_path',
@@ -203,5 +203,5 @@ describe('Differencify', () => {
203203
await page.close();
204204
await customDifferencify.cleanup();
205205
expect(result).toEqual(true);
206-
}, 20000);
206+
}, 30000);
207207
});

src/target.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,18 @@ export default class Target {
183183
}
184184

185185
isJest() {
186-
this.testConfig.isJest = (expect && isFunc(expect.getState));
187-
if (this.testConfig.isJest) {
188-
this.testStats = expect.getState() || null;
189-
this.prefixedLogger = logger.prefix(this.testStats.currentTestName);
190-
this.testConfig.testPath = this.testStats.testPath;
191-
this.testConfig.isUpdate = this.testStats.snapshotState._updateSnapshot === 'all' || false;
192-
} else {
186+
try {
187+
this.testConfig.isJest = (expect && isFunc(expect.getState));
188+
if (this.testConfig.isJest) {
189+
this.testStats = expect.getState() || null;
190+
this.prefixedLogger = logger.prefix(this.testStats.currentTestName);
191+
this.testConfig.testPath = this.testStats.testPath;
192+
this.testConfig.isUpdate = this.testStats.snapshotState._updateSnapshot === 'all' || false;
193+
} else {
194+
this.testId = this.testConfig.testId;
195+
}
196+
} catch (error) {
197+
this.testConfig.isJest = false;
193198
this.testId = this.testConfig.testId;
194199
}
195200
}

0 commit comments

Comments
 (0)