Skip to content

Commit 3008569

Browse files
author
fanniehuang
committed
fix(e2e): 解决e2e录制时,wxa的fetch接口没有劫持到wx.request
re #80
1 parent 7c579c2 commit 3008569

File tree

5 files changed

+90
-37
lines changed

5 files changed

+90
-37
lines changed

packages/wxa-cli/scripts/buildLib.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ rm -f ./dist/tester/wxa-e2eTest/e2eTestCaseTpl.ejs
44
rm -rf ./lib-dist/wxa-e2eTest
55
mkdir -p ./lib-dist/wxa-e2eTest
66
mkdir -p ./dist/tester/wxa-e2eTest
7+
ln ./src/tester/wxa-e2eTest/state.js ./lib-dist/wxa-e2eTest/state.js
78
ln ./src/tester/wxa-e2eTest/e2eTestSuite.js ./lib-dist/wxa-e2eTest/e2eTestSuite.js
8-
ln ./src/tester/wxa-e2eTest/mockWxMethodConfig.js ./lib-dist/wxa-e2eTest/mockWxMethodConfig.js
9+
ln ./src/tester/wxa-e2eTest/e2eMockWxMethod.js ./lib-dist/wxa-e2eTest/e2eMockWxMethod.js
910
ln ./src/tester/wxa-e2eTest/e2eRecordBtn.wxa ./lib-dist/wxa-e2eTest/e2eRecordBtn.wxa
1011
ln ./src/tester/wxa-e2eTest/e2eTestCaseTpl.ejs ./dist/tester/wxa-e2eTest/e2eTestCaseTpl.ejs
1112
echo $MODE;

packages/wxa-cli/src/tester/e2eTester.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,11 @@ class TesterScheduler extends Schedule {
112112
(/exports\.default/gm.test(mdl.code) || /exports\[["']default["']/gm.test(mdl.code))
113113
) {
114114
mdl.code = `
115+
var __e2e__state = require('wxa://wxa-e2eTest/state.js')
115116
var __testSuitePlugin = require('wxa://wxa-e2eTest/e2eTestSuite.js');
116-
require('@wxa/core').wxa.use(__testSuitePlugin, {record: ${!!this.cmdOptions.record}});
117+
var __e2eMockWxMethod = require('wxa://wxa-e2eTest/e2eMockWxMethod.js');
118+
__e2eMockWxMethod({state: __e2e__state});
119+
require('@wxa/core').wxa.use(__testSuitePlugin, {record: ${!!this.cmdOptions.record}, state: __e2e__state});
117120
${mdl.code}
118121
`;
119122
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
let state = require('./state.js');
2+
3+
function getKey(configKey) {
4+
return `${this.name}__e2e__${configKey.join('__e2e__')}`
5+
}
6+
let mockWxMethodConfig = [
7+
{
8+
name: 'showModal',
9+
recordStringify(config) {
10+
return getKey.call(this, [
11+
config.title, config.content, config.editable, config.showCancel, config.confirmText
12+
])
13+
},
14+
tpl: (() => {
15+
let key = ['config.title || ""', 'config.content || ""', 'config.editable || ""', 'config.showCancel || ""', 'config.confirmText || ""']
16+
return `showModal__e2e__\${${key.join('}__e2e__${')}}`
17+
})()
18+
},
19+
{
20+
name: 'showActionSheet',
21+
recordStringify(config) {
22+
return getKey.call(this, [
23+
config.alertText, config.itemColor, config.itemList.join(',')
24+
])
25+
},
26+
tpl: (() => {
27+
let key = ['config.alertText || ""', 'config.itemColor || ""', 'config.itemList.join(",") || ""']
28+
return `showActionSheet__e2e__\${${key.join('}__e2e__${')}}`
29+
})()
30+
},
31+
{
32+
name: 'request',
33+
recordStringify(config) {
34+
console.log(getKey.call(this, [
35+
config.url, config.method, Object.keys(config.data || {}).join(',')
36+
]));
37+
return getKey.call(this, [
38+
config.url.split('?')[0], config.method, Object.keys(config.data || {}).join(',')
39+
])
40+
},
41+
tpl: (() => {
42+
let key = ['(config.url && config.url.split("?")[0]) || ""', 'config.method || ""', 'Object.keys(config.data).join(",") || ""']
43+
return `request__e2e__\${${key.join('}__e2e__${')}}`
44+
})()
45+
}
46+
]
47+
module.exports = function({state}) {
48+
mockWxMethodConfig.forEach(item => {
49+
let originMethod = wx[item.name];
50+
Object.defineProperty(wx, item.name, {
51+
configurable: true,
52+
enumerable: true,
53+
writable: true,
54+
value: function() {
55+
const config = arguments[0] || {};
56+
let { recording, apiRecord } = state;
57+
if (recording) {
58+
let key = item.recordStringify(config)
59+
if (!apiRecord.has(key)) {
60+
apiRecord.set(key, [])
61+
}
62+
let originSuccess = config.success;
63+
config.success = function() {
64+
const res = arguments[0] || {};
65+
apiRecord.get(key).push(JSON.parse(JSON.stringify(res)))
66+
67+
originSuccess.apply(this, arguments);
68+
}
69+
return originMethod.apply(this, arguments);
70+
71+
}
72+
return originMethod.apply(this, arguments);
73+
}
74+
})
75+
})
76+
}

packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestSuite.js

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
/* eslint-disable no-invalid-this */
22
/* eslint-disable no-undef */
3-
import mockWxMethodConfig from './mockWxMethodConfig';
43

54
const IDKEY = '_wxatestuniqueid';
65
const EVENTMAPKEY = '_wxatesteventmap';
76
let lastEventTime = {};
8-
let state = {
9-
record: [],
10-
recording: false,
11-
apiRecord: new Map(),
12-
recordMode: false
13-
};
7+
let state;
148

159
// 获取eventMap中对应事件方法
1610
function getEventFunc(eventType, eventMap) {
@@ -141,6 +135,7 @@ const mountStateAndWrapEvent = (vm) => {
141135
};
142136

143137
let $$testSuitePlugin = (options) => {
138+
state = options.state;
144139
// vm是当前实例,type为实例类型
145140
return (vm, type)=>{
146141
// 劫持wx.request,做apimock
@@ -150,34 +145,6 @@ let $$testSuitePlugin = (options) => {
150145
state.recording = true;
151146
state.recordMode = true;
152147
}
153-
mockWxMethodConfig.forEach(item => {
154-
let originMethod = wx[item.name];
155-
Object.defineProperty(wx, item.name, {
156-
configurable: true,
157-
enumerable: true,
158-
writable: true,
159-
value: function() {
160-
const config = arguments[0] || {};
161-
let { recording, apiRecord } = state;
162-
if (recording) {
163-
let key = item.recordStringify(config)
164-
if (!apiRecord.has(key)) {
165-
apiRecord.set(key, [])
166-
}
167-
let originSuccess = config.success;
168-
config.success = function() {
169-
const res = arguments[0] || {};
170-
apiRecord.get(key).push(JSON.parse(JSON.stringify(res)))
171-
172-
originSuccess.apply(this, arguments);
173-
}
174-
return originMethod.apply(this, arguments);
175-
176-
}
177-
return originMethod.apply(this, arguments);
178-
}
179-
})
180-
})
181148
}
182149
if (['App', 'Page'].indexOf(type) > -1) {
183150
mountStateAndWrapEvent(vm);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
record: [],
3+
recording: false,
4+
apiRecord: new Map(),
5+
recordMode: false
6+
}

0 commit comments

Comments
 (0)