Tamejs can't work with mocha?
-
08-06-2021 - |
Question
See my test code using mocha + tamejs:
test/t.tjs
require('should');
function inc(n, callback) {
setTimeout(function() {
console.log('### inc: ' + n);
callback(n+1);
}, 1000);
};
describe('test', function(){
it('show ok with tamejs', function(){
console.log('### testing ...');
var result;
await { inc(1, defer(result)); }
console.log('result: ' + result);
result.should.equal(123456); // won't pass
});
});
Compile it to t.js:
tamejs -o test/t.js test/t.tjs
Run mocha
mocha
Result:
### testing ...
.
✔ 1 test complete (1ms)
It seems the inc
method has never been invoked.
Solution
The problem is I didn't use mocha correctly.
It should be wrote as:
describe('test', function(){
it('show ok with tamejs', function(done){
console.log('### testing ...');
var result;
await { inc(1, defer(result)); }
console.log('result: ' + result);
result.should.equal(123456); // won't pass
done();
});
});
Please notice the done
, it used in mocha to determine if a asynchronous invocation has finished or not. Without it, mocha won't wait for the asynchronous invocation.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow