MCPcopy
hub / github.com/layui/layui / #it

Method #it

examples/test/test.js:91–160  ·  view source on GitHub ↗
(opts = {})

Source from the content-addressed store, hash-verified

89 }
90
91 #it(opts = {}) {
92 let { code, expected, title, assert } = opts;
93 const describer = this.describer;
94 const itemsElem = lay(describer.itemsElem);
95 const stats = Test.stats;
96
97 const startTime = Date.now();
98 const actual = (function () {
99 try {
100 return new Function(`return function() {
101 let result;
102 const console = Object.assign({}, window.console);
103 console.log = (a) => result = a;
104 ${code}
105 return result;
106 }`)()();
107 } catch (e) {
108 return e.message;
109 }
110 })();
111
112 const duration = (Date.now() - startTime) / 1000;
113 const passed = (
114 assert ? assert.bind(arguments[0]) : Test.tools.equal.bind(this)
115 )(actual, expected);
116 const result = passed ? '✅' : '❌';
117 const output = Test.tools.output;
118 const itemElem = lay.elem('div', { class: 'test-item' });
119
120 stats.total++;
121 title = title || `任务 ${stats.total}`;
122
123 if (passed) {
124 stats.passes++;
125 } else {
126 stats.failures++;
127 console.error(
128 `${describer.title} → ${title} : Test failed\nExpected: ${output(expected)}\nActual: ${output(actual)}`
129 );
130 }
131
132 itemElem.innerHTML = `
133 <div><strong>${title}:</strong></div>
134 <pre class="layui-code">${code}</pre>
135 <div class="test-result">
136 <ul>
137 <li><strong>预期:</strong><code>${output(expected)}</code></li>
138 ${
139 !passed || opts.showActual
140 ? '<li><strong>实际:</strong><code>' +
141 output(actual) +
142 '</code></li>'
143 : ''
144 }
145 <li>
146 <span><strong>结果:</strong>${output(result)}</span>
147 <!-- <span><strong>耗时:</strong>${duration.toFixed(2)} s</span> -->
148 </li>

Callers

nothing calls this directly

Calls 2

layFunction · 0.85
outputFunction · 0.85

Tested by

no test coverage detected