totaljs / framework

Node.js framework
http://www.totaljs.com
Other
4.36k stars 450 forks source link

Test not ended correctly #611

Closed ckpiggy closed 6 years ago

ckpiggy commented 6 years ago

If there are several asserts with a little delay in test for example like below:

TEST('long run', function () {
  OK(true, 'long run 0')
  setTimeout(function () {
    OK(true, 'long run 1')
    OK(true, 'long run 2')
    OK(true, 'long run 3')
    OK(true, 'long run 4')
    OK(true, 'long run 5')
  }, 100)
})

TEST('next', function () {
  OK(true, 'the next test')
})

The next test will run before long run ended, and the log seems wired...

===================== TESTING ======================

[ TEST: long-run/demo.js ]

Passed ............. long run <long run 0> [0 ms]
Passed ............. next <the next test> [0 ms]

===================== RESULTS ======================

> Passed ......... 2/2
> Failed ......... 0/2

Passed ............. next <long run 1> [101 ms]
Passed ............. next <long run 2> [101 ms]
Passed ............. next <long run 3> [102 ms]
Passed ............. next <long run 4> [102 ms]
Passed ............. next <long run 5> [102 ms]

===================== RESULTS ======================

> Passed ......... 7/7
> Failed ......... 0/7

How about we pass a callback to the test function, so that the test runner could know the test ended. ex:

TEST('long run', function (finished) {
   OK(true, 'long run 0')
  setTimeout(function () {
    OK(true, 'long run 1')
    OK(true, 'long run 2')
    OK(true, 'long run 3')
    OK(true, 'long run 4')
    OK(true, 'long run 5')
    finished()
  }, 100)
})
petersirka commented 6 years ago

@ckpiggy you can use multiple times OK() or FAIL() but not in another callback. This is correct.

TEST('long run 1', function () {
  OK(true, 'long run 1');
});

TEST('long run 2', function () {
  OK(true, 'long run 2');
});

// etc..

TEST('next', function () {
  OK(true, 'the next test')
})

In other word: OK() and FAIL() ends the current test. It's something similar to your finished example. Solution:

TEST('long run', function () {
  setTimeout(function () {
    OK(true, 'long run 0')
    OK(true, 'long run 1')
    OK(true, 'long run 2')
    OK(true, 'long run 3')
    OK(true, 'long run 4')
    OK(true, 'long run 5')
  }, 100)
});
ckpiggy commented 6 years ago

@petersirka I think the document should update, it looks like we could have multiple OK in a test. 2018-02-19 6 02 16

petersirka commented 6 years ago

Yes, but in a row ... But I'll add not in some callback.

ckpiggy commented 6 years ago

@petersirka I think I got your point now. The OK and FAIL should be called synchronized. My test look like this

TEST('get service', 'some/url',async function (builder) {
  builder.get().exec(async (err, data, resp) => {
    OK(resp.status === 200, 'status')
    try {
     const dbData = await fetchDbData()
     OK(dbData === mockedData, 'check db')
     }
  })
})

And the problem happened in second OK check So I can simply modify my test to

TEST('get service', 'some/url',async function (builder) {
  builder.get().exec(async (err, data, resp) => {
    try {
     const dbData = await fetchDbData()
     OK(resp.status === 200, 'status')
     OK(dbData === mockedData, 'check db')
     }
  })
})
petersirka commented 6 years ago

Yes, this can solve your problem.