how to get list of files in a directory recursively in Cypress?



  • I wrote this code and it works fine, that is written in TypeScript. When I use the same code in the test file in Cypress I get error TypeError: fs.readdir is not a function

    import * as fs from 'fs'
    
    let inputPath: String = "C:\\Users\\rkon";
    
    
    let replacementString = "/";
    let newInputPath = inputPath.split('\\').join(replacementString)
    console.log('path after replacement: ' + newInputPath);
    fs.readdir(newInputPath as string, function (err: any, files: any[]) {
        //handling error
        if (err) {
            return console.log('Unable to scan directory: ' + err);
        }
        //listing all files using forEach
        files.forEach(function (file) {
            console.log('f: ' + file);
    
        });
    });
    

    I verified the above code by first doing

    >tsc temp.ts
    >node temp.js
    

    As I said it worked fine but not sure why does the same code does not work in Cypress and gives the error

    TypeError: fs.readdir is not a function

    Here is the tsconfig.json

    {
         "compilerOptions": {
    
            "esModuleInterop": true,
            "moduleResolution": "node",
            "module": "commonjs",
            "resolveJsonModule": true,
            "skipLibCheck": true,
            "noImplicitAny": true,
            "sourceMap": true,
            "allowJs": true,
            "strict": true,
            "target": "es2017",
            "lib": ["es2017"],
            "typeRoots": ["./typings","./node_modules/@types","node_modules/@types/node","node_modules/@types"],
            "baseUrl": "../node_modules",
            "paths": {
                "*": ["./node_modules/@types/*"]
            },
            "outDir": "build/"
        }
    
    }
    

    Here are the versions

    >node -v
    v12.13.0
    
    >npm -v
    6.12.0
    
    >tsc -v
    Version 3.7.2
    
    >.\node_modules\.bin\cypress -v
    Cypress package version: 3.5.0
    Cypress binary version: 3.5.0
    >ver
    Microsoft Windows [Version 10.0.17763.805]
    


  • Surprisingly both the statements below work well in Windows machine to get the directory (Note that this solution is a workaround due to the fact that Cypress tests run in browser environment.)

            cy.exec('pwd').then((result) =>
                cy.log('pwd res:' + JSON.stringify(result))
            );
    
            cy.exec('cd').then( (result) =>
                cy.log('cd res:' + JSON.stringify(result))
            );
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2