From feca9ef16b4fa2ab2fb62a4bc2799d5321ab156d Mon Sep 17 00:00:00 2001 From: Zoupers <1171443643@qq.com> Date: Thu, 16 May 2024 10:11:41 +0800 Subject: [PATCH] =?UTF-8?q?karma=E5=8D=95=E6=B5=8B=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .npmrc | 2 +- packages/fineui/.gitignore | 2 + packages/fineui/karma.conf.js | 167 ++++++++++++++++++++++++++++++++ packages/fineui/package.json | 16 ++- packages/fineui/test/prepare.js | 3 + packages/fineui/test/utils.js | 45 +++++++++ 6 files changed, 232 insertions(+), 3 deletions(-) create mode 100644 packages/fineui/.gitignore create mode 100644 packages/fineui/karma.conf.js create mode 100644 packages/fineui/test/prepare.js create mode 100644 packages/fineui/test/utils.js diff --git a/.npmrc b/.npmrc index 529f7ef76..bdfef7c4e 100644 --- a/.npmrc +++ b/.npmrc @@ -1,2 +1,2 @@ -puppeteer_download_host=https://npm.taobao.org/mirrors +PUPPETEER_DOWNLOAD_BASE_URL=https://cdn.npmmirror.com/binaries/chrome-for-testing @fui:registry=https://npm.fineres.com/ \ No newline at end of file diff --git a/packages/fineui/.gitignore b/packages/fineui/.gitignore new file mode 100644 index 000000000..03b5477d9 --- /dev/null +++ b/packages/fineui/.gitignore @@ -0,0 +1,2 @@ +logs/* +coverage/* \ No newline at end of file diff --git a/packages/fineui/karma.conf.js b/packages/fineui/karma.conf.js new file mode 100644 index 000000000..96b48d9d1 --- /dev/null +++ b/packages/fineui/karma.conf.js @@ -0,0 +1,167 @@ +// Karma configuration +// Generated on Mon Nov 27 2017 11:16:26 GMT+0800 (中国标准时间) + +const os = require("os"); +const webpackConfig = require("./webpack/webpack.dev") + +process.env.CHROME_BIN = require("puppeteer").executablePath(); + +module.exports = function (config) { + config.set({ + + // base path that will be used to resolve all patterns (eg. files, exclude) + basePath: "", + + + // frameworks to use + // available frameworks: https://npmjs.org/browse/keyword/karma-adapter + frameworks: ["webpack", "mocha", "chai"], + + + // list of files / patterns to load in the browser + files: [].concat( + webpackConfig.entry.fineui, + ["test/**/*.js", "src/**/*.test.js"] + ), + + exclude: [ + "src/base/tree/ztree/jquery.ztree.core-3.5.js", + "src/base/tree/ztree/jquery.ztree.excheck-3.5.js", + "src/base/single/input/file.js", + "src/case/loading/loading_pane.js", + "src/base/tree/ztree/**/*.js", + "src/case/tree/ztree/*.js", + "src/widget/multitree/**/*.js", + "src/widget/multiselecttree/**/*.js", + "src/component/treevaluechooser/*.js" + ], + + + // preprocess matching files before serving them to the browser + // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor + preprocessors: { + "src/less/**/*.less": ["webpack"], + "ui/**/*.less": ["webpack"], + "src/*.js": ["webpack"], + "src/**/!(*.test).js": ["webpack"], + }, + + + // test results reporter to use + // possible values: 'dots', 'progress' + // available reporters: https://npmjs.org/browse/keyword/karma-reporter + reporters: ["progress", "coverage", "html"], + + browserDisconnectTolerance: 3, + browserDisconnectTimeout : 300000, + browserNoActivityTimeout : 300000, + + coverageReporter: { + // specify a common output directory + dir: "coverage/", + reporters: [ + // reporters not supporting the `file` property + { type: "html", subdir: "report-html" }, + { type: "json-summary", subdir: "report-json-summary" }, + { type: "cobertura", subdir: "report-cobertura"} + ] + }, + + htmlReporter: { + outputFile: "logs/test-report.html" + }, + + + // web server port + port: 9878, + + + // enable / disable colors in the output (reporters and logs) + colors: true, + + + // level of logging + // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG + logLevel: config.LOG_INFO, + + loggers: [ + { + type: "console" + }, + { + type: "file", + filename: "logs/karma.log" + } + ], + + + // enable / disable watching file and executing tests whenever any file changes + autoWatch: true, + + // you can define custom flags + customLaunchers: { + HeadlessChrome: { + base: "ChromeHeadless", + flags: [ + "--no-sandbox", + "--remote-debugging-port=9222", + "--enable-logging", + "--user-data-dir=./karma-chrome", + "--v=1", + "--disable-background-timer-throttling", + "--disable-renderer-backgrounding", + "--proxy-bypass-list=*", + "--disable-web-security", + "--disable-gpu", + ], + }, + ChromeDebug: { + base: "Chrome", + flags: [ + "--no-sandbox", + "--remote-debugging-port=9222", + "--enable-logging", + "--user-data-dir=./karma-chrome", + "--v=1", + "--disable-background-timer-throttling", + "--disable-renderer-backgrounding", + "--proxy-bypass-list=*", + "--disable-web-security", + "--disable-gpu", + ], + }, + }, + + // // start these browsers + // // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher + browsers: [os.platform() === "win32" + && parseFloat(os.release() + .split(".") + .slice(0, 2) + .join(".")) <= 6.1 + ? "HeadlessChrome" : "ChromeHeadless"], + + retryLimit: 30, + + captureTimeout: 30000, + + // Continuous Integration mode + // if true, Karma captures browsers, runs the tests and exits + singleRun: true, + + // Concurrency level + // how many browser should be started simultaneous + concurrency: Infinity, + + webpack: webpackConfig, + + plugins: [ + "karma-webpack", + "karma-mocha", + "karma-chai", + "karma-chrome-launcher", + "karma-coverage", + "karma-htmlfile-reporter", + ] + }); +}; diff --git a/packages/fineui/package.json b/packages/fineui/package.json index f467aa097..e88daf256 100644 --- a/packages/fineui/package.json +++ b/packages/fineui/package.json @@ -30,7 +30,8 @@ "tsc": "tsc", "link": "pnpm link --global", "build:biCss": "cross-env LESS_CONFIG_PATH=bi.lessconfig.json LESS_FILE_NAME=bi npm run webpack:css", - "build:jsyCss": "cross-env LESS_CONFIG_PATH=jsy.lessconfig.json LESS_FILE_NAME=jsy npm run webpack:css" + "build:jsyCss": "cross-env LESS_CONFIG_PATH=jsy.lessconfig.json LESS_FILE_NAME=jsy npm run webpack:css", + "test": "karma start" }, "files": [ "dist", @@ -57,7 +58,18 @@ "jquery": "3.6.3" }, "devDependencies": { + "chai": "4.2.0", "core-js": "^3.30.2", - "cross-env": "^7.0.3" + "cross-env": "^7.0.3", + "karma": "3.1.4", + "karma-chai": "0.1.0", + "karma-chrome-launcher": "2.2.0", + "karma-coverage": "1.1.2", + "karma-htmlfile-reporter": "^0.3.8", + "karma-mocha": "1.3.0", + "karma-typescript": "^5.5.4", + "karma-webpack": "^5.0.1", + "mocha": "5.2.0", + "puppeteer": "^22.8.2" } } \ No newline at end of file diff --git a/packages/fineui/test/prepare.js b/packages/fineui/test/prepare.js new file mode 100644 index 000000000..080634536 --- /dev/null +++ b/packages/fineui/test/prepare.js @@ -0,0 +1,3 @@ +while (BI.prepares && BI.prepares.length > 0) { + BI.prepares.shift()() +} diff --git a/packages/fineui/test/utils.js b/packages/fineui/test/utils.js new file mode 100644 index 000000000..0aa209d0a --- /dev/null +++ b/packages/fineui/test/utils.js @@ -0,0 +1,45 @@ +BI.Test = {}; +BI.extend(BI.Test, { + createWidget: function (widgetJson) { + var widget = BI.createWidget(BI.extend(widgetJson, { + root: true + })); + widget.element.appendTo("body"); + return widget; + }, + + /** + * 模拟一次输入框的keydown事件 + */ + triggerKeyDown: function (element, value, keyCode, callback) { + // keydown + var e = BI.$.Event("keydown"); + e.keyCode = keyCode; + element.trigger(e); + + // input + BI.isNotNull(value) && element.val(value); + var e1 = BI.$.Event("input"); + e1.originalEvent = {}; + e1.keyCode = keyCode; + element.trigger(e1); + + // keyup 至少等300ms后触发 + var e2 = BI.$.Event("keyup"); + e2.keyCode = keyCode; + element.trigger(e2); + BI.delay(function () { + callback(); + }, 300); + }, + + /** + * 模拟一次鼠标hover + */ + triggerMouseover: function (element, callback) { + // keydown + var e = BI.$.Event("mouseover"); + element.trigger(e); + callback && callback(); + } +}) \ No newline at end of file