Skip to content

Instantly share code, notes, and snippets.

@GitaiQAQ

GitaiQAQ/result.js

Last active Apr 26, 2019
Embed
What would you like to do?
使用 Promise 实现的单元测试框架??函数
// 只是因为 js 的高亮好看
$ node src/test.js
Tiny UnitTest v0.1
# 1. A passing test
[ok] This test will pass.
# 2. Assertions with tape.
[err] Error: Given two mismatched values, .equal() should produce a nice bug report
at Object.end (C:\Users\Administrator\Desktop\transer\src\test.js:31:67)
at test (C:\Users\Administrator\Desktop\transer\src\test.js:62:12)
at UnitTest.Promise (C:\Users\Administrator\Desktop\transer\src\test.js:34:46)
at new Promise (<anonymous>)
at new UnitTest (C:\Users\Administrator\Desktop\transer\src\test.js:23:17)
at C:\Users\Administrator\Desktop\transer\src\test.js:47:61
at test (C:\Users\Administrator\Desktop\transer\src\test.js:47:66)
at Object.<anonymous> (C:\Users\Administrator\Desktop\transer\src\test.js:55:1)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
# 3. Throw
[err] Error: boom
at test (C:\Users\Administrator\Desktop\transer\src\test.js:66:11)
at UnitTest.Promise (C:\Users\Administrator\Desktop\transer\src\test.js:34:46)
at new Promise (<anonymous>)
at new UnitTest (C:\Users\Administrator\Desktop\transer\src\test.js:23:17)
at C:\Users\Administrator\Desktop\transer\src\test.js:47:61
at test (C:\Users\Administrator\Desktop\transer\src\test.js:47:66)
at Object.<anonymous> (C:\Users\Administrator\Desktop\transer\src\test.js:65:1)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
# 4. Promise
[ok] do some async ops here.
# pass 2/4
# fail 2/4
let test = function UnitTest (label, fn, taskId=1) {
let _pt = UnitTest.prototype;
'passed' in _pt || (_pt.passed = 0) || console.log('Tiny UnitTest v0.1');
// 在结束时打印汇总
_pt.exit = () => {
console.log(`# pass ${_pt.passed}/${taskId}`);
console.log(`# fail ${taskId - _pt.passed}/${taskId}`);
}
process.on('exit', () => _pt.exit && (_pt.exit() || (_pt.exit = null)));
// 处理逻辑
let _handler = (err, msg) => {
_pt.passed += !err;
let title = `# ${('' + taskId).padStart(2)}. ${label}`;
console.group(title);
console[err ? 'error' : 'log'](err ? `[err]` : `[ok] ${msg}`, err || '', '\n');
console.groupEnd(title);
}
let _this = new Promise((resolve, reject) => {
let assert = {
true: (_ok, _msg) => Object.assign(assert, { _ok, _msg }),
pass: (_msg) => assert.true(true, _msg),
false: (_err, _msg) => assert.true(!_err, _msg),
equal: (a, b, _msg) => assert.true(a === b, _msg),
notEq: (a, b, _msg) => assert.true(a !== b, _msg),
end: () => assert._ok ? resolve(assert._msg) : reject(new Error(assert._msg))
}
return Promise.race([Promise.resolve(fn(assert)).finally(() => {
reject(new Error('test exited without ending'))
}), new Promise(function (resolve) {
setTimeout(resolve, 200);
}).then(() => {
reject(new Error('UnitTest timed out after ' + 200 + ' ms'));
})]);
}).then(
(msg) => _handler(null, msg),
(err) => _handler(err, null)
);
// 递增任务 ID
test = (...args) => (() => new UnitTest(...args, taskId + 1))();
}
test('A passing test', (assert) => {
assert.pass('This test will pass.');
assert.end();
});
test('Assertions with tape.', (assert) => {
const expected = 'something to test';
const actual = 'sonething to test';
assert.equal(actual, expected,
'Given two mismatched values, .equal() should produce a nice bug report');
assert.end();
});
test('Throw', (assert) => {
throw new Error('boom')
});
test('Promise', (assert) => {
return new Promise((resolve, reject) => {
assert.pass('do some async ops here.');
}).then(() => {
assert.end();
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment