@aiot-toolkit/parser 2.0.6-beta.2 → 2.0.6-beta.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,7 +8,7 @@ var t = _interopRequireWildcard(require("@babel/types"));
8
8
  var parser = _interopRequireWildcard(require("@babel/parser"));
9
9
  var _path = _interopRequireDefault(require("path"));
10
10
  var _fsExtra = _interopRequireDefault(require("fs-extra"));
11
- var _sharedUtils = require("@aiot-toolkit/shared-utils");
11
+ var _UxUtil = _interopRequireDefault(require("../../../utils/UxUtil"));
12
12
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
13
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
14
14
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
@@ -62,10 +62,6 @@ function addAppTestCode(path) {
62
62
  * b. 设置测试函数: 如果 onTestStart 方法已存在,则在其内部添加测试代码;否则,添加 onTestStart方法
63
63
  *
64
64
  *
65
- * 2. 如果启用自动路由,且当前页面是首页
66
- * a. 从routerList 中过滤出有测试文件的路由
67
- * b. 在末尾添加 $generateAutoRunTest
68
- * c. 在onShow 方法中调用 $generateAutoRunTest
69
65
  *
70
66
  *
71
67
  * @description 示例
@@ -91,12 +87,20 @@ function addAppTestCode(path) {
91
87
  * @param opts
92
88
  */
93
89
  function addPageTestCode(path, opts) {
94
- const e2eConfig = getE2eConfig(opts);
95
- const testFilePath = getTestFilePath(opts.options.filePath, opts, e2eConfig);
90
+ if (!opts.compilerOption.e2eConfigPath) {
91
+ return;
92
+ }
93
+ const e2eConfig = _UxUtil.default.getE2eConfig({
94
+ projectPath: opts.options.projectPath,
95
+ e2eConfigPath: opts.compilerOption.e2eConfigPath
96
+ });
97
+ const filePath = opts.options.filePath;
98
+ const testFilePath = getTestFilePath(filePath, opts, e2eConfig);
99
+ const testCaseConfig = getTestcaseConfig(filePath, opts, e2eConfig);
96
100
  // 1
97
101
  if (_fsExtra.default.existsSync(testFilePath)) {
98
102
  // 1.a
99
- const relativePath = _path.default.relative(_path.default.dirname(opts.options.filePath), testFilePath);
103
+ const relativePath = _path.default.relative(_path.default.dirname(filePath), testFilePath);
100
104
  const vmImport = t.importDeclaration([t.importDefaultSpecifier(t.identifier('fnTestCase'))], t.stringLiteral(relativePath));
101
105
  if (path.parent.type === 'Program') {
102
106
  path.parent.body.unshift(vmImport);
@@ -105,33 +109,21 @@ function addPageTestCode(path, opts) {
105
109
  // 1.b
106
110
  if (path.node.declaration.type === 'ObjectExpression') {
107
111
  const properties = path.node.declaration.properties;
108
- insertFun('onTestStart', generateTestFn(), properties);
109
- }
110
- }
111
-
112
- // 2
113
- if (isIndexPage(opts) && e2eConfig.autoRoute) {
114
- let {
115
- routerList,
116
- pageSleep
117
- } = e2eConfig.autoRoute;
118
- routerList = routerList?.filter(item => hasTestFile(item, opts, e2eConfig));
119
- if (!routerList?.length) {
120
- console.log('missing autoRoute.routerList');
121
- return;
122
- }
123
- // 2.a
124
-
125
- if (path.node.declaration.type === 'ObjectExpression') {
126
- const properties = path.node.declaration.properties;
127
-
128
- // 2.b
129
- insertFun('$generateAutoRunTest', generateAutoRouterFn(pageSleep, routerList), properties);
130
- // 2.c
131
- insertFun('onShow', generateAutoRouterInOnShow(), properties);
112
+ insertFn('onTestStart', generateTestFn(testCaseConfig), properties);
132
113
  }
133
114
  }
134
115
  }
116
+ function getTestcaseConfig(filePath, opts, e2eConfig) {
117
+ const {
118
+ projectPath
119
+ } = opts.options;
120
+ const {
121
+ sourceRoot
122
+ } = opts.compilerOption;
123
+ const relativeSrc = _path.default.relative(_path.default.join(projectPath, sourceRoot || ''), filePath);
124
+ const pageName = _path.default.dirname(relativeSrc).replace(/\\/g, _path.default.posix.sep);
125
+ return e2eConfig.testcases?.find(item => item.name === pageName);
126
+ }
135
127
 
136
128
  /**
137
129
  * 插入函数
@@ -141,7 +133,7 @@ function addPageTestCode(path, opts) {
141
133
  * @param name 函数名
142
134
  * @param body 函数体
143
135
  */
144
- function insertFun(name, body, properties) {
136
+ function insertFn(name, body, properties) {
145
137
  const fun = properties.find(p => p.type === 'ObjectMethod' && p.key.name === name);
146
138
  if (fun) {
147
139
  fun.body.body.push(...body.body);
@@ -152,77 +144,6 @@ function insertFun(name, body, properties) {
152
144
  }, [], body));
153
145
  }
154
146
  }
155
- function hasTestFile(router, opts, e2eConfig) {
156
- const manifest = getManifest(opts);
157
- if (manifest) {
158
- const {
159
- pages
160
- } = manifest.router;
161
- const pageRouter = pages[router];
162
- if (!pageRouter) {
163
- opts.options.onLog({
164
- level: _sharedUtils.Loglevel.WARN,
165
- message: [{
166
- word: router
167
- }, `missing router in manifest.json`]
168
- });
169
- return false;
170
- }
171
- const pagePath = `${opts.compilerOption.sourceRoot}/${router}/${pageRouter.component}`;
172
- const testFilePath = getTestFilePath(pagePath, opts, e2eConfig);
173
- return _fsExtra.default.existsSync(testFilePath);
174
- }
175
- return false;
176
- }
177
- function isIndexPage(opts) {
178
- const manifest = getManifest(opts);
179
- if (manifest) {
180
- const {
181
- entry,
182
- pages
183
- } = manifest.router;
184
- const entryItem = pages[entry];
185
- const entryPath = `${entry}/${entryItem.component}`;
186
- const sourcePath = _path.default.join(opts.options.projectPath, opts.compilerOption.sourceRoot);
187
- const filePath = _path.default.relative(sourcePath, opts.options.filePath);
188
- const filePathParsed = _path.default.parse(filePath);
189
- return entryPath === `${filePathParsed.dir}/${filePathParsed.name}`;
190
- }
191
- return false;
192
- }
193
- function getManifest(opts) {
194
- const manifestPath = _path.default.resolve(opts.options.projectPath, opts.compilerOption.sourceRoot, 'manifest.json');
195
- if (manifestPath) {
196
- return _fsExtra.default.readJSONSync(manifestPath);
197
- }
198
- }
199
- function getE2eConfig(opts) {
200
- const defaultTestDir = 'test';
201
- let result = {
202
- testDir: _path.default.join(opts.options.projectPath, defaultTestDir)
203
- };
204
- const {
205
- projectPath
206
- } = opts.options;
207
- const {
208
- e2eConfigPath
209
- } = opts.compilerOption;
210
- if (e2eConfigPath) {
211
- const configFilePath = _path.default.resolve(projectPath, e2eConfigPath);
212
- const configFileDir = _path.default.dirname(configFilePath);
213
- if (_fsExtra.default.existsSync(configFilePath)) {
214
- result = _fsExtra.default.readJSONSync(configFilePath, 'utf-8');
215
-
216
- // 目录转换为绝对路径
217
- if (result) {
218
- result.testDir = _path.default.resolve(configFileDir, result.testDir || defaultTestDir);
219
- }
220
- } else {
221
- throw new Error(`e2e Configuration file does not exist: ${configFilePath}`);
222
- }
223
- }
224
- return result;
225
- }
226
147
 
227
148
  /**
228
149
  * 获取源文件对应的测试文件地址
@@ -240,14 +161,18 @@ function getTestFilePath(sourceFile, opts, e2eConfig) {
240
161
  } = opts.compilerOption;
241
162
  const relativeSrc = _path.default.relative(_path.default.join(projectPath, sourceRoot || ''), sourceFile);
242
163
  const parsed = _path.default.parse(relativeSrc);
243
- return _path.default.join(e2eConfig.testDir, parsed.dir, `${parsed.name}.js`);
164
+ return _path.default.join(e2eConfig.dir, parsed.dir, `${parsed.name}.js`);
244
165
  }
245
166
 
246
167
  /**
247
168
  * 获取测试函数的函数体
248
169
  * @returns
249
170
  */
250
- function generateTestFn() {
171
+ function generateTestFn(config) {
172
+ const {
173
+ sleep,
174
+ back = true
175
+ } = config || {};
251
176
  const fn = `function(){
252
177
  this.$options = this._options || this.$options;
253
178
  if (this.$options._descriptor) {
@@ -259,46 +184,16 @@ function generateTestFn() {
259
184
  global.qat = new VelaQuickappTest();
260
185
  setTimeout(() => {
261
186
  typeof fnTestCase === 'function' && fnTestCase(this)
262
- qat.run(() => {
187
+ qat.run(async () => {
263
188
  if (this.back !== "false") {
264
189
  console.info("拥有关联测试用例,测试完毕,返回到之前的页面");
265
190
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
266
191
  var _system = _interopRequireDefault($app_require$('@app-module/system.router'));
267
- _system.default.back();
192
+ ${sleep ? `await new Promise(resolve => setTimeout(resolve, ${sleep}));` : ''}
193
+ ${back ? `_system.default.back();` : ''}
268
194
  }
269
195
  });
270
196
  }, global.CASE_TEST_START);
271
197
  }`;
272
198
  return parser.parseExpression(fn).body;
273
- }
274
- function generateAutoRouterFn() {
275
- let sleep = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 3000;
276
- let routerList = arguments.length > 1 ? arguments[1] : undefined;
277
- const fn = `function(){
278
- const routerList = ${JSON.stringify(routerList)}
279
- if (!routerList) {
280
- console.log('missing routerList');
281
- return;
282
- }
283
- if (this.autoTestPageIndex === undefined) {
284
- this.autoTestPageIndex = -1;
285
- }
286
-
287
- setTimeout(() => {
288
- if (++this.autoTestPageIndex < routerList.length) {
289
- const uri = routerList[this.autoTestPageIndex];
290
- console.log(\`auto test page=\${uri}, index=\${this.autoTestPageIndex}\`);
291
- router.push({ uri });
292
- } else {
293
- console.log('auto test finish');
294
- }
295
- }, ${sleep});
296
- }`;
297
- return parser.parseExpression(fn).body;
298
- }
299
- function generateAutoRouterInOnShow() {
300
- const fn = `function(){
301
- this.$generateAutoRunTest();
302
- }`;
303
- return parser.parseExpression(fn).body;
304
199
  }
@@ -29,7 +29,7 @@ const ATTRIBUTE_CONFIG = {
29
29
  return $classValue$;
30
30
  }`;
31
31
  }
32
- return `[${JSON.parse(result).split(' ').map(item => `"${item.trim()}"`).filter(Boolean).join(',')}]`;
32
+ return `[${JSON.parse(result).split(' ').map(item => item.trim()).filter(Boolean).map(item => JSON.stringify(item)).join(',')}]`;
33
33
  }
34
34
  },
35
35
  style: {
@@ -261,10 +261,10 @@ declare class TemplateUtil {
261
261
  * 获取节点中的文字内容
262
262
  *
263
263
  * 1. 如果是文字元素,返回 value
264
- * 1. 循环子元素列表
264
+ * 2. 循环子元素列表
265
265
  * 1. 如果子元素是文本,直接返回内容
266
266
  * 2. 否则,如果子元素允许文本,则递归获取结果
267
- * 2. 文字中连续的特定符号[\r\n\t ]
267
+ * 3. 文字中连续的特定符号[\r\n\t ]
268
268
  * 1. 首尾的替换为空字符串
269
269
  * 2. 其它替换为1个空格
270
270
  *
@@ -273,7 +273,9 @@ declare class TemplateUtil {
273
273
  * a
274
274
  * b
275
275
  *
276
- * ` --> `a b
276
+ * `
277
+ * -->
278
+ * `a b`
277
279
  *
278
280
  * @param node
279
281
  */