Search code examples
javascriptcypresssybase

spawn is not a function


I created a before to run an update on a Sybase database before a certain test in Cypress, but the following error occurs when running "spawn is not a function"

before(() => {
    var Sybase = require('sybase'),
    db = new Sybase('host', '4400', 'dbname', 'user', 'pass');

db.connect(function (err) {
    if (err) return console.log(err);

    db.query("update ..... ", function (err, data) {
        if (err) console.log(err);

        console.log(data);

        db.disconnect();

    });
});```

Solution

  • The package node-sybase requires NodeJS to run, since it calls spawn which is a Node function.

    You'll need to add you sybase code to a task which is where Cypress runs Node code,

    /cypress/plugins/index.js

    module.exports = (on, config) => {
      on('task', {
        sybaseQuery(queryString) {
    
          // sybase connection and query code here
    
          return null
        },
      })
    }
    

    test

    before(() => {
      cy.task('sybaseQuery', 'update...')
    })