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.
- Why is this test passing (It should fail where
['books'] === ['anything']
? - Why isn't
expect()
run aftertableList()
? - 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'])
})
})
})
})
})
})
})