diff --git a/.eslintignore b/.eslintignore index 22c20bea9..6a170b7e5 100644 --- a/.eslintignore +++ b/.eslintignore @@ -6,6 +6,7 @@ /tests/compile/* /tests/jsunit/* /tests/generators/* +/tests/test_runner.js /tests/workspace_svg/* /generators/* /demos/* diff --git a/package.json b/package.json index d843adfd5..d2bc48a2b 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "scripts": { "lint": "eslint .", "pretest": "tests/scripts/test_setup.sh", - "test": "node tests/jsunit/test_runner.js" + "test": "node tests/test_runner.js" }, "license": "Apache-2.0", "private": true, diff --git a/tests/jsunit/run_jsunit_tests_in_browser.js b/tests/jsunit/run_jsunit_tests_in_browser.js new file mode 100644 index 000000000..ae14cc394 --- /dev/null +++ b/tests/jsunit/run_jsunit_tests_in_browser.js @@ -0,0 +1,76 @@ +/** + * @license + * Visual Blocks Editor + * + * Copyright 2018 Google Inc. + * https://developers.google.com/blockly/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @fileoverview Node.js script to run JsUnit tests in Chrome, via webdriver. + */ +var webdriverio = require('webdriverio'); + +/** + * Runs the JsUnit tests in this directory in Chrome. It uses webdriverio to + * launch Chrome and load index.html. Outputs a summary of the test results + * to the console. + * @throws If any error occurs when attempting to run the tests. + */ +function runJsUnitTestsInBrowser() { + var options = { + desiredCapabilities: { + browserName: 'chrome' + } + }; + + //TODO: change pause to waitunitl + var browser = webdriverio + .remote(options) + .init() + .url('file://' + __dirname + '/index.html').pause(5000); + + + browser + .getHTML('#closureTestRunnerLog') + .then(function(result) { + // call js to parse html + var regex = /[\d]+\spassed,\s([\d]+)\sfailed./i; + var numOfFailure = regex.exec(result)[1]; + var regex2 = /Unit Tests for Blockly .*]/; + var testStatus = regex2.exec(result)[0]; + console.log('============Blockly Unit Test Summary================='); + console.log(testStatus); + var regex3 = /\d+ passed,\s\d+ failed/; + var detail = regex3.exec(result)[0]; + console.log(detail); + console.log('============Blockly Unit Test Summary================='); + if ( parseInt(numOfFailure) !== 0) { + console.log(result); + process.exit(1); + } + }) +} + +module.exports = runJsUnitTestsInBrowser; + +if (require.main === module) { + try { + runJsUnitTestsInBrowser(); + } catch(errorStr) { + console.error(errorStr); + process.exit(1); + } +} diff --git a/tests/jsunit/test_runner.js b/tests/jsunit/test_runner.js deleted file mode 100644 index bfd7c0bd3..000000000 --- a/tests/jsunit/test_runner.js +++ /dev/null @@ -1,35 +0,0 @@ -var webdriverio = require('webdriverio'); -var options = { - desiredCapabilities: { - browserName: 'chrome' - } -}; - -var path = process.cwd(); -//TODO: change pause to waitunitl -var browser = webdriverio - .remote(options) - .init() - .url("file://" + path + "/tests/jsunit/index.html").pause(5000); - - -browser -.getHTML('#closureTestRunnerLog') -.then(function(result) { - // call js to parse html - var regex = /[\d]+\spassed,\s([\d]+)\sfailed./i; - var numOfFailure = regex.exec(result)[1]; - var regex2 = /Unit Tests for Blockly .*]/; - var testStatus = regex2.exec(result)[0]; - console.log("============Blockly Unit Test Summary================="); - console.log(testStatus); - var regex3 = /\d+ passed,\s\d+ failed/; - var detail = regex3.exec(result)[0]; - console.log(detail); - console.log("============Blockly Unit Test Summary================="); - if ( parseInt(numOfFailure) !== 0) { - console.log(result); - process.exit(1); - } -}) -.catch(function(err) { console.log(err); process.exit(1); }); diff --git a/tests/test_runner.js b/tests/test_runner.js new file mode 100644 index 000000000..e21f3ec0a --- /dev/null +++ b/tests/test_runner.js @@ -0,0 +1,44 @@ +/** + * @license + * Visual Blocks Editor + * + * Copyright 2018 Google Inc. + * https://developers.google.com/blockly/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @fileoverview Node.js script to run all tests. + */ +if (require.main !== module) { + throw __filename + ' must be called directly.'; +} + +var testFns = [ + require('./jsunit/run_jsunit_tests_in_browser') + ]; + +var errored = false; +testFns.forEach((testFn) => { + try { + testFn(); + } catch (errorStr) { + errored = true; + console.error(errorStr + '\n\n'); + } +}); + +if (errored) { + process.exit(1); +}