Skip to content

Commit 6ada5b6

Browse files
committed
improve jupyter test design:
- make lib/server.js handle arbitrary html index and js test file (to test multiple ipynb fixutres) - use ipykernel to determine which python version nosetest is launched w/ - use domready to make sure that tests are ran after DOM is ready
1 parent 8cd7e96 commit 6ada5b6

File tree

4 files changed

+86
-23
lines changed

4 files changed

+86
-23
lines changed

plotly/tests/test_core/test_jupyter/lib/server.js

+43-11
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,48 @@ var http = require('http');
22
var url = require('url');
33
var fs = require('fs');
44
var path = require('path');
5+
56
var ecstatic = require('ecstatic');
67
var browserify = require('browserify');
78
var cheerio = require('cheerio');
8-
var chrome = require('chrome-launch');
99
var tapParser = require('tap-parser');
10+
var chrome = require('chrome-launch');
1011

1112
var PORT = 8080;
1213
var PATH_ROOT = path.join(__dirname, '..');
13-
var PATH_FIXTURES = path.join(PATH_ROOT, 'fixtures');
14-
var PATH_INDEX = path.join(PATH_FIXTURES, 'test.html');
15-
var PATH_INDEX_STUB = path.join(PATH_FIXTURES, 'test.tmp.html');
16-
var PATH_TEST_FILE = path.join(PATH_ROOT, 'test.js');
14+
var PATH_INDEX_STUB = path.join(PATH_ROOT, 'index.tmp.html');
1715
var PATH_TEST_BUNDLE = path.join(PATH_ROOT, 'test.tmp.js');
18-
var URL = 'http://localhost:' + PORT + '/fixtures/test.tmp.html';
16+
17+
var URL = 'http://localhost:' + PORT + '/index.tmp.html';
1918
var EXIT_CODE = 0;
2019

21-
// main
22-
stubIndex()
23-
.then(bundleTests)
24-
.then(startServer)
25-
.then(launch)
20+
if(process.argv.length !== 4) {
21+
throw new Error('must provide path to html and js files');
22+
}
23+
24+
var PATH_INDEX = process.argv[2];
25+
var PATH_TEST_FILE = process.argv[3];
26+
27+
main();
28+
29+
function main() {
30+
scanInput();
31+
32+
stubIndex()
33+
.then(bundleTests)
34+
.then(startServer)
35+
.then(launch);
36+
}
37+
38+
function scanInput() {
39+
var reqFiles = [PATH_INDEX, PATH_TEST_FILE];
40+
41+
reqFiles.forEach(function(filePath) {
42+
if(!doesFileExist(filePath)) {
43+
throw new Error(filePath + ' does not exist');
44+
}
45+
});
46+
}
2647

2748
function stubIndex() {
2849
return new Promise(function(resolve, reject) {
@@ -91,3 +112,14 @@ function removeBuildFiles() {
91112
fs.unlinkSync(PATH_INDEX_STUB);
92113
fs.unlinkSync(PATH_TEST_BUNDLE);
93114
}
115+
116+
function doesFileExist(filePath) {
117+
try {
118+
if(fs.statSync(filePath).isFile()) return true;
119+
}
120+
catch(e) {
121+
return false;
122+
}
123+
124+
return false;
125+
}

plotly/tests/test_core/test_jupyter/lib/tape-wrapper.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
'use strict';
2+
13
var test = require('tape');
24
var xhr = require('xhr');
5+
var domready = require('domready');
36

47
var cnt = 0;
58
var noop = function() {};
@@ -19,4 +22,12 @@ test.onFinish(function() {
1922
post('done');
2023
});
2124

25+
test('should not crash browser', function(t) {
26+
t.plan(1);
27+
28+
domready(function() {
29+
t.pass('domready');
30+
});
31+
});
32+
2233
module.exports = test;

plotly/tests/test_core/test_jupyter/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"browserify": "^13.1.0",
1212
"cheerio": "^0.20.0",
1313
"chrome-launch": "^1.1.4",
14+
"domready": "^1.0.8",
1415
"ecstatic": "^2.1.0",
1516
"tap-parser": "^2.0.0",
1617
"tape": "^4.6.0",

plotly/tests/test_core/test_jupyter/test_jupyter.py

+31-12
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,49 @@
22
test__jupyter
33
44
"""
5-
from __future__ import absolute_import
6-
75
import nbformat
86
from nbconvert import HTMLExporter
97
from nbconvert.preprocessors import ExecutePreprocessor
8+
from ipykernel import kernelspec
109

1110
from unittest import TestCase
1211
from os import path
1312
import subprocess
1413

15-
import plotly
16-
1714
PATH_ROOT = path.dirname(__file__)
1815
PATH_FIXTURES = path.join(PATH_ROOT, 'fixtures')
19-
PATH_TEST_NB = path.join(PATH_FIXTURES, 'test.ipynb')
20-
PATH_TEST_HTML = path.join(PATH_FIXTURES, 'test.html')
16+
PATH_JS_TESTS = path.join(PATH_ROOT, 'js_tests')
17+
2118

19+
class Common(TestCase):
20+
__test__ = False
21+
name = None
2222

23-
class PlotlyJupyterTestCase(TestCase):
2423
def setUp(self):
25-
with open(PATH_TEST_NB, 'r') as f:
24+
self.path_test_nb = path.join(PATH_FIXTURES, self.name + '.ipynb')
25+
self.path_test_html = path.join(PATH_FIXTURES, self.name + '.html')
26+
self.path_test_js = path.join(PATH_JS_TESTS, self.name + '.js')
27+
28+
self.kernel_name = kernelspec.KERNEL_NAME
29+
30+
with open(self.path_test_nb, 'r') as f:
2631
self.nb = nbformat.read(f, as_version=4)
2732

28-
self.ep = ExecutePreprocessor(timeout=600)
33+
self.ep = ExecutePreprocessor(timeout=600,
34+
kernel_name=self.kernel_name)
35+
2936
self.html_exporter = HTMLExporter()
3037

3138
self.ep.preprocess(self.nb, {'metadata': {'path': '.'}})
3239
(self.body, _) = self.html_exporter.from_notebook_node(self.nb)
3340

34-
with open(PATH_TEST_HTML, 'w') as f:
41+
with open(self.path_test_html, 'w') as f:
3542
f.write(self.body)
3643

37-
def test_one(self):
38-
proc = subprocess.Popen(['npm', 'test'],
44+
def test_js(self):
45+
cmd = ['npm', 'test', '--', self.path_test_html, self.path_test_js]
46+
47+
proc = subprocess.Popen(cmd,
3948
cwd=PATH_ROOT,
4049
stdout=subprocess.PIPE,
4150
stderr=subprocess.PIPE)
@@ -44,3 +53,13 @@ def test_one(self):
4453

4554
if stderr:
4655
self.fail('One or more javascript test failed')
56+
57+
58+
class PlotlyJupyterConnectedFalseTestCase(Common):
59+
__test__ = True
60+
name = 'connected_false'
61+
62+
63+
class PlotlyJupyterConnectedTrueTestCase(Common):
64+
__test__ = True
65+
name = 'connected_true'

0 commit comments

Comments
 (0)