jbrumwell / mock-knex

A mock knex adapter for simulating a database during testing
MIT License
239 stars 71 forks source link

Requires an actual database for mock-knex to work #91

Open dougmolineux opened 4 years ago

dougmolineux commented 4 years ago

Thank you for this module, it's helpful to me. I am setting up my Knex object like this:

function setupDB() {
  if (process.env.NODE_ENV === 'test') {
    return Knex({ client: 'mssql' });
  }
  return Knex({
    client: 'mssql',
    connection: {
      database : process.env.DBNAME,
      host : process.env.DBHOST,
      password : process.env.DBPASS,
      user : process.env.DBUSER
    },
    useNullAsDefault: true
  });
}

I am using a local MSSQL database to test this out (ran in docker-compose), and all my tests work if the mssql database is up and running locally. My process environment variables are all configured to point to this local database.

When i run docker-compose down, stopping the container running mssql, all my tests timeout. I was hoping that when the database isn't running locally, that the mockDb would still work. Is that not correct? Am I doing something wrong here?

Thank you again.

jbrumwell commented 4 years ago

@dougmolineux I am able to run my tests in mysql and postgres without a db running, this may be specific to the mssql adapter in knex. What version of knex are you on?

dougmolineux commented 4 years ago

Thank you for the response, the versions I currently have are:

    "mock-knex": "^0.4.5",
    ...
    "@types/knex": "^0.16.1",
    "knex": "0.16.5",

I could attempt to work on something that reproduces this next week if thats helpful to you, thanks again.

jbrumwell commented 4 years ago

@dougmolineux That would be great :)

felixmosh commented 3 years ago

Try knex-mock-client it works with a different approach, and it doesn't requires a real SQL server running at all.