diff --git a/tasks/test_mock.js b/tasks/test_mock.js index 957b86b41ef..0503ef8796b 100644 --- a/tasks/test_mock.js +++ b/tasks/test_mock.js @@ -1,16 +1,9 @@ var minimist = require('minimist'); -var jsdom = require('jsdom'); var path = require('path'); var fs = require('fs'); -var plotlyServerDom = new jsdom.JSDOM('', { runScripts: 'dangerously'}); -// Mock a few things that jsdom doesn't support out-of-the-box -plotlyServerDom.window.URL.createObjectURL = function() {}; - -// Run Plotly inside jsdom -var plotlyJsPath = require.resolve('../build/plotly.js'); -var plotlyJsSource = fs.readFileSync(plotlyJsPath, 'utf-8'); -plotlyServerDom.window.eval(plotlyJsSource); +var plotlyNode = require('./util/plotly_node'); +var Plotly = plotlyNode('build/plotly.js'); var pathToRoot = path.join(__dirname, '..'); var pathToMocks = path.join(pathToRoot, 'test', 'image', 'mocks'); @@ -39,7 +32,7 @@ for(var i = 0; i < list.length; i++) { var filename = path.join(pathToMocks, name + '.json'); var fig = JSON.parse(fs.readFileSync(filename)); - var out = plotlyServerDom.window.Plotly.validate(fig.data, fig.layout); + var out = Plotly.validate(fig.data, fig.layout); fail = false; assert(name, out); diff --git a/tasks/test_plain_obj.js b/tasks/test_plain_obj.js index 20904337e80..007e20c05a2 100644 --- a/tasks/test_plain_obj.js +++ b/tasks/test_plain_obj.js @@ -1,19 +1,10 @@ -var jsdom = require('jsdom'); -var fs = require('fs'); - -var plotlyServerDom = new jsdom.JSDOM('', { runScripts: 'dangerously'}); -// Mock a few things that jsdom doesn't support out-of-the-box -plotlyServerDom.window.URL.createObjectURL = function() {}; - -// Run Plotly inside jsdom -var plotlyJsPath = require.resolve('../build/plotly.js'); -var plotlyJsSource = fs.readFileSync(plotlyJsPath, 'utf-8'); -plotlyServerDom.window.eval(plotlyJsSource); +var plotlyNode = require('./util/plotly_node'); +var Plotly = plotlyNode('build/plotly.js'); var assertValidate = function(fig, exp) { console.log(fig); - var errorList = plotlyServerDom.window.Plotly.validate(fig.data, fig.layout); + var errorList = Plotly.validate(fig.data, fig.layout); if(exp) { if(errorList !== undefined) throw 'should be valid:'; diff --git a/tasks/util/make_schema.js b/tasks/util/make_schema.js index f2e68036aad..47c1790afa4 100644 --- a/tasks/util/make_schema.js +++ b/tasks/util/make_schema.js @@ -1,22 +1,12 @@ var fs = require('fs'); var path = require('path'); -var JSDOM = require('jsdom').JSDOM; +var plotlyNode = require('./plotly_node'); module.exports = function makeSchema(plotlyPath, schemaPath) { return function() { - var plotlyjsCode = fs.readFileSync(plotlyPath, 'utf-8'); + var Plotly = plotlyNode(plotlyPath); - var w = new JSDOM('', {runScripts: 'dangerously'}).window; - - // jsdom by itself doesn't support getContext, and adding the npm canvas - // package is annoying and platform-dependent. - // see https://github.com/tmpvar/jsdom/issues/1782 - w.HTMLCanvasElement.prototype.getContext = function() { return null; }; - w.URL.createObjectURL = function() { return null; }; - - w.eval(plotlyjsCode); - - var plotSchema = w.Plotly.PlotSchema.get(); + var plotSchema = Plotly.PlotSchema.get(); var plotSchemaStr = JSON.stringify(plotSchema, null, 1); fs.writeFileSync(schemaPath, plotSchemaStr); diff --git a/tasks/util/plotly_node.js b/tasks/util/plotly_node.js new file mode 100644 index 00000000000..2bc34bce069 --- /dev/null +++ b/tasks/util/plotly_node.js @@ -0,0 +1,18 @@ +var fs = require('fs'); +var JSDOM = require('jsdom').JSDOM; + +var window = new JSDOM('', { + runScripts: 'dangerously' +}).window; + +// Mock things that jsdom doesn't support out-of-the-box +window.URL.createObjectURL = function() {}; + +module.exports = function plotlyNode(plotlyPath) { + // Execute source code by inserting a