You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Node脚本中避免`Secure coding is not enabled for restorable state...`警告?

如何在Node脚本中避免Secure coding is not enabled for restorable state...警告?

这个警告确实是Mac Sonoma系统带来的小困扰,和Cypress本身的功能没什么关系,主要是系统对应用程序状态恢复的安全校验机制导致的。下面给你几个实用的解决办法:

  • 通过环境变量直接抑制警告
    你可以在执行Cypress的命令前加上特定的环境变量,告诉系统跳过这个安全检查。修改你的command变量就行:

    let command = "NSApplicationCrashOnExceptions=1 npx cypress run --headless --browser chrome --spec './cypress/e2e/LoremProdPage.feature'";
    

    这个环境变量会让系统忽略状态恢复的安全编码要求,直接消除警告。

  • 在Node进程中设置环境变量(更整洁)
    如果你不想把环境变量写在命令串里,也可以在Node脚本里提前设置,子进程(也就是Cypress)会自动继承这个环境变量:

    const { exec } = require('child_process');
    
    // 提前设置环境变量
    process.env.NSApplicationCrashOnExceptions = '1';
    
    let command = "npx cypress run --headless --browser chrome --spec './cypress/e2e/LoremProdPage.feature'";
    
    exec(command, (error, stdout, stderr) => {
        if (error) {
            console.error(`Error executing command: ${error}`);
            return;
        }
        console.log(`Output:\n${stdout}`);
        if (stderr) {
            console.error(`Error Output:\n${stderr}`);
        }
    });
    
  • 过滤掉警告输出(治标不治本但实用)
    要是你不想碰环境变量,也可以在处理stderr的时候,把这条特定的警告过滤掉,只输出真正有用的错误信息:

    exec(command, (error, stdout, stderr) => {
        if (error) {
            console.error(`Error executing command: ${error}`);
            return;
        }
        console.log(`Output:\n${stdout}`);
        if (stderr) {
            // 过滤掉目标警告行
            const filteredLogs = stderr.split('\n').filter(line => 
                !line.includes('WARNING: Secure coding is not enabled for restorable state')
            ).join('\n');
            if (filteredLogs) {
                console.error(`Error Output:\n${filteredLogs}`);
            }
        }
    });
    

顺便提一句:这个警告本身不会影响你的测试执行结果,只是系统的一个提示而已。如果日志里多一行这个对你没什么影响,也可以直接忽略~

备注:内容来源于stack exchange,提问作者MeltingDog

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 10:29:34