0

Trying to wrap my head around this and it's throwing my mind in an async loop.

After running this simple test, the result when I check RethinkDB manually is correct (one table named 'books' in the db 'test'); however, this test passes no matter what I assert in the expect function from chai. I know this is an async issue because the console.log(result) prints to the console after the test has finished. I would have thought expect would run after Rethink got the tableList() because it is in the callback.

  1. Why is this test passing (It should fail where ['books'] === ['anything']?
  2. Why isn't expect() run after tableList()?
  3. What is the proper way to chain commands to RethinkDB so that they are executed in order?

db_spec.js:

import {expect} from 'chai'
import r from 'rethinkdb'

describe('rethinkdb', () => {
  it('makes a connection', () => {
    var connection = null;
    r.connect({host: 'localhost', port: 28015}, function(err, conn) {
      if (err) throw err
      connection = conn

      r.dbDrop('test').run(connection, () => {
        r.dbCreate('test').run(connection, () => {
          r.db('test').tableCreate('books').run(connection, () => {
            r.db('test').tableList().run(connection, (err, result) => {
              if (err) throw err
              console.log(result)
              expect(result).to.equal(['anything'])
            })
          })
        })
      })
    })
  })
})
RichardForrester
  • 1,048
  • 11
  • 19
  • I believe you should be using `expect(result).to.deep.equal(...)`. Directly comparing arrays in Javascript isn't that simple. – Tryneus Dec 16 '15 at 00:51
  • 2
    As for the async problem, your test case isn't taking a `done` callback, so the framework assumes your test is synchronous. To fix this, the test case declaration should be `it('makes a connection', (done) => ...)`, and you should call `done()` after your `expect`. – Tryneus Dec 16 '15 at 00:56
  • @Tryneus Thank you. See the modified code. There are still some issues. – RichardForrester Dec 16 '15 at 01:23
  • @Tryneus Actually, nevermind, it's still not reading the inner most callback for some reason. – RichardForrester Dec 16 '15 at 01:29
  • 1
    Very helpful link to help me understand: http://staxmanade.com/2015/11/testing-asyncronous-code-with-mochajs-and-es7-async-await/ – RichardForrester Dec 18 '15 at 20:26

1 Answers1

0

I think this would work but the inner most callback is not being executed and the test is just timing out.

import {expect} from 'chai'
import r from 'rethinkdb'

describe('rethinkdb', () => {
  it('makes a connection', (done) => {
    var connection = null;
    r.connect({host: 'localhost', port: 28015}, function(err, conn) {
      if (err) throw err
      connection = conn

      r.dbDrop('test').run(connection, () => {
        r.dbCreate('test').run(connection, () => {
          r.db('test').tableCreate('books').run(connection, () => {
            r.db('test').tableList().run(connection, (err, result) => {
              if (err) throw err
              expect(result[0]).to.equal('books').done()
            })
          })
        })
      })
    })
  })
})
RichardForrester
  • 1,048
  • 11
  • 19