Comparing version 0.2.4 to 0.2.5
1145
lazy.js
@@ -1,712 +0,715 @@ | ||
var RGX_TRIM_BEFORE = /^\s+/; | ||
var RGX_TRIM_AFTER = /\s+$/; | ||
;(function () { | ||
var RGX_TRIM_BEFORE = /^\s+/; | ||
var RGX_TRIM_AFTER = /\s+$/; | ||
var _ = { | ||
trim: function (string) { | ||
return string.replace(RGX_TRIM_BEFORE, '').replace(RGX_TRIM_AFTER, ''); | ||
}, | ||
each: function (object, callback) { | ||
for (var key in object) { | ||
if (object.hasOwnProperty(key)) { | ||
callback(object[key], key); | ||
var _ = { | ||
trim: function (string) { | ||
return string.replace(RGX_TRIM_BEFORE, '').replace(RGX_TRIM_AFTER, ''); | ||
}, | ||
each: function (object, callback) { | ||
for (var key in object) { | ||
if (object.hasOwnProperty(key)) { | ||
callback(object[key], key); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
var CONSTS = { | ||
TEST_DEFAULT_TIMEOUT: 8000 | ||
}; | ||
var CONSTS = { | ||
TEST_DEFAULT_TIMEOUT: 8000 | ||
}; | ||
/** | ||
* 设计: http://task.redefime.com/app/list/d916f590be6487d952/adw4_mv7z4 | ||
* | ||
* 注意!!! 本脚本会前后端都调用, 所以请使用 ES5 格式来写 | ||
*/ | ||
var lazy = { | ||
modules: {}, | ||
suiteData: null, | ||
suites: [], | ||
suiteMap: {}, | ||
currentSuiteKey: null, | ||
/** | ||
* 设计: http://task.redefime.com/app/list/d916f590be6487d952/adw4_mv7z4 | ||
* | ||
* 注意!!! 本脚本会前后端都调用, 所以请使用 ES5 格式来写 | ||
*/ | ||
var lazy = { | ||
modules: {}, | ||
suiteData: null, | ||
suites: [], | ||
suiteMap: {}, | ||
currentSuiteKey: null, | ||
lazyTestPage: '/pages/lazy-test.html', | ||
setLazyTestPage: function (url) { | ||
lazy.lazyTestPage = url; | ||
}, | ||
lazyTestPage: '/pages/lazy-test.html', | ||
setLazyTestPage: function (url) { | ||
lazy.lazyTestPage = url; | ||
}, | ||
initSuite: function (suite) { | ||
suite.suiteKey = this.currentSuiteKey; | ||
lazy.suites.push(suite); | ||
lazy.setSuite(suite, this.suiteMap); | ||
initSuite: function (suite) { | ||
suite.suiteKey = this.currentSuiteKey; | ||
lazy.suites.push(suite); | ||
lazy.setSuite(suite, this.suiteMap); | ||
if (lazy.clientPendingTest) { | ||
lazy.clientRunTest(this.clientPendingTest, true); | ||
} | ||
}, | ||
if (lazy.clientPendingTest) { | ||
lazy.clientRunTest(this.clientPendingTest, true); | ||
} | ||
}, | ||
setSuiteKey: function (suiteKey) { | ||
this.currentSuiteKey = suiteKey; | ||
}, | ||
setSuiteKey: function (suiteKey) { | ||
this.currentSuiteKey = suiteKey; | ||
}, | ||
setModules: function (modules) { | ||
for (var moduleKey in modules) { | ||
lazy.modules[moduleKey] = modules[moduleKey]; | ||
} | ||
}, | ||
setModules: function (modules) { | ||
for (var moduleKey in modules) { | ||
lazy.modules[moduleKey] = modules[moduleKey]; | ||
} | ||
}, | ||
data: {}, | ||
setSuiteData: function (data) { | ||
this.data = data; | ||
}, | ||
data: {}, | ||
setSuiteData: function (data) { | ||
this.data = data; | ||
}, | ||
stringify: function (value, depth) { | ||
var peekValue = lazy.processValue(value, depth); | ||
return _.trim(lazy.innerStringify(peekValue)); | ||
}, | ||
stringify: function (value, depth) { | ||
var peekValue = lazy.processValue(value, depth); | ||
return _.trim(lazy.innerStringify(peekValue)); | ||
}, | ||
repeat: function (str, count) { | ||
return new Array(count + 1).join(str); | ||
}, | ||
repeat: function (str, count) { | ||
return new Array(count + 1).join(str); | ||
}, | ||
innerStringify: function (value, indentation) { | ||
indentation = indentation || 0; | ||
innerStringify: function (value, indentation) { | ||
indentation = indentation || 0; | ||
var line = function (content) { | ||
return lazy.repeat(' ', indentation + 1) + content; | ||
}; | ||
var line = function (content) { | ||
return lazy.repeat(' ', indentation + 1) + content; | ||
}; | ||
if (typeof value === 'number') { | ||
if (isNaN(value)) { | ||
return 'NaN\n'; | ||
} | ||
else if (!isFinite(value)) { | ||
if (value > 0) { | ||
return 'Infinity\n'; | ||
if (typeof value === 'number') { | ||
if (isNaN(value)) { | ||
return 'NaN\n'; | ||
} | ||
else { | ||
return '-Infinity\n'; | ||
else if (!isFinite(value)) { | ||
if (value > 0) { | ||
return 'Infinity\n'; | ||
} | ||
else { | ||
return '-Infinity\n'; | ||
} | ||
} | ||
} | ||
} | ||
switch (typeof value) { | ||
case 'object': | ||
if (!value) { | ||
return 'null\n'; | ||
} | ||
switch (typeof value) { | ||
case 'object': | ||
if (!value) { | ||
return 'null\n'; | ||
} | ||
var result; | ||
if (value instanceof Array) { | ||
result = '[]\n'; | ||
} | ||
else { | ||
result = '{}\n'; | ||
} | ||
var result; | ||
if (value instanceof Array) { | ||
result = '[]\n'; | ||
} | ||
else { | ||
result = '{}\n'; | ||
} | ||
var keys = []; | ||
for (var key in value) { | ||
keys.push(key); | ||
} | ||
keys.sort(); | ||
keys.forEach(function (key) { | ||
result += line(key + ' : ' + lazy.innerStringify(value[key], indentation + 1)); | ||
}); | ||
var keys = []; | ||
for (var key in value) { | ||
keys.push(key); | ||
} | ||
keys.sort(); | ||
keys.forEach(function (key) { | ||
result += line(key + ' : ' + lazy.innerStringify(value[key], indentation + 1)); | ||
}); | ||
return result; | ||
default: | ||
return JSON.stringify(value) + '\n'; | ||
} | ||
}, | ||
return result; | ||
default: | ||
return JSON.stringify(value) + '\n'; | ||
} | ||
}, | ||
processValue: function (value, depth) { | ||
if (typeof depth === 'undefined') { | ||
depth = 5; | ||
} | ||
processValue: function (value, depth) { | ||
if (typeof depth === 'undefined') { | ||
depth = 5; | ||
} | ||
switch (typeof value) { | ||
case 'object': | ||
if (!value) { | ||
return value | ||
} | ||
switch (typeof value) { | ||
case 'object': | ||
if (!value) { | ||
return value | ||
} | ||
if (!depth) { | ||
return '_[[[reference: object]]]_'; | ||
} | ||
if (!depth) { | ||
return '_[[[reference: object]]]_'; | ||
} | ||
if (value instanceof Array) { | ||
var result = []; | ||
if (value instanceof Array) { | ||
var result = []; | ||
} | ||
else { | ||
var result = {}; | ||
} | ||
for (var key in value) { | ||
result[key] = lazy.processValue(value[key], depth - 1); | ||
} | ||
return result; | ||
case 'function': | ||
return '_[[[function]]]_'; | ||
default: | ||
return value; | ||
} | ||
}, | ||
lazyAPI: function (path) { | ||
return path ? '/_lazy-test_' + path : '/_lazy-test_'; | ||
}, | ||
parseQuery: function () { | ||
var queryString = location.search; | ||
var query = {}; | ||
queryString.replace(/([^?&=]+)=([^?&=]+)/g, function (_whole, key, value) { | ||
query[decodeURIComponent(key)] = decodeURIComponent(value); | ||
}); | ||
return query; | ||
}, | ||
getQueryString: function (query) { | ||
var queryString = '?'; | ||
var isFirst = true; | ||
for (var key in query) { | ||
if (!isFirst) { | ||
queryString += '&'; | ||
} | ||
else { | ||
var result = {}; | ||
isFirst = false; | ||
} | ||
for (var key in value) { | ||
result[key] = lazy.processValue(value[key], depth - 1); | ||
} | ||
return result; | ||
case 'function': | ||
return '_[[[function]]]_'; | ||
default: | ||
return value; | ||
} | ||
}, | ||
lazyAPI: function (path) { | ||
return path ? '/_lazy-test_' + path : '/_lazy-test_'; | ||
}, | ||
parseQuery: function () { | ||
var queryString = location.search; | ||
var query = {}; | ||
queryString.replace(/([^?&=]+)=([^?&=]+)/g, function (_whole, key, value) { | ||
query[decodeURIComponent(key)] = decodeURIComponent(value); | ||
}); | ||
return query; | ||
}, | ||
getQueryString: function (query) { | ||
var queryString = '?'; | ||
var isFirst = true; | ||
for (var key in query) { | ||
if (!isFirst) { | ||
queryString += '&'; | ||
queryString += encodeURIComponent(key) + '=' + encodeURIComponent(query[key]); | ||
} | ||
else { | ||
isFirst = false; | ||
} | ||
queryString += encodeURIComponent(key) + '=' + encodeURIComponent(query[key]); | ||
} | ||
return queryString; | ||
}, | ||
return queryString; | ||
}, | ||
validateReport: function (report) { | ||
lazy.traverseReport(report, { | ||
preSuite: function (res) { | ||
res.suite.status = 'ok'; | ||
}, | ||
preTest: function (res) { | ||
res.test.status = 'ok'; | ||
}, | ||
result: function (res) { | ||
var result = res.result; | ||
validateReport: function (report) { | ||
lazy.traverseReport(report, { | ||
preSuite: function (res) { | ||
res.suite.status = 'ok'; | ||
}, | ||
preTest: function (res) { | ||
res.test.status = 'ok'; | ||
}, | ||
result: function (res) { | ||
var result = res.result; | ||
if (typeof result.actual === 'undefined') { | ||
result.status = 'not-complete'; | ||
if (typeof result.actual === 'undefined') { | ||
result.status = 'not-complete'; | ||
if (res.test.status !== 'fail') { | ||
res.test.status = 'not-complete'; | ||
if (res.test.status !== 'fail') { | ||
res.test.status = 'not-complete'; | ||
} | ||
} | ||
} | ||
else { | ||
if (_.trim(result.expect) === _.trim(result.actual) | ||
&& result.actual !== 'not-found') { | ||
result.status = 'ok'; | ||
} | ||
else { | ||
result.status = 'fail'; | ||
res.test.status = 'fail'; | ||
if (_.trim(result.expect) === _.trim(result.actual) | ||
&& result.actual !== 'not-found') { | ||
result.status = 'ok'; | ||
} | ||
else { | ||
result.status = 'fail'; | ||
res.test.status = 'fail'; | ||
} | ||
} | ||
} | ||
}, | ||
postTest: function (res) { | ||
var test = res.test; | ||
var suite = res.suite; | ||
}, | ||
postTest: function (res) { | ||
var test = res.test; | ||
var suite = res.suite; | ||
if (test.exception) { | ||
test.status = 'fail'; | ||
} | ||
if (test.exception) { | ||
test.status = 'fail'; | ||
} | ||
if (test.status === 'not-complete' && suite.status !== 'fail') { | ||
suite.status = 'not-complete'; | ||
if (test.status === 'not-complete' && suite.status !== 'fail') { | ||
suite.status = 'not-complete'; | ||
} | ||
else if (test.status === 'fail') { | ||
suite.status = 'fail'; | ||
} | ||
} | ||
else if (test.status === 'fail') { | ||
suite.status = 'fail'; | ||
} | ||
} | ||
}); | ||
}, | ||
}); | ||
}, | ||
setReport: function (report) { | ||
lazy.validateReport(report); | ||
this.report = report; | ||
}, | ||
setReport: function (report) { | ||
lazy.validateReport(report); | ||
this.report = report; | ||
}, | ||
setAllSuites: function (suites) { | ||
this.suites = suites; | ||
setAllSuites: function (suites) { | ||
this.suites = suites; | ||
var suiteMap = {}; | ||
suites.forEach(function (suite) { | ||
lazy.setSuite(suite, suiteMap); | ||
}); | ||
var suiteMap = {}; | ||
suites.forEach(function (suite) { | ||
lazy.setSuite(suite, suiteMap); | ||
}); | ||
this.suiteMap = suiteMap; | ||
}, | ||
this.suiteMap = suiteMap; | ||
}, | ||
getSuite: function (suiteKey) { | ||
var foundSuite; | ||
this.suites.some(function (suite) { | ||
if (suite.suiteKey === suiteKey) { | ||
foundSuite = suite; | ||
return true; | ||
} | ||
}); | ||
return foundSuite; | ||
}, | ||
getSuite: function (suiteKey) { | ||
var foundSuite; | ||
this.suites.some(function (suite) { | ||
if (suite.suiteKey === suiteKey) { | ||
foundSuite = suite; | ||
return true; | ||
} | ||
}); | ||
return foundSuite; | ||
}, | ||
setSuite: function (suite, suiteMap) { | ||
var testMap = {}; | ||
setSuite: function (suite, suiteMap) { | ||
var testMap = {}; | ||
suite.tests.forEach(test => { | ||
testMap[test.testKey] = test; | ||
}); | ||
suite.tests.forEach(test => { | ||
testMap[test.testKey] = test; | ||
}); | ||
suiteMap[suite.suiteKey] = testMap; | ||
}, | ||
suiteMap[suite.suiteKey] = testMap; | ||
}, | ||
traverseReport: function (report, callbacks) { | ||
var context = {}; | ||
traverseReport: function (report, callbacks) { | ||
var context = {}; | ||
_.each(report, function (suiteReport, suiteKey) { | ||
// suite 预处理 | ||
if (callbacks.preSuite) { | ||
callbacks.preSuite({ | ||
suiteKey: suiteKey, | ||
suite: suiteReport | ||
}, context) | ||
} | ||
_.each(suiteReport.tests, function (testReport, testKey) { | ||
// test 预处理 | ||
if (callbacks.preTest) { | ||
callbacks.preTest({ | ||
_.each(report, function (suiteReport, suiteKey) { | ||
// suite 预处理 | ||
if (callbacks.preSuite) { | ||
callbacks.preSuite({ | ||
suiteKey: suiteKey, | ||
testKey: testKey, | ||
suite: suiteReport, | ||
test: testReport | ||
suite: suiteReport | ||
}, context) | ||
} | ||
_.each(testReport.peeks, function (peekReport, peekKey) { | ||
// peek 预处理 | ||
if (callbacks.prePeek) { | ||
callbacks.prePeek({ | ||
_.each(suiteReport.tests, function (testReport, testKey) { | ||
// test 预处理 | ||
if (callbacks.preTest) { | ||
callbacks.preTest({ | ||
suiteKey: suiteKey, | ||
testKey: testKey, | ||
peekKey: peekKey, | ||
suite: suiteReport, | ||
test: testReport, | ||
peek: peekReport | ||
}, context); | ||
test: testReport | ||
}, context) | ||
} | ||
if (callbacks.result) { | ||
// 单一 result 处理 | ||
peekReport.forEach(function (singleResult, index) { | ||
callbacks.result({ | ||
_.each(testReport.peeks, function (peekReport, peekKey) { | ||
// peek 预处理 | ||
if (callbacks.prePeek) { | ||
callbacks.prePeek({ | ||
suiteKey: suiteKey, | ||
testKey: testKey, | ||
peekKey: peekKey, | ||
index: index, | ||
suite: suiteReport, | ||
test: testReport, | ||
peek: peekReport, | ||
result: singleResult | ||
peek: peekReport | ||
}, context); | ||
}); | ||
} | ||
} | ||
if (callbacks.postPeek) { | ||
// peek 后处理 | ||
callbacks.postPeek({ | ||
if (callbacks.result) { | ||
// 单一 result 处理 | ||
peekReport.forEach(function (singleResult, index) { | ||
callbacks.result({ | ||
suiteKey: suiteKey, | ||
testKey: testKey, | ||
peekKey: peekKey, | ||
index: index, | ||
suite: suiteReport, | ||
test: testReport, | ||
peek: peekReport, | ||
result: singleResult | ||
}, context); | ||
}); | ||
} | ||
if (callbacks.postPeek) { | ||
// peek 后处理 | ||
callbacks.postPeek({ | ||
suiteKey: suiteKey, | ||
testKey: testKey, | ||
peekKey: peekKey, | ||
suite: suiteReport, | ||
test: testReport, | ||
peek: peekReport | ||
}, context); | ||
} | ||
}); | ||
if (callbacks.postTest) { | ||
// test 后处理 | ||
callbacks.postTest({ | ||
suiteKey: suiteKey, | ||
testKey: testKey, | ||
peekKey: peekKey, | ||
suite: suiteReport, | ||
test: testReport, | ||
peek: peekReport | ||
}, context); | ||
test: testReport | ||
}, context) | ||
} | ||
}); | ||
if (callbacks.postTest) { | ||
// test 后处理 | ||
callbacks.postTest({ | ||
if (callbacks.postSuite) { | ||
// suite 后处理 | ||
callbacks.postSuite({ | ||
suiteKey: suiteKey, | ||
testKey: testKey, | ||
suite: suiteReport, | ||
test: testReport | ||
suite: suiteReport | ||
}, context) | ||
} | ||
}); | ||
}, | ||
getReportLink: function (reportKey) { | ||
return '/pages/lazy-report.html' + this.getQueryString({ | ||
reportKey: reportKey | ||
}); | ||
}, | ||
if (callbacks.postSuite) { | ||
// suite 后处理 | ||
callbacks.postSuite({ | ||
suiteKey: suiteKey, | ||
suite: suiteReport | ||
}, context) | ||
peek: function (suiteKey, testKey, peekKey, value, depth) { | ||
var top = window.top; | ||
var message = { | ||
type: 'peek', | ||
suiteKey: suiteKey, | ||
testKey: testKey, | ||
peekKey: peekKey, | ||
data: lazy.stringify(value, depth) | ||
}; | ||
top.postMessage(JSON.stringify(message), '*'); | ||
}, | ||
finish: function (suiteKey, testKey, error) { | ||
var top = window.top; | ||
var message = { | ||
type: 'finish', | ||
suiteKey: suiteKey, | ||
testKey: testKey, | ||
error: error | ||
} | ||
}); | ||
}, | ||
getReportLink: function (reportKey) { | ||
return '/pages/lazy-report.html' + this.getQueryString({ | ||
reportKey: reportKey | ||
}); | ||
}, | ||
top.postMessage(JSON.stringify(message), '*'); | ||
}, | ||
peek: function (suiteKey, testKey, peekKey, value, depth) { | ||
var top = window.top; | ||
/** | ||
* ================ TEST 页面 运行的 逻辑 =================== | ||
*/ | ||
var message = { | ||
type: 'peek', | ||
suiteKey: suiteKey, | ||
testKey: testKey, | ||
peekKey: peekKey, | ||
data: lazy.stringify(value, depth) | ||
}; | ||
setupTest: function (startTest) { | ||
top.postMessage(JSON.stringify(message), '*'); | ||
}, | ||
}, | ||
finish: function (suiteKey, testKey, error) { | ||
var top = window.top; | ||
var message = { | ||
type: 'finish', | ||
suiteKey: suiteKey, | ||
testKey: testKey, | ||
error: error | ||
} | ||
clientTestScriptLoadingTimeoutChecker: null, | ||
clientPendingTest: null, | ||
clientRunTest: function (pendingTest, skipPending) { | ||
var suiteKey = pendingTest.suiteKey; | ||
var testKey = pendingTest.testKey | ||
top.postMessage(JSON.stringify(message), '*'); | ||
}, | ||
var suiteMap = this.suiteMap; | ||
/** | ||
* ================ TEST 页面 运行的 逻辑 =================== | ||
*/ | ||
var peek = function (key, value, depth) { | ||
lazy.peek(suiteKey, testKey, key, value, depth); | ||
}; | ||
setupTest: function (startTest) { | ||
var finish = function (error) { | ||
lazy.finish(suiteKey, testKey, error); | ||
lazy.jq('#_lazy_message_').html(suiteKey + '.' + testKey + ' finished!'); | ||
}; | ||
}, | ||
if (suiteMap[suiteKey]) { | ||
var test = suiteMap[suiteKey][testKey]; | ||
clearTimeout(lazy.clientTestScriptLoadingTimeoutChecker); | ||
if (!test) { | ||
finish('没有找到对应的测试 ' + suiteKey + '.' + testKey); | ||
} | ||
else { | ||
// 检查是否依赖的 modules 都加载成功 | ||
clientTestScriptLoadingTimeoutChecker: null, | ||
clientPendingTest: null, | ||
clientRunTest: function (pendingTest, skipPending) { | ||
var suiteKey = pendingTest.suiteKey; | ||
var testKey = pendingTest.testKey | ||
var modules = lazy.getSuite(suiteKey).modules; | ||
var modulesNotFound = []; | ||
modules.forEach(function (moduleKey) { | ||
if (!lazy.modules.hasOwnProperty(moduleKey)) { | ||
modulesNotFound.push(moduleKey); | ||
} | ||
}); | ||
if (modulesNotFound.length) { | ||
finish('测试依赖缺失, 请确认已注入! 缺失模块为 : ' + modulesNotFound.join(', ')); | ||
return; | ||
} | ||
var suiteMap = this.suiteMap; | ||
// 处理后续的脚本错误, 记为 test 的 exception | ||
window.addEventListener('error', function (e) { | ||
var errorMessage = '测试运行发现错误 : \n'; | ||
var peek = function (key, value, depth) { | ||
lazy.peek(suiteKey, testKey, key, value, depth); | ||
}; | ||
if (e.error && e.error.stack) { | ||
errorMessage += e.error.stack; | ||
} | ||
else { | ||
errorMessage += e.message | ||
} | ||
var finish = function (error) { | ||
lazy.finish(suiteKey, testKey, error); | ||
lazy.jq('#_lazy_message_').html(suiteKey + '.' + testKey + ' finished!'); | ||
}; | ||
finish(errorMessage); | ||
}); | ||
if (suiteMap[suiteKey]) { | ||
var test = suiteMap[suiteKey][testKey]; | ||
clearTimeout(lazy.clientTestScriptLoadingTimeoutChecker); | ||
if (!test) { | ||
finish('没有找到对应的测试 ' + suiteKey + '.' + testKey); | ||
test.script(lazy.data, lazy.modules, peek, finish); | ||
} | ||
} | ||
else { | ||
// 检查是否依赖的 modules 都加载成功 | ||
var modules = lazy.getSuite(suiteKey).modules; | ||
var modulesNotFound = []; | ||
modules.forEach(function (moduleKey) { | ||
if (!lazy.modules.hasOwnProperty(moduleKey)) { | ||
modulesNotFound.push(moduleKey); | ||
} | ||
}); | ||
if (modulesNotFound.length) { | ||
finish('测试依赖缺失, 请确认已注入! 缺失模块为 : ' + modulesNotFound.join(', ')); | ||
else if (!skipPending) { | ||
if (!lazy.origin) { | ||
finish('请在 test 页面中设置 origin (注: 调用 lazy.setupTest(function(suiteKey, testKey, runTest) { /* YOUR TEST RUNNER HERE */ }); ), ' + | ||
'否则 origin 出错将导致无法正常测试'); | ||
return; | ||
} | ||
// 处理后续的脚本错误, 记为 test 的 exception | ||
window.addEventListener('error', function (e) { | ||
var errorMessage = '测试运行发现错误 : \n'; | ||
lazy.jq | ||
.getScript(lazy.origin + lazy.lazyAPI('/get-test-script?suiteKey=' + suiteKey)) | ||
.fail(function (res, errorType, error) { | ||
// 处理脚本首次解析运行的脚本错误, 记为 test 的 exception | ||
if (e.error && e.error.stack) { | ||
errorMessage += e.error.stack; | ||
} | ||
else { | ||
errorMessage += e.message | ||
} | ||
var errorMessage = '测试解析运行发现错误 : \n'; | ||
errorMessage += errorType + '\n'; | ||
errorMessage += error; | ||
finish(errorMessage); | ||
}); | ||
finish(errorMessage); | ||
}); | ||
test.script(lazy.data, lazy.modules, peek, finish); | ||
lazy.clientTestScriptLoadingTimeoutChecker = setTimeout(function () { | ||
finish('测试 ' + suiteKey + '.' + testKey + ' 测试脚本加载超时!'); | ||
}, 4000); | ||
lazy.clientPendingTest = pendingTest; | ||
} | ||
} | ||
else if (!skipPending) { | ||
if (!lazy.origin) { | ||
finish('请在 test 页面中设置 origin (注: 调用 lazy.setupTest(function(suiteKey, testKey, runTest) { /* YOUR TEST RUNNER HERE */ }); ), ' + | ||
'否则 origin 出错将导致无法正常测试'); | ||
return; | ||
} | ||
}, | ||
lazy.jq | ||
.getScript(lazy.origin + lazy.lazyAPI('/get-test-script?suiteKey=' + suiteKey)) | ||
.fail(function (res, errorType, error) { | ||
// 处理脚本首次解析运行的脚本错误, 记为 test 的 exception | ||
/** | ||
* ================ INDEX 页面 运行的 逻辑 =================== | ||
*/ | ||
reportKey: '', | ||
setReportKey: function (reportKey) { | ||
this.reportKey = reportKey; | ||
}, | ||
var errorMessage = '测试解析运行发现错误 : \n'; | ||
errorMessage += errorType + '\n'; | ||
errorMessage += error; | ||
hasPostMessageSetup: false, | ||
finish(errorMessage); | ||
}); | ||
origin: '', | ||
originCallback: null, | ||
setOrigin: function (origin) { | ||
lazy.origin = origin; | ||
lazy.clientTestScriptLoadingTimeoutChecker = setTimeout(function () { | ||
finish('测试 ' + suiteKey + '.' + testKey + ' 测试脚本加载超时!'); | ||
}, 4000); | ||
var query = lazy.parseQuery(); | ||
var suiteKey = query.suiteKey; | ||
var testKey = query.testKey; | ||
lazy.clientPendingTest = pendingTest; | ||
} | ||
}, | ||
var runTest = function () { | ||
lazy.clientRunTest({ | ||
suiteKey: suiteKey, | ||
testKey: testKey | ||
}); | ||
}; | ||
/** | ||
* ================ INDEX 页面 运行的 逻辑 =================== | ||
*/ | ||
reportKey: '', | ||
setReportKey: function (reportKey) { | ||
this.reportKey = reportKey; | ||
}, | ||
lazy.jq(document.body) | ||
.prepend(`<div id="_lazy_message_">Running test ${query.suiteKey}.${query.testKey}</div>`); | ||
lazy.jq.get(lazy.origin + lazy.lazyAPI('/get-test-data'), { | ||
suiteKey: suiteKey | ||
}).success(function (res) { | ||
lazy.setSuiteData(res.data); | ||
hasPostMessageSetup: false, | ||
lazy.originCallback(suiteKey, testKey, runTest); | ||
}); | ||
}, | ||
getOrigin: function () { | ||
var l = location; | ||
return l.protocol + '//' + l.host; | ||
}, | ||
setupTest: function (callback) { | ||
lazy.setupPostMessage(); | ||
window.lazy = lazy; | ||
origin: '', | ||
originCallback: null, | ||
setOrigin: function (origin) { | ||
lazy.origin = origin; | ||
lazy.originCallback = callback; | ||
window.top.postMessage(JSON.stringify({ | ||
type: 'try-setup-test' | ||
}), '*'); | ||
}, | ||
var query = lazy.parseQuery(); | ||
var suiteKey = query.suiteKey; | ||
var testKey = query.testKey; | ||
jqueryScript: '', | ||
setJqueryScript: function (script) { | ||
lazy.jqueryScript = script; | ||
}, | ||
getJqueryScript: function () { | ||
return lazy.jqueryScript; | ||
}, | ||
var runTest = function () { | ||
lazy.clientRunTest({ | ||
suiteKey: suiteKey, | ||
testKey: testKey | ||
}); | ||
}; | ||
/** | ||
* | ||
* @param {Object} opts | ||
* refreshTests: () => undefined | ||
*/ | ||
setupPostMessage: function (opts) { | ||
opts = opts || {}; | ||
lazy.jq(document.body) | ||
.prepend(`<div id="_lazy_message_">Running test ${query.suiteKey}.${query.testKey}</div>`); | ||
lazy.jq.get(lazy.origin + lazy.lazyAPI('/get-test-data'), { | ||
suiteKey: suiteKey | ||
}).success(function (res) { | ||
lazy.setSuiteData(res.data); | ||
if (!lazy.hasPostMessageSetup) { | ||
window.addEventListener('message', function (event) { | ||
var message = JSON.parse(event.data); | ||
var pendingTest = lazy.parentPendingTest; | ||
lazy.originCallback(suiteKey, testKey, runTest); | ||
}); | ||
}, | ||
getOrigin: function () { | ||
var l = location; | ||
return l.protocol + '//' + l.host; | ||
}, | ||
setupTest: function (callback) { | ||
lazy.setupPostMessage(); | ||
window.lazy = lazy; | ||
if (message.type === 'setup-test') { | ||
if (window.jQuery) { | ||
var _jQuery = jQuery.noConflict(); | ||
} | ||
lazy.originCallback = callback; | ||
window.top.postMessage(JSON.stringify({ | ||
type: 'try-setup-test' | ||
}), '*'); | ||
}, | ||
eval(message.jquery); | ||
jqueryScript: '', | ||
setJqueryScript: function (script) { | ||
lazy.jqueryScript = script; | ||
}, | ||
getJqueryScript: function () { | ||
return lazy.jqueryScript; | ||
}, | ||
lazy.injectJQuery(window.jQuery); | ||
if (_jQuery) { | ||
window.jQuery = window.$ = _jQuery; | ||
} else { | ||
delete window.jQuery; | ||
delete window.$; | ||
} | ||
/** | ||
* | ||
* @param {Object} opts | ||
* refreshTests: () => undefined | ||
*/ | ||
setupPostMessage: function (opts) { | ||
opts = opts || {}; | ||
lazy.setOrigin(message.origin); | ||
} else if (message.type === 'try-setup-test') { | ||
event.source.postMessage(JSON.stringify({ | ||
type: 'setup-test', | ||
origin: lazy.getOrigin(), | ||
jquery: lazy.getJqueryScript() | ||
}), '*'); | ||
} else { | ||
if (message.suiteKey === pendingTest.suiteKey | ||
&& message.testKey === pendingTest.testKey) { | ||
if (message.type === 'finish') { | ||
if (message.error) { | ||
pendingTest.report.exception = message.error; | ||
} | ||
if (!lazy.hasPostMessageSetup) { | ||
window.addEventListener('message', function (event) { | ||
var message = JSON.parse(event.data); | ||
var pendingTest = lazy.parentPendingTest; | ||
lazy.parentFinishTest(opts.refreshTests); | ||
} | ||
else if (message.type === 'peek') { | ||
var peekReport = pendingTest.report.peeks[message.peekKey] | ||
= pendingTest.report.peeks[message.peekKey] || []; | ||
if (message.type === 'setup-test') { | ||
if (window.jQuery) { | ||
var _jQuery = jQuery.noConflict(); | ||
peekReport.push(message.data); | ||
} | ||
} | ||
} | ||
eval(message.jquery); | ||
// 用于测试用, 看 post message | ||
// console.log(message); | ||
}); | ||
lazy.injectJQuery(window.jQuery); | ||
if (_jQuery) { | ||
window.jQuery = window.$ = _jQuery; | ||
} else { | ||
delete window.jQuery; | ||
delete window.$; | ||
} | ||
lazy.hasPostMessageSetup = true; | ||
} | ||
}, | ||
lazy.setOrigin(message.origin); | ||
} else if (message.type === 'try-setup-test') { | ||
event.source.postMessage(JSON.stringify({ | ||
type: 'setup-test', | ||
origin: lazy.getOrigin(), | ||
jquery: lazy.getJqueryScript() | ||
}), '*'); | ||
} else { | ||
if (message.suiteKey === pendingTest.suiteKey | ||
&& message.testKey === pendingTest.testKey) { | ||
if (message.type === 'finish') { | ||
if (message.error) { | ||
pendingTest.report.exception = message.error; | ||
} | ||
jq: null, | ||
injectJQuery: function (jQuery) { | ||
lazy.jq = jQuery; | ||
}, | ||
lazy.parentFinishTest(opts.refreshTests); | ||
} | ||
else if (message.type === 'peek') { | ||
var peekReport = pendingTest.report.peeks[message.peekKey] | ||
= pendingTest.report.peeks[message.peekKey] || []; | ||
clientTestRunningTimeoutChecker: null, | ||
parentPendingTest: null, | ||
peekReport.push(message.data); | ||
} | ||
} | ||
} | ||
/** | ||
* | ||
* @param {string} suiteKey suite 的 key | ||
* @param {string} testKey test 的 key | ||
* @param {$iframe} $testFrame test 页面的 iframe jquery 对象 | ||
* @param {Object} opts | ||
* refreshTests: () => undefined | ||
*/ | ||
parentRunTest: function (suiteKey, testKey, $testFrame, opts) { | ||
lazy.setupPostMessage(opts); | ||
// 用于测试用, 看 post message | ||
// console.log(message); | ||
}); | ||
clearTimeout(lazy.clientTestRunningTimeoutChecker); | ||
$testFrame.attr('src', ''); | ||
lazy.hasPostMessageSetup = true; | ||
} | ||
}, | ||
lazy.parentPendingTest = { | ||
suiteKey: suiteKey, | ||
testKey: testKey, | ||
report: { | ||
peeks: {} | ||
} | ||
}; | ||
jq: null, | ||
injectJQuery: function (jQuery) { | ||
lazy.jq = jQuery; | ||
}, | ||
var test = lazy.suiteMap[suiteKey][testKey]; | ||
lazy.clientTestRunningTimeoutChecker = setTimeout(function () { | ||
// 测试运行 timeout | ||
lazy.finish(suiteKey, testKey, '测试脚本运行超时!'); | ||
}, test.timeout || CONSTS.TEST_DEFAULT_TIMEOUT); | ||
clientTestRunningTimeoutChecker: null, | ||
parentPendingTest: null, | ||
$testFrame.attr('src', lazy.lazyTestPage + lazy.getQueryString({ | ||
suiteKey: suiteKey, | ||
testKey: testKey | ||
})); | ||
}, | ||
parentFinishTest: function (refreshTests) { | ||
var pendingTest = lazy.parentPendingTest; | ||
/** | ||
* | ||
* @param {string} suiteKey suite 的 key | ||
* @param {string} testKey test 的 key | ||
* @param {$iframe} $testFrame test 页面的 iframe jquery 对象 | ||
* @param {Object} opts | ||
* refreshTests: () => undefined | ||
*/ | ||
parentRunTest: function (suiteKey, testKey, $testFrame, opts) { | ||
lazy.setupPostMessage(opts); | ||
clearTimeout(lazy.clientTestRunningTimeoutChecker); | ||
lazy.jq | ||
.ajax({ | ||
url: lazy.lazyAPI('/write-test-report'), | ||
data: JSON.stringify({ | ||
reportKey: lazy.reportKey, | ||
suiteKey: pendingTest.suiteKey, | ||
testKey: pendingTest.testKey, | ||
report: pendingTest.report | ||
}), | ||
type: 'post', | ||
contentType: 'application/json' | ||
}) | ||
.success(function (res) { | ||
refreshTests(res.report); | ||
lazy.parentRunNextTest(); | ||
}); | ||
}, | ||
parentRunNextTest: function () { | ||
var nextTest = lazy.testQueue.shift(); | ||
if (nextTest) { | ||
lazy.parentRunTest( | ||
nextTest.suiteKey, | ||
nextTest.testKey, | ||
nextTest.$testFrame, | ||
nextTest.opts | ||
); | ||
} | ||
}, | ||
clearTimeout(lazy.clientTestRunningTimeoutChecker); | ||
$testFrame.attr('src', ''); | ||
cleanTestQueue: function () { | ||
lazy.testQueue = []; | ||
}, | ||
testQueue: [], | ||
parentRunSuite: function (suiteKey, $testFrame, opts) { | ||
var testMap = this.suiteMap[suiteKey]; | ||
lazy.parentPendingTest = { | ||
suiteKey: suiteKey, | ||
testKey: testKey, | ||
report: { | ||
peeks: {} | ||
lazy.cleanTestQueue(); | ||
for (var testKey in testMap) { | ||
lazy.testQueue.push({ | ||
suiteKey: suiteKey, | ||
testKey: testKey, | ||
$testFrame: $testFrame, | ||
opts: opts | ||
}) | ||
} | ||
}; | ||
lazy.parentRunNextTest(); | ||
}, | ||
var test = lazy.suiteMap[suiteKey][testKey]; | ||
lazy.clientTestRunningTimeoutChecker = setTimeout(function () { | ||
// 测试运行 timeout | ||
lazy.finish(suiteKey, testKey, '测试脚本运行超时!'); | ||
}, test.timeout || CONSTS.TEST_DEFAULT_TIMEOUT); | ||
parentRunRestTests: function (restTests, $testFrame, opts) { | ||
$testFrame.attr('src', lazy.lazyTestPage + lazy.getQueryString({ | ||
suiteKey: suiteKey, | ||
testKey: testKey | ||
})); | ||
}, | ||
parentFinishTest: function (refreshTests) { | ||
var pendingTest = lazy.parentPendingTest; | ||
clearTimeout(lazy.clientTestRunningTimeoutChecker); | ||
lazy.jq | ||
.ajax({ | ||
url: lazy.lazyAPI('/write-test-report'), | ||
data: JSON.stringify({ | ||
reportKey: lazy.reportKey, | ||
suiteKey: pendingTest.suiteKey, | ||
testKey: pendingTest.testKey, | ||
report: pendingTest.report | ||
}), | ||
type: 'post', | ||
contentType: 'application/json' | ||
}) | ||
.success(function (res) { | ||
refreshTests(res.report); | ||
lazy.parentRunNextTest(); | ||
lazy.cleanTestQueue(); | ||
restTests.forEach(function (test) { | ||
lazy.testQueue.push({ | ||
suiteKey: test.suiteKey, | ||
testKey: test.testKey, | ||
$testFrame: $testFrame, | ||
opts: opts | ||
}); | ||
}); | ||
}, | ||
parentRunNextTest: function () { | ||
var nextTest = lazy.testQueue.shift(); | ||
if (nextTest) { | ||
lazy.parentRunTest( | ||
nextTest.suiteKey, | ||
nextTest.testKey, | ||
nextTest.$testFrame, | ||
nextTest.opts | ||
); | ||
lazy.parentRunNextTest(); | ||
} | ||
}, | ||
}; | ||
cleanTestQueue: function () { | ||
lazy.testQueue = []; | ||
}, | ||
testQueue: [], | ||
parentRunSuite: function (suiteKey, $testFrame, opts) { | ||
var testMap = this.suiteMap[suiteKey]; | ||
lazy.cleanTestQueue(); | ||
for (var testKey in testMap) { | ||
lazy.testQueue.push({ | ||
suiteKey: suiteKey, | ||
testKey: testKey, | ||
$testFrame: $testFrame, | ||
opts: opts | ||
}) | ||
if (typeof module !== 'undefined') { | ||
module.exports = lazy; | ||
} else { | ||
if (typeof window !== 'undefined') { | ||
window.lazy = lazy; | ||
} | ||
lazy.parentRunNextTest(); | ||
}, | ||
parentRunRestTests: function (restTests, $testFrame, opts) { | ||
lazy.cleanTestQueue(); | ||
restTests.forEach(function (test) { | ||
lazy.testQueue.push({ | ||
suiteKey: test.suiteKey, | ||
testKey: test.testKey, | ||
$testFrame: $testFrame, | ||
opts: opts | ||
}); | ||
}); | ||
lazy.parentRunNextTest(); | ||
} | ||
}; | ||
if (typeof module !== 'undefined') { | ||
module.exports = lazy; | ||
} else { | ||
if (typeof window !== 'undefined') { | ||
window.lazy = lazy; | ||
} | ||
} | ||
})(); |
{ | ||
"name": "lazytest", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "A project from matriks2 seed", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3372530
20844