Search code examples
javascripttwitter-bootstraptestingautomated-teststestcafe

TypeError: createTestCafe is not a function


I am getting this error when I copied runner code from TestCafe site and try to run it. I have testcafe 1.6.0 on Ubuntu 18.04

Below is my runner,

const createTestCafe = require('/usr/local/bin/testcafe');
let testcafe         = null;

createTestCafe('localhost', 1337, 1338)
    .then(tc => {
       testcafe     = tc;
       const runner = testcafe.createRunner();

       return runner
          //.src(['__test__/*.js', 'tests/func/fixture3.js'])
          //.browsers(['chrome:headless --no-sandbox --disable-gpu', 'safari'])

          .src(['./__tests__/testcafe1.js'])
          .browsers(['chrome'])
          .run();
    })
    .then(failedCount => {
       console.log('Tests failed: ' + failedCount);

       testcafe.close();
    });

Below is my package.json

{
  "private": true,
  "scripts": {
    "test": "testcafe 'chrome:headless --no-sandbox' ./__tests__/*.js --hostname localhost"
  },
  "devDependencies": {
    "chalk": "^2.4.2",
    "prettier": "^1.18.2",
    "testcafe": "*",
    "rimraf": "^2.6.3"
  }
}

Below is the error:

ERROR Cannot prepare tests due to an error.

TypeError: createTestCafe is not a function
    at Object.createTestCafe (/app/code/testcafe_runner.js:6:1)
    at Function._execAsModule (/usr/local/lib/node_modules/testcafe/src/compiler/test-file/api-based.js:50:13)
    at ESNextTestFileCompiler._runCompiledCode (/usr/local/lib/node_modules/testcafe/src/compiler/test-file/api-based.js:150:42)
    at ESNextTestFileCompiler.execute (/usr/local/lib/node_modules/testcafe/src/compiler/test-file/api-based.js:174:21)
    at ESNextTestFileCompiler.compile (/usr/local/lib/node_modules/testcafe/src/compiler/test-file/api-based.js:180:21)
    at Compiler._getTests (/usr/local/lib/node_modules/testcafe/src/compiler/index.js:86:31)
    at Compiler._compileTestFiles (/usr/local/lib/node_modules/testcafe/src/compiler/index.js:98:35)
    at Compiler.getTests (/usr/local/lib/node_modules/testcafe/src/compiler/index.js:111:34)
    at Bootstrapper._getTests (/usr/local/lib/node_modules/testcafe/src/runner/bootstrapper.js:81:26)
    at Bootstrapper._bootstrapParallel (/usr/local/lib/node_modules/testcafe/src/runner/bootstrapper.js:214:39)

I'm following the code from Testcafe https://devexpress.github.io/testcafe/documentation/using-testcafe/programming-interface/runner.html

What am I doing wrong here?


Solution

  • You try to require TestCafe's executable file instead of the TestCafe's library. Likely it is located in /usr/lib/node_modules/testcafe or in /usr/local/lib/node_modules/testcafe. You can use the following command to find the path to your globally installed modules:

    npm ls -g
    

    However, it's really better to install TestCafe locally if you want to use it as a library. If you plan to run and distribute your test runner script as a standalone CLI tool, you can achieve it with creating a NPM package and adding your runner script to the bin section in package.json: https://docs.npmjs.com/files/package.json#bin