-1

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]
  • Hi show your tsconfig , I think you should add the node type also into the tsconfig and package.json for this to work . – PDHide Nov 23 '19 at 19:37
  • stackoverflow.com/a/43048371/6793637 – PDHide Nov 23 '19 at 20:06
  • @PDHide I do have "typeRoots": ["./node_modules/@types"] in package.json – RajKon Nov 27 '19 at 21:19
  • if I run >tsc temp.ts --noResolve then I get error temp.ts:14:21 - error TS2307: Cannot find module 'fs'. – RajKon Nov 27 '19 at 21:25
  • it might have to do something with Cypress as it runs the tests in browser. I see some options like brfs and browserify-fs – RajKon Nov 29 '19 at 3:09
0

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))
        );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.