From d0c1403957f75350550ac8fb47ac2ca94239e7a0 Mon Sep 17 00:00:00 2001 From: Dannon Baker Date: Mon, 10 Aug 2020 06:37:28 -0400 Subject: [PATCH] Update mocha tests, use bdd style assertions consistently --- client/package.json | 4 +-- .../src/components/Datatypes/test_fixtures.js | 5 +-- .../DatasetCollectionDialog.test.js | 4 +-- .../SelectionDialog/WorkflowDialog.test.js | 2 +- .../src/components/Upload/UploadModal.test.js | 2 +- client/test/karma/karma.config.base.js | 28 +++++++++-------- client/test/karma/karma.config.mocha.js | 31 +++++++++---------- client/test/karma/karma.config.qunit.js | 16 +++------- client/test/karma/webpack.config.qunit.js | 28 +++++++---------- client/test/karma/webpack.config.unittest.js | 24 +++++++------- client/test/mocha/tests/rules_dsl_spec.yml | 2 +- client/test/mocha/tests/rules_tests.js | 15 ++++----- client/test/qunit/test-app.js | 4 +-- 13 files changed, 75 insertions(+), 90 deletions(-) diff --git a/client/package.json b/client/package.json index 0da185fd18b..d200780ed47 100644 --- a/client/package.json +++ b/client/package.json @@ -81,8 +81,8 @@ "styleguide:build": "vue-styleguidist build", "test": "yarn run test-mocha && yarn run test-qunit", "test-watch": "yarn run test-mocha --no-single-run", - "test-qunit": "karma start karma/karma.config.qunit.js", - "test-mocha": "karma start karma/karma.config.mocha.js", + "test-qunit": "karma start test/karma/karma.config.qunit.js", + "test-mocha": "karma start test/karma/karma.config.mocha.js", "eslint": "eslint -c .eslintrc.js src --ext .js,.vue" }, "devDependencies": { diff --git a/client/src/components/Datatypes/test_fixtures.js b/client/src/components/Datatypes/test_fixtures.js index 39897c7480a..180f09b4cb1 100644 --- a/client/src/components/Datatypes/test_fixtures.js +++ b/client/src/components/Datatypes/test_fixtures.js @@ -1,5 +1,6 @@ -import DatatypesMappingJson from "qunit/test-data/json/datatypes.mapping.json"; -import DatatypesJson from "qunit/test-data/json/datatypes.json"; +// TODO: Swap these awful relative paths to an alias +import DatatypesMappingJson from "../../../test/qunit/test-data/json/datatypes.mapping.json"; +import DatatypesJson from "../../../test/qunit/test-data/json/datatypes.json"; import { DatatypesMapperModel } from "./model.js"; export const typesAndMappingResponse = { diff --git a/client/src/components/SelectionDialog/DatasetCollectionDialog.test.js b/client/src/components/SelectionDialog/DatasetCollectionDialog.test.js index 50dddfe3624..550f8082e65 100644 --- a/client/src/components/SelectionDialog/DatasetCollectionDialog.test.js +++ b/client/src/components/SelectionDialog/DatasetCollectionDialog.test.js @@ -1,8 +1,8 @@ import MockAdapter from "axios-mock-adapter"; import axios from "axios"; import DatasetCollectionDialog from "./DatasetCollectionDialog.vue"; -import { setupTestGalaxy } from "qunit/test-app"; -import SelectionDialog from "./SelectionDialog.vue"; +import { setupTestGalaxy } from "../../../test/qunit/test-app"; + import SelectionDialog from "./SelectionDialog.vue"; import { shallowMount, createLocalVue } from "@vue/test-utils"; diff --git a/client/src/components/SelectionDialog/WorkflowDialog.test.js b/client/src/components/SelectionDialog/WorkflowDialog.test.js index 409e371fff4..7514b0e4845 100644 --- a/client/src/components/SelectionDialog/WorkflowDialog.test.js +++ b/client/src/components/SelectionDialog/WorkflowDialog.test.js @@ -3,7 +3,7 @@ import axios from "axios"; import WorkflowDialog from "./WorkflowDialog.vue"; import { __RewireAPI__ as rewire } from "components/Workflow/services"; import SelectionDialog from "./SelectionDialog.vue"; -import { setupTestGalaxy } from "qunit/test-app"; +import { setupTestGalaxy } from "../../../test/qunit/test-app"; import { shallowMount, createLocalVue } from "@vue/test-utils"; diff --git a/client/src/components/Upload/UploadModal.test.js b/client/src/components/Upload/UploadModal.test.js index 89f0e30c26f..691bce07e3a 100644 --- a/client/src/components/Upload/UploadModal.test.js +++ b/client/src/components/Upload/UploadModal.test.js @@ -1,7 +1,7 @@ import MockAdapter from "axios-mock-adapter"; import axios from "axios"; import UploadModal from "./UploadModal.vue"; -import { setupTestGalaxy } from "qunit/test-app"; +import { setupTestGalaxy } from "../../../test/qunit/test-app"; import { shallowMount, createLocalVue } from "@vue/test-utils"; diff --git a/client/test/karma/karma.config.base.js b/client/test/karma/karma.config.base.js index 368a6551446..96e1427b8a2 100644 --- a/client/test/karma/karma.config.base.js +++ b/client/test/karma/karma.config.base.js @@ -4,30 +4,32 @@ const webpackConfigFunc = require("./webpack.config.unittest"); -module.exports = config => { - +module.exports = (config) => { // Karma does not execute the webpack config function by default - const webpackConfig = webpackConfigFunc({}, { - mode: "testing" - }); + const webpackConfig = webpackConfigFunc( + {}, + { + mode: "testing", + } + ); return { - basePath: "../src", + basePath: "../../src", browsers: ["ChromeHeadlessNoSandbox"], client: { - captureConsole: true + captureConsole: true, }, failOnEmptyTestSuite: false, singleRun: true, webpack: webpackConfig, webpackMiddleware: { - noInfo: true + noInfo: true, }, customLaunchers: { ChromeHeadlessNoSandbox: { base: "ChromeHeadless", - flags: ["--no-sandbox"] - } - } - } -} + flags: ["--no-sandbox"], + }, + }, + }; +}; diff --git a/client/test/karma/karma.config.mocha.js b/client/test/karma/karma.config.mocha.js index c4248b8df3f..71caf20d2e8 100644 --- a/client/test/karma/karma.config.mocha.js +++ b/client/test/karma/karma.config.mocha.js @@ -14,30 +14,31 @@ const defaultFiles = [ // component/module tests "**/*.test.js", // pre-existing rules definition tests - "**/mocha/tests/*_tests.js" + "../test/mocha/tests/rules_tests.js", ]; function getTestFiles() { // check for user-supplied list - let userPatterns = getUserTestGlobs(); - let patterns = userPatterns.length ? userPatterns : defaultFiles; - return patterns.map(pattern => ({ pattern, watched: true})); + const userPatterns = getUserTestGlobs(); + const patterns = userPatterns.length ? userPatterns : defaultFiles; + return patterns.map((pattern) => ({ pattern, watched: true })); } // command line arg "watch-only" can be a list of file globs // for karma to watch function getUserTestGlobs() { - let userGlobs = process.argv.find(s => s.startsWith("watch-only")); + const userGlobs = process.argv.find((s) => s.startsWith("watch-only")); return userGlobs ? processUserGlobs(userGlobs) : []; } // split command line arg into an array function processUserGlobs(val) { let result = []; - let fileListString = val.split("=")[1]; + const fileListString = val.split("=")[1]; if (fileListString) { - result = fileListString.split(",") - .map(s => s.trim()) + result = fileListString + .split(",") + .map((s) => s.trim()) .map(checkGlobPrefix); } return result; @@ -49,23 +50,19 @@ function checkGlobPrefix(glob) { } module.exports = function (config) { - const baseConfig = baseKarmaConfig(config); - let files = [ - "../../node_modules/@babel/polyfill/dist/polyfill.js", - ...getTestFiles() - ]; + const files = ["../node_modules/@babel/polyfill/dist/polyfill.js", ...getTestFiles()]; - let settings = Object.assign({}, baseConfig, { + const settings = Object.assign({}, baseConfig, { files, preprocessors: { - "**/*.js": ["webpack"] + "**/*.js": ["webpack"], }, exclude: ["**/qunit/*"], reporters: ["mocha"], - frameworks: ["mocha", "chai"] + frameworks: ["mocha", "chai"], }); config.set(settings); -} +}; diff --git a/client/test/karma/karma.config.qunit.js b/client/test/karma/karma.config.qunit.js index df3481d8124..b038c52169f 100644 --- a/client/test/karma/karma.config.qunit.js +++ b/client/test/karma/karma.config.qunit.js @@ -11,29 +11,23 @@ const baseKarmaConfig = require("./karma.config.base"); -const testFiles = [ - "../../node_modules/@babel/polyfill/dist/polyfill.js", - "qunit/test.js" -]; +const testFiles = ["node_modules/@babel/polyfill/dist/polyfill.js", "../qunit/test.js"]; -const assets = [ - "qunit/assets/*.css" -]; +const assets = ["../qunit/assets/*.css"]; -let preprocessors = testFiles.reduce((result, path) => { +const preprocessors = testFiles.reduce((result, path) => { result[path] = ["webpack"]; return result; }, {}); module.exports = function (config) { - const baseConfig = baseKarmaConfig(config); - let settings = Object.assign({}, baseConfig, { + const settings = Object.assign({}, baseConfig, { files: testFiles.concat(assets), preprocessors: preprocessors, frameworks: ["polyfill", "qunit"], - singleRun: true + singleRun: true, }); config.set(settings); diff --git a/client/test/karma/webpack.config.qunit.js b/client/test/karma/webpack.config.qunit.js index 3e10735fac2..49e559ea620 100644 --- a/client/test/karma/webpack.config.qunit.js +++ b/client/test/karma/webpack.config.qunit.js @@ -4,47 +4,43 @@ * TODO: Replace these tests with like... actual unit tests. */ -const baseConfig = require("../webpack.config"); +const baseConfig = require("../../webpack.config"); module.exports = (env, argv) => { - // load base config - let wpConfig = baseConfig(env, argv); + const wpConfig = baseConfig(env, argv); wpConfig.mode = "production"; wpConfig.entry = () => ({}); const switchPlugin = (reMatcher, replacement) => { - - let scrubLoader = (obj) => - (typeof obj == "string" && obj.match(reMatcher)) - ? Object.assign({}, replacement) - : obj + const scrubLoader = (obj) => + typeof obj == "string" && obj.match(reMatcher) ? Object.assign({}, replacement) : obj; return (rule) => { if (rule.use && rule.use.length) { rule.use = rule.use.map(scrubLoader); } return rule; - } - } + }; + }; // Remove MiniCssExtractPlugin loader references // replace mini-css-extract plugin with basic style loader // Honestly I'm not clear on why we process css at all for unit tests const scrubLoaderRules = (module) => { - let rePluginMatch = /mini-css-extract-plugin/; - let styleLoader = { loader: "style-loader" }; - let processor = switchPlugin(rePluginMatch, styleLoader); + const rePluginMatch = /mini-css-extract-plugin/; + const styleLoader = { loader: "style-loader" }; + const processor = switchPlugin(rePluginMatch, styleLoader); module.rules = module.rules.map(processor); - } + }; // Remove mini-css-extract loader references scrubLoaderRules(wpConfig.module); // remove MiniCSSExtract Plugin - wpConfig.plugins = wpConfig.plugins.filter(p => { + wpConfig.plugins = wpConfig.plugins.filter((p) => { return p.constructor.name != "MiniCssExtractPlugin"; }); return wpConfig; -} +}; diff --git a/client/test/karma/webpack.config.unittest.js b/client/test/karma/webpack.config.unittest.js index 63466d2b093..aadbeaf0ed3 100644 --- a/client/test/karma/webpack.config.unittest.js +++ b/client/test/karma/webpack.config.unittest.js @@ -4,35 +4,33 @@ */ const merge = require("webpack-merge"); -const baseConfig = require("../webpack.config"); +const baseConfig = require("../../webpack.config"); module.exports = (env, argv) => { - const wpConfig = baseConfig(env, argv); wpConfig.mode = "development"; wpConfig.entry = () => ({}); // Don't need any assets for unit testing - let ignoreAssetLoaders = { - rules:[ + const ignoreAssetLoaders = { + rules: [ { test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)(\?.*$|$)/, - loader: "ignore-loader" + loader: "ignore-loader", }, { test: /\.css$/, - loader: "ignore-loader" + loader: "ignore-loader", }, { test: /\.scss$/, - loader: "ignore-loader" - } - ] + loader: "ignore-loader", + }, + ], }; wpConfig.module = merge.smart(wpConfig.module, ignoreAssetLoaders); - // Using babel-plugin-rewire to handle dependency mocking since webpack 4 // exports immutable bindings for ES modules but we still need a way to // overwrite dependencies during unit-testing. @@ -41,13 +39,13 @@ module.exports = (env, argv) => { // node-modules, without our exceptions to support IE as in parent webpack // config. - wpConfig.module.rules = wpConfig.module.rules.map(rule => { + wpConfig.module.rules = wpConfig.module.rules.map((rule) => { if (rule.loader == "babel-loader") { - rule.exclude = [/(node_modules\/)/] + rule.exclude = [/(node_modules\/)/]; rule.options.plugins.push("rewire"); } return rule; }); return wpConfig; -} +}; diff --git a/client/test/mocha/tests/rules_dsl_spec.yml b/client/test/mocha/tests/rules_dsl_spec.yml index bb0210e9aa6..539495f6d64 120000 --- a/client/test/mocha/tests/rules_dsl_spec.yml +++ b/client/test/mocha/tests/rules_dsl_spec.yml @@ -1 +1 @@ -../../../../../lib/galaxy_test/base/data/rules_dsl_spec.yml \ No newline at end of file +../../../../lib/galaxy_test/base/data/rules_dsl_spec.yml \ No newline at end of file diff --git a/client/test/mocha/tests/rules_tests.js b/client/test/mocha/tests/rules_tests.js index 1148cad9cab..6b484b9fd60 100644 --- a/client/test/mocha/tests/rules_tests.js +++ b/client/test/mocha/tests/rules_tests.js @@ -1,11 +1,8 @@ -import chai from "chai"; import RuleDefs from "mvc/rules/rule-definitions"; import SPEC_TEST_CASES from "json-loader!yaml-loader!./rules_dsl_spec.yml"; -const RULES = RuleDefs.RULES; - function applyRules(rules, data, sources) { - let columns = []; + const columns = []; if (data[0]) { for (const index in data[0]) { columns.push("new"); @@ -20,9 +17,9 @@ function itShouldConform(specTestCase, i) { doc = " - " + specTestCase.doc; } it("should pass conformance test case " + i + " (from rules_dsl_spec.yml)" + doc, function () { - chai.assert.property(specTestCase, "rules"); + expect(specTestCase).to.have.property("rules"); if (specTestCase.initial) { - chai.assert.property(specTestCase, "final"); + expect(specTestCase).to.have.property("final"); const rules = specTestCase.rules; const initial = specTestCase.initial; @@ -31,12 +28,12 @@ function itShouldConform(specTestCase, i) { const final = applyRules(rules, initial.data, initial.sources); const finalData = final.data; const finalSources = final.sources; - chai.assert.deepEqual(finalData, expectedFinal.data); + expect(finalData).to.deep.equal(expectedFinal.data); if (expectedFinal.sources !== undefined) { - chai.assert.deepEqual(finalSources, expectedFinal.sources); + expect(finalSources).to.deep.equal(expectedFinal.sources); } } else { - chai.assert(specTestCase.error); + expect(specTestCase.error).to.be.true; // TODO: test these... } }); diff --git a/client/test/qunit/test-app.js b/client/test/qunit/test-app.js index 13b3621103e..a1bbfd07602 100644 --- a/client/test/qunit/test-app.js +++ b/client/test/qunit/test-app.js @@ -9,8 +9,8 @@ import sinon from "sinon"; import Backbone from "backbone"; import { setGalaxyInstance } from "app"; import { getAppRoot } from "onload"; -import galaxyOptions from "qunit/test-data/bootstrapped"; -import serverdata from "qunit/test-data/fakeserver"; +import galaxyOptions from "./test-data/bootstrapped"; +import serverdata from "./test-data/fakeserver"; import "./assets/base.css"; export function setupTestGalaxy(galaxyOptions_ = null) {