Browse Source

Unit tests: upgrade jsdom to v11 to fix breaking unit tests for Network (#3603)

* First working version

* Fix completed for jsdom getContext() issue

* Fixed commenting for review
mbroad/code-climate-coverage-develop
wimrijnders 7 years ago
committed by Yotam Berkowitz
parent
commit
773d593ca9
10 changed files with 91 additions and 49 deletions
  1. +3
    -0
      .gitignore
  2. +1
    -0
      .travis.yml
  3. +2
    -3
      package.json
  4. +6
    -12
      test/Graph3d.test.js
  5. +1
    -6
      test/Label.test.js
  6. +2
    -18
      test/Network.test.js
  7. +9
    -2
      test/PointItem.test.js
  8. +9
    -3
      test/TimeStep.test.js
  9. +9
    -3
      test/TimelineRange.test.js
  10. +49
    -2
      test/canvas-mock.js

+ 3
- 0
.gitignore View File

@ -17,3 +17,6 @@ npm-debug.log
.*.sw[op]
.commits.tmp
gen/
.nyc_output/
coverage/
package-lock.json

+ 1
- 0
.travis.yml View File

@ -1,6 +1,7 @@
language: node_js
node_js:
- "6"
- "7"
env:
- CXX=g++-4.8
addons:

+ 2
- 3
package.json View File

@ -61,11 +61,10 @@
"gulp-rename": "^1.2.2",
"gulp-util": "^3.0.8",
"jsdoc": "^3.5.5",
"jsdom": "9.12.0",
"jsdom-global": "^2.1.1",
"jsdom": "11.3.0",
"jsdom-global": "^3.0.2",
"merge-stream": "^1.0.1",
"mocha": "^3.4.2",
"mocha-jsdom": "^1.1.0",
"nyc": "^11.2.1",
"rimraf": "^2.6.1",
"test-console": "^1.0.0",

+ 6
- 12
test/Graph3d.test.js View File

@ -11,16 +11,16 @@ var now = new Date();
describe('Graph3d', function () {
before(function() {
//console.log('before!');
this.jsdom_global = jsdom_global(
"<div id='mygraph'></div>",
{ skipWindowCheck: true}
);
canvasMockify(window);
this.jsdom_global = canvasMockify("<div id='mygraph'></div>");
this.container = document.getElementById('mygraph');
});
after(function() {
this.jsdom_global();
});
it('should pass validation for the default options', function () {
assert(Graph3d.DEFAULTS !== undefined);
@ -61,10 +61,4 @@ describe('Graph3d', function () {
graph.setOptions({ style: 'bar'}); // Call should just work, no exception thrown
assert.equal(graph.style, BAR_STYLE, "Style not set to expected 'bar'");
});
after(function() {
//console.log('after!');
this.jsdom_global();
});
});

+ 1
- 6
test/Label.test.js View File

@ -13,7 +13,6 @@ var assert = require('assert');
var Label = require('../lib/network/modules/components/shared/Label').default;
var NodesHandler = require('../lib/network/modules/NodesHandler').default;
var util = require('../lib/util');
var jsdom_global = require('jsdom-global');
var canvasMockify = require('./canvas-mock');
var DataSet = require('../lib/DataSet');
var Network = require('../lib/network/Network');
@ -322,11 +321,7 @@ describe('Network Label', function() {
**************************************************************/
before(function() {
this.jsdom_global = jsdom_global(
"<div id='mynetwork'></div>",
{ skipWindowCheck: true}
);
canvasMockify(window);
this.jsdom_global = canvasMockify("<div id='mynetwork'></div>");
this.container = document.getElementById('mynetwork');
});

+ 2
- 18
test/Network.test.js View File

@ -16,7 +16,6 @@ var DataSet = require('../lib/DataSet');
var Network = require('../lib/network/Network');
var stdout = require('test-console').stdout;
var Validator = require("./../lib/shared/Validator").default;
var jsdom_global = require('jsdom-global');
var canvasMockify = require('./canvas-mock');
var {allOptions, configureOptions} = require('./../lib/network/options.js');
//var {printStyle} = require('./../lib/shared/Validator');
@ -222,31 +221,16 @@ function checkFontProperties(fontItem, checkStrict = true) {
}
describe('Network', function () {
before(function() {
this.jsdom_global = jsdom_global(
"<div id='mynetwork'></div>",
{ skipWindowCheck: true}
);
canvasMockify(window);
this.jsdom_global = canvasMockify("<div id='mynetwork'></div>");
this.container = document.getElementById('mynetwork');
});
after(function() {
try {
this.jsdom_global();
} catch(e) {
if (e.message() === 'window is undefined') {
console.warning("'" + e.message() + "' happened again");
} else {
throw e;
}
}
this.jsdom_global();
});

+ 9
- 2
test/PointItem.test.js View File

@ -1,5 +1,5 @@
var assert = require('assert');
var jsdom = require('mocha-jsdom');
var jsdom_global = require('jsdom-global');
var moment = require('../lib/module/moment');
var PointItem = require("../lib/timeline/component/item/PointItem");
var Range = require('../lib/timeline/Range');
@ -7,9 +7,16 @@ var TestSupport = require('./TestSupport');
describe('Timeline PointItem', function () {
jsdom();
var now = moment();
before(function() {
this.jsdom_global = jsdom_global();
});
after(function() {
this.jsdom_global();
});
it('should initialize with minimal data', function() {
var pointItem = new PointItem({start: now.toDate()}, null, null);
assert.equal(pointItem.props.content.height, 0);

+ 9
- 3
test/TimeStep.test.js View File

@ -1,12 +1,18 @@
var assert = require('assert');
var jsdom = require('mocha-jsdom');
var jsdom_global = require('jsdom-global');
var moment = require('../lib/module/moment');
var TimeStep = require('../lib/timeline/TimeStep');
var TestSupport = require('./TestSupport');
describe('TimeStep', function () {
jsdom();
before(function() {
this.jsdom_global = jsdom_global();
});
after(function() {
this.jsdom_global();
});
it('should work with just start and end dates', function () {
var timestep = new TimeStep(new Date(2017, 3, 3), new Date(2017, 3, 5));
@ -85,4 +91,4 @@ describe('TimeStep', function () {
assert.equal(timestep.getCurrent().unix(), moment("2017-04-03T01:00:00.000").unix(), "should have the right value after a step");
});
});
});

+ 9
- 3
test/TimelineRange.test.js View File

@ -1,12 +1,18 @@
var assert = require('assert');
var jsdom = require('mocha-jsdom');
var jsdom_global = require('jsdom-global');
var moment = require('../lib/module/moment');
var Range = require('../lib/timeline/Range');
var TestSupport = require('./TestSupport');
describe('Timeline Range', function () {
jsdom();
before(function() {
this.jsdom_global = jsdom_global();
});
after(function() {
this.jsdom_global();
});
it('should have start default before now', function () {
var now = moment().hours(0).minutes(0).seconds(0).milliseconds(0).valueOf();
@ -37,4 +43,4 @@ describe('Timeline Range', function () {
range.setRange(new Date(2017, 0, 26, 13, 26, 3, 320), new Date(2017, 0, 26, 13, 26, 4, 320), false, false, null);
assert.equal(range.getMillisecondsPerPixel(), 1, "one second over 1000 pixels");
});
});
});

+ 49
- 2
test/canvas-mock.js View File

@ -3,6 +3,8 @@
*
* Adapted from: https://github.com/Cristy94/canvas-mock
*/
var jsdom = require('jsdom');
var jsdom_global = require('jsdom-global');
var canvasMock; // Use one canvas instance for all calls to createElement('canvas');
@ -59,10 +61,11 @@ function replaceCanvasContext (el) {
* The override is only done if there is no 2D context already present.
* This allows for normal running in a browser, and for node.js the usage of 'canvas'.
*
* @param {object} the current global window object. This can possible come from module 'jsdom',
* @param {object} the current global window object. This can possibly come from module 'jsdom',
* when running under node.js.
* @private
*/
function mockify(window) {
function overrideCreateElement(window) {
var d = window.document;
var f = window.document.createElement;
@ -87,4 +90,48 @@ function mockify(window) {
};
}
/**
* Initialize the mock, jsdom and jsdom_global for unit test usage.
*
* Suppresses a warning from `jsdom` on usage of `getContext()`. A mock definition is added for
* it, so the message is not relevant.
*
* @param {string} [html=''] html definitions which should be added to the jsdom definition
* @returns {function} function to call in after(), to clean up for `jsdom_global`
*/
function mockify(html = '') {
// Start of message that we want to suppress.
let msg = 'Error: Not implemented: HTMLCanvasElement.prototype.getContext'
+ ' (without installing the canvas npm package)';
// Override default virtual console of jsdom
const virtualConsole = new jsdom.VirtualConsole();
// Set up a simple 'mock' console output. Only 'error' needs to be overridden
let myConsole = {
error: (msg) => {
if (msg.indexOf(msg) === 0) {
//console.error('all is well');
} else {
// All other messages pass through
console.error(msg);
}
}
};
// Using the global catch instead of specific event handler, because I couldn't get them to work
virtualConsole.sendTo(myConsole);
let cleanupFunction = jsdom_global(
html,
{ skipWindowCheck: true, virtualConsole: virtualConsole}
);
overrideCreateElement(window); // The actual initialization of canvas-mock
return cleanupFunction;
}
module.exports = mockify;

Loading…
Cancel
Save