Testcafe V1.9.0 UnhandledPromiseRejectionWarning:TypeError:无法将未定义或null转换为对象

问题描述

我正在弄丢大理石弹,试图弄清楚为什么testcafe这么痛苦。所以这是我的情况: 我有一个运行程序,它针对浏览器堆栈启动测试。精细。一旦我将testcafe版本从1.6.1升级到1.9.0,浏览器堆栈运行器将无法启动。我收到此错误

(node:12310) UnhandledPromiseRejectionWarning: TypeError: Cannot convert undefined or null to object
    at Function.entries (<anonymous>)
    at testcafeConfiguration.mergeOptions (/Users/testcafe/node_modules/testcafe/lib/configuration/configuration-base.js:50:16)
    at testcafeConfiguration.init (/Users/testcafe/node_modules/testcafe/lib/configuration/testcafe-configuration.js:48:14)
    at async getConfiguration (/Users/testcafe/node_modules/testcafe/lib/index.js:41:9)
    at async createtestcafe (/Users/testcafe/node_modules/testcafe/lib/index.js:57:27)
(node:12310) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection,use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:12310) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

如果我将版本升级到1.7.0,这很奇怪,我没有遇到任何问题。任何版本1.8.0或更高版本都会出现上述错误

也有人经历过吗?有人可以阐明这个问题吗?

这是我的跑步者SetupRunners.js文件

import getRunnerSrc from "./getRunnerSrc";
import getRunnerbrowsers from "./getRunnerbrowsers";


export default function setupRunner(runner,options = {},cb) {
    let stream = false;

    const src = options.src || getRunnerSrc(options.breakpoint);

    if (src && src.length) {
        runner.src(src);

        runner.browsers(
            options.browsers || getRunnerbrowsers(options.breakpoint)
        );
  
        return cb(runner).then(data => {
            if (stream) {
                stream.end();
            }
            return data;
        });
    }
}

这是针对浏览器堆栈启动测试的文件

import getArgs from "./getArgs";
import sanitizeArg from "./sanitizeArg";
import isbrowserstack from "./getIsbrowserstack";

const defaultbrowsers = {
    desktop: "Chrome:OS X",mobile: "Safari:iOS",};
const browserstackPrefix = "browserstack:";

export default function getRunnerbrowsers(breakpoint) {
    const usebrowserstack = isbrowserstack();

    if (usebrowserstack && breakpoint) {
        return getbrowserstackDevices(breakpoint);
    }

    return `${usebrowserstack ? browserstackPrefix : ""}${sanitizeArg(
        getArgs("browsers") || defaultbrowsers.desktop
    )}`;
}

function getbrowserstackDevices(breakpoint) {
    return `${browserstackPrefix}${getArgs(`${breakpoint}_devices`) ||
        defaultbrowsers[breakpoint]}`;
}

这是助手getArgs.js:

function getArgs(key) {
    if (process && process.argv && process.argv.length) {
        return process.argv.reduce((val,arg) => {
            const split = arg.split("=");
            if (split[0] === key || split[0] === "--" + key) {
                val = split[1];
            }
            return val;
        },null);
    }
    return null;
}

module.exports = getArgs;

和santizeArgs.js

export default function sanitizeArg(ar) {
    return filterEmpty((ar || "").split(",").map(str => str.trim()));
}

export function filterEmpty(items) {
    return items.filter(item => item && item.length);
}

========更新=======

在v1.7.0上创建testcafe功能

async function createtestcafe(hostname,port1,port2,sslOptions,developmentMode,retryTestPages) {
    console.dir
    const configuration = new testcafe_configuration_1.default();
    await configuration.init({
        hostname,ssl: sslOptions,retryTestPages
    });

在将testcafe更新到v1.9.2之后创建testcafe功能

async function createtestcafe(...args) {
    const configuration = await getConfiguration(args); // HERE IS WHERE THE ERROR IS
    const [hostname,port2] = await Promise.all([
        getValidHostname(configuration.getoption(option_names_1.default.hostname)),getValidPort(configuration.getoption(option_names_1.default.port1)),getValidPort(configuration.getoption(option_names_1.default.port2))
    ]);
    configuration.mergeOptions({ hostname,port2 });
    const testcafe = new testcafe(configuration);
    setupExitHook(cb => testcafe.close().then(cb));
    return testcafe;
}

解决方法

我根据您的信息重现了该问题,并在TestCafe GitHub存储库中创建了issue。解决方法是,您可以更正传递给'createTestCafe'函数的参数值。

根据您共享的输出,您将'null'值作为'hostname'参数传递。我猜'null'值意味着未指定'hostname'值。在这种情况下,您需要向'createTestCafe'函数返回'undefined'而不是'null'。