How do I make cypress requests to an H2 file database hosted in Spring Boot?

Viewed 23

I'm trying to submit SQL to an H2 in-memory file database which is created and hosted in a Spring Boot application from Cypress tests. For example, the following is used as part of checking whether a user exists:

cy.task('queryDb', `SELECT COUNT(*) as "rowCount" FROM USERS WHERE username=` + user.username).then((result) => {...}))

However, whenever I attempt to submit the request, I get a "getaddrinfo ENotFound" error.

I'm stuck trying to work out what the correct URL to submit this request to is, especially which parts to put as host/database in my .env file. From what I can tell, it should be jdbc:h2:file:./data/userdb (at least, that's what H2-console lists as my JDBC URL and as my database name), but this doesn't seem to be working for me. Do I need to prefix or somehow otherwise include the localhost port on which it can be accessed through spring? If so, is that the same port as h2-console is available on?

(In case it's atypical, my config file looks like this)

// Sends a query to the database
function queryTestDb(query, config) {
  // Creates a new mysql connection using credentials from cypress.json env's
  const connection = mysql.createConnection(config.env.db)
  // Start connection to db
  connection.connect()
  // Exec query + disconnect to db as a Promise
  return new Promise((resolve, reject) => {
    connection.query(query, (error, results) => {
      if (error) reject(error)
      else {
        connection.end()
        return resolve(results)
      }
    })
  })
}

export default defineConfig({
  e2e: {
    baseUrl: 'http://localhost:9000',
    supportFile: false,
    setupNodeEvents(on, config) {
      on('task', {
        queryDb: query => {
          return queryTestDb(query, config)
        },
      });
    }
  },
});
1 Answers

H2 database urls are listed here: http://h2database.com/html/features.html#database_url

It seems that since you're accessing a spring boot app from Cypress, you should use the remote connection: jdbc:h2:tcp://localhost:9000/data/userdb. Unfortunately I'm not able to test this right now, so cannot confirm.

Related