-
Notifications
You must be signed in to change notification settings - Fork 1
/
vm.js
43 lines (34 loc) · 1.46 KB
/
vm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const vm = require('vm');
(async () => {
const code = `
(async(require, __dirname) => {
const pixelmatch = require('pixelmatch');
const fs = require('fs');
const rimraf = require('rimraf');
const { PNG } = require('./pngjs/lib/png');
console.time('reading screenshot-a:');
const screenshotA = fs.readFileSync(__dirname + '/stubs/screenshot-a.png');
console.timeEnd('reading screenshot-a:');
console.time('reading screenshot-b:');
const screenshotB = fs.readFileSync(__dirname + '/stubs/screenshot-b.png');
console.timeEnd('reading screenshot-b:');
const diff = new PNG({
width: 1100,
height: 2400,
});
console.time('running image comparison:');
pixelmatch(screenshotA, screenshotB, diff.data, 1100, 2400, {
threshold: 0.1
});
console.timeEnd('running image comparison:');
console.time('creating diff buffer:');
const buffer = PNG.sync.write(diff);
console.timeEnd('creating diff buffer:');
console.time('writing diff file:');
fs.writeFileSync('image-diff.png', buffer);
console.timeEnd('writing diff file:');
rimraf.sync('image-diff.png');
})
`;
vm.runInThisContext(code)(require, __dirname);
})();