TypeError: createTestCafe is not a function

TypeError: createTestCafe is not a function

我从 TestCafe 站点复制 运行ner 代码并尝试 运行 时遇到此错误。我在 Ubuntu 18.04

上有 testcafe 1.6.0

下面是我的运行小伙伴,

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

下面是我的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"
  }
}

错误如下:

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)

我正在遵循 Testcafe https://devexpress.github.io/testcafe/documentation/using-testcafe/programming-interface/runner.html

中的代码

我在这里做错了什么?

您忘记了关键字 function

function createTestCafe(...)

您尝试要求 TestCafe 的可执行文件而不是 TestCafe 的库。它可能位于 /usr/lib/node_modules/testcafe/usr/local/lib/node_modules/testcafe 中。您可以使用以下命令查找全局安装模块的路径:

npm ls -g

不过,如果你想把TestCafe当作一个库来使用,最好在本地安装它。如果您计划 运行 并将测试 运行ner 脚本作为独立的 CLI 工具分发,您可以通过创建 NPM 包并将 运行ner 脚本添加到 bin package.json 部分:https://docs.npmjs.com/files/package.json#bin