Search code examples
javascriptmysqldatabasewebpackcypress

Webpack Compilation Error after running the test and querying the table in the database


I need to run an automated test (Cypress) that connects to my database (mysql) and does a select (lib mysql2). I am unable to run my test successfully.

I'm getting the following error:

Error: Webpack Compilation Error
./node_modules/lru-cache/dist/mjs/index.js 48:8
Module parse failed: Unexpected token (48:8)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| }
| class Stack {
>     heap;
|     length;
|     // private constructor
 @ ./node_modules/mysql2/lib/parsers/parser_cache.js 3:12-32
 @ ./node_modules/mysql2/promise.js

I imagined it would be something related to node_modules. I deleted and installed everything again. However, the error remains.

Could anyone help me?

> My file db.js below:

var mysql = require('mysql2/promise');

var pool = mysql.createPool({
    host: "for-qa-dblibsmetricsinformation.clmii4vy3haa.us-east-1.rds.amazonaws.com",
    user: "usr_test",
    password: "usr-test",
    database: "db_test",
    connectionLimit: 10 // maximum number of connections in the pool
});

module.exports = pool;

> My file .spec.js below

const pool = require('../../../../../../../../db.js');

describe('Test DB', () => {
  it('should consult a new user', () => {
    const result = pool.execute('select user_name from table_test');
    cy.log(result);
  });
});

Thanks in advance!!


Solution

  • Try using npm i cypress-sql-server

    const sqlServer = require('cypress-sql-server')

    module.exports = (on, config) => { tasks = sqlServer.loadDBPlugin(config.db); on('task', tasks); }

    In your spec:

    import sqlServer from 'cypress-sql-server';

    cy.sqlServer('SELECT 'test').should('eq', 'test');

    https://www.npmjs.com/package/cypress-sql-server?activeTab=readme