Search code examples
cypressssh2-sftpssh2-sftp-client

How to connect to SFTP server in Cypress test?


I am trying to connect to an SFTP server using the ssh2-sftp-client NPM package in my Cypress test.

Here is my test at the moment

describe('example to-do app', () => {
    it('displays two todo items by default', () => {
        let Client = require('ssh2-sftp-client');
        let sftp = new Client();
        
        sftp.connect({
          host: 'myHost',
          port: 'myPort',
          username: 'myUsername',
          password: 'myPassword'
        }).then(() => {
          return sftp.list('/reports');
        }).then(data => {
          console.log(data, 'the data info');
        }).catch(err => {
          console.log(err, 'catch error');
        });
    })
})

Currently, when I run the test I get this error:

Cannot read properties of undefined (reading 'DEFLATE')
node_modules/ssh2/lib/protocol/zlib.js:7:1
   5 |   createInflate,
   6 |   constants: {
>  7 |     DEFLATE,
     | ^
   8 |     INFLATE,
   9 |     Z_DEFAULT_CHUNK,
  10 |     Z_DEFAULT_COMPRESSION,

Can someone please tell me how to resolve this issue?


Solution

  • Establishing such connection in a test will not work. This is because cypress does not communicate with a Node.js process supplied by the host. In cypress if we need to run node code, we need to use their so called cy.task Here is the link to their docs - https://docs.cypress.io/api/commands/task#Examples

    That's why you need to establish this connection in your cypress/plugins/index.js file inside a task and then use this task in your test.

    Here is an example of connecting to mysql with ssh - How do I connect mysql with cypress through ssh tunneling?