0

I have a js file which supplies some db operations. This file works with promises only which can be chained. To test that class I work with an async function.

The problem is, that whenever I work with promises inside my test function the it function gets blocked for every other test later.

Here are two examples:

'use strict'

const exec = require('child_process').exec
const path = require('path')
const request = require('request')
const expect = require('chai').expect
const createTableStatements = require('../data')

test()

async function test () {
  await testGetUser()
  console.log('1')
  await testGetFaculties()
}

function testGetUser () {
  return new Promise((resolve1) => {
    describe('test get user', function () {
      const db = require('../dbInterface')
      it('test get user should be complete', function () {
        db.dbFunctions.dropAll()
          .then(onResolve => {
              return db.dbFunctions.createTable(createTableStatements.createTableStatements.user)
            }
          )
          .then(() => {
            console.log('success create user table')
            return db.dbFunctions.addUser('1', 'firstName', 'lastName', 'email')
          })
          .then(resolve => {
            return db.dbFunctions.getUser('email', undefined)
          })
          .then(result => {
            expect(result.toString().includes('dummy')).to.equal(false)
          })
          .then(resolve => {
            return db.dbFunctions.dropAll()
          })
          .then(resolve => {
            console.log('resolve')
            resolve1()
          })
          .catch(err => console.log(err))
      })
    })
  })
}


function testGetFaculties () {
  return new Promise(resolve => {
    describe('test get faculties', function () {

      let db
      before(function () {
        db = require('../dbInterface')
      })
      console.log('displayed')
      it('should work', function () {
        console.log('locked')
        expect(db.dbFunctions.getFaculties('hsa')).to.be.an('array').that.does.include('Science')
        resolve()
      })
    })
  })
}

And this is the output

resolve
1
displayed

As you can see console.log('locked') is not being processed. What i figured out so far, that I only have this issue when I call expect within a then function. But this is necessary for my tests.

The test () function should contain much more tests, only for this question I shortened it.

And for clarification: If I only test methods type of testGetFaculties () which don't contains another promise chain inside it works like it should.

Any idea why this is like it is?

Anna Klein
  • 1,906
  • 4
  • 27
  • 56
  • Do not use `new Promise()`. https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it – SLaks Dec 10 '18 at 20:07

1 Answers1

1

Most probably the console.log( 'locked' ); doesn't do anything, because your previous test case was not finished at all.

Writing describe, it, before inside a Promise and containing unreturned Promises is something that you should not do.

Much better test case would look like :

'use strict'

const exec = require('child_process').exec
const path = require('path')
const request = require('request')
const expect = require('chai').expect
const createTableStatements = require('../data')

// You use this in both test cases anyway
const db = require('../dbInterface');

describe('test get user', function () {

    it('test get user should be complete', function () {          
        return db
     // ^ returning promise will make sure that the test ends when the promise ends.
            .dbFunctions
            .dropAll()
            .then(onResolve => { ... } )
            ...
      )
    } );

} );

describe('test get faculties', function () {

    it('should work', function () {
        return db
     // ^ returning promise will make sure that the test ends when the promise ends.
           .dbFunctions
           .getFaculties('hsa')
           .then( value => {
               // ^ You actually need to test the value of the resolve promise
               expect( value ).to.be.an('array').that.does.include('Science');
           } )
    } );

} ); 
drinchev
  • 19,201
  • 4
  • 67
  • 93
  • 1
    You have no idea how much I tried to work with describe only instead of nested promised and the return Promise was the thing I was missing all the time O_o Ty a lot! – Anna Klein Dec 10 '18 at 20:29