-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
node_test_setup.js
129 lines (119 loc) · 4.35 KB
/
node_test_setup.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// set the fabric framework as a global for tests
var chalk = require('chalk');
var diff = require('deep-object-diff').diff;
var commander = require('commander');
// commander.program
// .option('-d, --debug', 'debug visual tests by overriding refs (golden images) in case of visual changes', false)
// .option('-r, --recreate', 'recreate visual refs (golden images)', false)
// .action(options => {
// QUnit.debug = QUnit.debugVisual = options.debug;
// QUnit.recreateVisualRefs = options.recreate;
// }).parse(process.argv);
// // for now accept an env variable because qunit doesn't allow passing unknown options
// QUnit.debugVisual = Number(process.env.QUNIT_DEBUG_VISUAL_TESTS);
// QUnit.recreateVisualRefs = Number(process.env.QUNIT_RECREATE_VISUAL_REFS);
// QUnit.config.filter = process.env.QUNIT_FILTER;
global.fabric = require('../dist/fabric').fabric;
global.pixelmatch = require('pixelmatch');
global.fs = require('fs');
global.visualCallback = {
addArguments: function() {},
};
global.visualTestLoop = require('./lib/visualTestLoop').visualTestLoop;
global.compareGoldensTest = require('./lib/visualTestLoop').compareGoldensTest;
global.getFixture = require('./lib/visualTestLoop').getFixture;
global.getAsset = require('./lib/visualTestLoop').getAsset;
global.getAssetName = require('./lib/visualTestLoop').getAssetName;
global.simulateEvent = require('./lib/event.simulate').simulateEvent;
global.imageDataToChalk = function(imageData) {
// actually this does not work on travis-ci, so commenting it out
return '';
var block = String.fromCharCode(9608);
var data = imageData.data;
var width = imageData.width;
var height = imageData.height;
var outputString = '';
var cp = 0;
for (var i = 0; i < height; i++) {
outputString += '\n';
for (var j = 0; j < width; j++) {
cp = (i * width + j) * 4;
outputString += chalk.rgb(data[cp], data[cp + 1], data[cp + 2])(block);
}
}
return outputString;
};
QUnit.config.testTimeout = 15000;
QUnit.config.noglobals = true;
QUnit.config.hidepassed = true;
var jsdom = require('jsdom');
// make a jsdom version for tests that does not spam too much.
class CustomResourceLoader extends jsdom.ResourceLoader {
fetch(url, options) {
return super.fetch(url, options).catch(e => {
console.log('JSDOM CATCHED FETCHING', url);
throw new Error('JSDOM FETCH CATCHED');
});
}
}
var jsdom = require('jsdom');
var virtualWindow = new jsdom.JSDOM(
decodeURIComponent('%3C!DOCTYPE%20html%3E%3Chtml%3E%3Chead%3E%3C%2Fhead%3E%3Cbody%3E%3C%2Fbody%3E%3C%2Fhtml%3E'),
{
features: {
FetchExternalResources: ['img']
},
resources: new CustomResourceLoader(),
}).window;
fabric.document = virtualWindow.document;
fabric.jsdomImplForWrapper = require('jsdom/lib/jsdom/living/generated/utils').implForWrapper;
fabric.nodeCanvas = require('jsdom/lib/jsdom/utils').Canvas;
fabric.window = virtualWindow;
DOMParser = fabric.window.DOMParser;
// QUnit Logging
// testID
var objectInit = fabric.Object.prototype.initialize;
var canvasInit = fabric.StaticCanvas.prototype.initialize;
var testID = 0;
fabric.Object.prototype.initialize = function () {
objectInit.apply(this, arguments);
this.testID = `${this.type}#${++testID}`;
}
fabric.StaticCanvas.prototype.initialize = function () {
canvasInit.apply(this, arguments);
this.testID = `Canvas#${++testID}`;
}
function getLoggingRepresentation(input) {
return typeof input === 'object' && input && input.testID ?
input.testID :
input;
}
// https://api.qunitjs.com/extension/QUnit.dump.parse/
QUnit.dump.maxDepth = 1;
// https://github.com/qunitjs/qunit/blob/main/src/assert.js
QUnit.assert.deepEqual = function (actual, expected, message) {
actual = QUnit.dump.parse(actual);
expected = QUnit.dump.parse(expected);
this.pushResult({
result: QUnit.equiv(actual, expected),
message: `${message}\ndiff:\n${diff(actual, expected)}`,
actual,
expected
});
};
QUnit.assert.equal = function (actual, expected, message) {
this.pushResult({
result: actual == expected,
actual: getLoggingRepresentation(actual),
expected: getLoggingRepresentation(expected),
message
});
};
QUnit.assert.strictEqual = function (actual, expected, message) {
this.pushResult({
result: actual === expected,
actual: getLoggingRepresentation(actual),
expected: getLoggingRepresentation(expected),
message
});
};