mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
1
2
"use strict";const fs=require("fs"),parser=require("@babel/parser"),generate=require("@babel/generator").default,traverse=require("@babel/traverse").default,types=require("@babel/types"),loaderUtils=require("loader-utils"),{colorconsole:colorconsole}=require("@hap-toolkit/shared-utils"),{parseFragmentsWithCache:parseFragmentsWithCache}=require("@hap-toolkit/compiler");function generateFn(e=!1){let t="function () {\n    this.$options = this._options || this.$options\n    if (this.$options._descriptor) {\n      this.$options._descriptor['back'] = { access: 'public' }\n    }\n\n    // 测试执行:开始时间,结束事件\n    global.CASE_TEST_START = global.CASE_TEST_START || 1000\n    global.CASE_TEST_TIMEOUT = global.CASE_TEST_TIMEOUT || 2000\n\n    global.mocha = new Mocha({ reporter: 'json', timeout: global.CASE_TEST_TIMEOUT })\n    mocha.ui('bdd')\n    mocha.suite.emit('pre-require', global, null, mocha)\n    setTimeout(() => {\n      // 记录测试用例\n      typeof fnTestCase === 'function' && fnTestCase(this)\n      const mochaRunner = mocha.run(() => {\n        if (mochaRunner) {\n          // 标题\n          mochaRunner.testResults.stats.title = mocha.suite.suites && mocha.suite.suites[0] && mocha.suite.suites[0].title\n          console.info('testResults: ', JSON.stringify(mochaRunner.testResults))\n          pushData('pageTestList', mochaRunner.testResults)\n\n          // 显示结果\n          const stats = mochaRunner.testResults.stats\n          this.$page.setTitleBar({ text: '通过/全部: ' + stats.passes + '/' + stats.tests })\n        }\n\n        // 是否返回\n        if (this.back !== 'false') {\n          console.info('拥有关联测试用例,测试完毕,返回到之前的页面')\n          history.back()\n        }\n      })\n    }, global.CASE_TEST_START)\n  }";return e||(t="function () {\n      this.$options = this._options || this.$options\n      // 允许back被外部覆盖\n      if (this.$options._descriptor) {\n        this.$options._descriptor['back'] = { access: 'public' }\n      }\n\n      // 测试执行:开始时间,结束事件\n      global.CASE_TEST_START = global.CASE_TEST_START || 1000\n      global.CASE_TEST_TIMEOUT = global.CASE_TEST_TIMEOUT || 2000\n\n      setTimeout(() => {\n        // 是否返回\n        if (this.back !== 'false') {\n          console.info('没有关联测试用例,直接返回到之前的页面')\n          history.back()\n        }\n      }, global.CASE_TEST_START)\n    }"),parser.parseExpression(t).body}module.exports=function(e){this.cacheable&&this.cacheable();const t=loaderUtils.parseQuery(this.resourceQuery||"?"),s=loaderUtils.getOptions(this),n=s&&s.hookName||"onCreate",o=parseFragmentsWithCache(e).script[0]||{content:""},r=this.resourcePath.replace(/([\\/])(src)([\\/])/,(...e)=>e[1]+"test"+e[3]).replace(/\.\w{2,5}$/,".js"),i=parser.parse(o.content,{sourceType:"module",plugins:["jsx"]});if("app"===t.uxType){const e=types.importDeclaration([],types.stringLiteral("@hap-toolkit/packager/lib/common/app.js"));i.program.body.unshift(e)}else{if("page"!==t.uxType)return e;{const e=fs.existsSync(r),s=r.match(/[\\/]test[\\/](.*)/)[1];if(e){const e=types.importDeclaration([types.importDefaultSpecifier(types.identifier("fnTestCase"))],types.stringLiteral(r));i.program.body.unshift(e),"script"===t.type&&colorconsole.info(`[INFO] 脚本注入测试用例:${s}`)}traverse(i,{ExportDefaultDeclaration(s){if("page"!==t.uxType)return;const o=generateFn(e);let r;const i=s.node.declaration;if("ObjectExpression"!==i.type){const e=s.scope.generateUidIdentifier("replace");s.node.declaration=e;const t=types.variableDeclaration("const",[types.variableDeclarator(e,i)]),r=types.assignmentExpression("=",types.memberExpression(e,types.identifier(n)),types.functionExpression(types.identifier(n),[],o));return s.insertBefore(t),void s.insertBefore(r)}r=i.properties;let a,c=!1;if(r.forEach((e,t)=>{e.key.name===n&&!0===e.method&&(a=t,c=!0)}),c)return void(r[a].body.body=r[a].body.body.concat(o.body));const p=types.identifier(n),l=o,u=types.objectMethod("method",p,[],l);s.node.declaration.properties.push(u)}})}}return e=e.replace(/<script.*?>([\s\S]+)<\/script>/,(function(){return`<script>\n${generate(i).code}\n<\/script>`}))};
//# sourceMappingURL=inject-suite-loader.js.map