Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support question - Errors thrown in code called asynchronously #363

Closed
ORESoftware opened this issue Feb 19, 2016 · 2 comments
Closed

Support question - Errors thrown in code called asynchronously #363

ORESoftware opened this issue Feb 19, 2016 · 2 comments

Comments

@ORESoftware
Copy link

I am just starting to use this library, and trying to understand how vows knows if my codeblock is asynchronous or not. It seems like in even the simplest case it fails to work as I would expect.

// my-first-test.js

var vows = require('vows'),
    assert = require('assert');

vows.describe('Deep Thought').addBatch({
    'part 1': {
        topic: new Object({foo:'bar'}),

        'should know the answer to the ultimate question of life': function (deepThought) {
            setTimeout(function(){
                assert.equal (obj.foo,'br');
            },1000);

            setImmediate(function(){
                assert.equal (obj.foo,'ba');
            });
        }
    },
    'part 2': {
        topic: new Object({foo:'baz'}),

        'should know the answer to the ultimate question of life': function (obj) {
            setTimeout(function(){
                assert.equal (obj.foo,'br');
            },1000);

            setImmediate(function(){
                assert.equal (obj.foo,'ba');
            });
        }
    }
}).run();

what happens, is that upon the first assert error, the test framework conks out. Ideally, upon each assertion error, they could get trapped and the test library could move on to the next test.
Is it possible to test asynchronous code like this? Am I missing something?

as you can see, the output of

$ node my-first-test.js

is

·· ✓ OK » 2 honored (0.009s) 

assert.js:89
  throw new assert.AssertionError({
  ^
AssertionError: expected {expected},
    got  {actual} ({operator})
    at Immediate._onImmediate (/Users/amills001c/WebstormProjects/ORESoftware/suman/competition/vows/ex1.js:14:24)
    at processImmediate [as _immediateCallback] (timers.js:383:17)

I am aware, that with domains being deprecated in Node core, it may be impossible to trap errors that are thrown asynchronously...

but if I fix so that the assert's don't throw:

var vows = require('vows'),
    assert = require('assert');

vows.describe('Deep Thought').addBatch({
    'part 1': {
        topic: new Object({foo:'bar'}),

        'should know the answer to the ultimate question of life': function (obj) {
            setTimeout(function(){
                assert.equal (obj.foo,'bar');
            },1000);

            setImmediate(function(){
                assert.equal (obj.foo,'bar');
            });
        }
    },
    'part 2': {
        topic: new Object({foo:'baz'}),

        'should know the answer to the ultimate question of life': function (obj) {
            setTimeout(function(){
                assert.equal (obj.foo,'baz');
            },1000);

            setImmediate(function(){
                assert.equal (obj.foo,'baz');
            });
        }
    }
}).run();

then I get

·· ✓ OK » 2 honored (0.008s) 
@ORESoftware
Copy link
Author

As you can see, AVA has the same problem as your library

avajs/ava#567

because both Vows and AVA run tests asynchronously, not just in series

@evanp
Copy link
Contributor

evanp commented Apr 27, 2018

You need to do all your async stuff in the topic. The tests are sync.

@evanp evanp closed this as completed Apr 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants