【问题标题】:nodejs: how to run tests and then build dockerfilenodejs:如何运行测试然后构建 dockerfile
【发布时间】:2020-10-15 09:39:47
【问题描述】:

我正在尝试创建一个基本的 CI CD 管道,我正在尝试创建批处理文件来运行测试,然后执行 docker build。这是我的基本批处理文件

@ECHO OFF
call npm run test
call docker build -t my-docker-file .
PAUSE

我如何知道是否所有的测试都成功运行了?我正在使用摩卡和柴

【问题讨论】:

    标签: node.js mocha.js chai


    【解决方案1】:

    首先,您需要使用--exit 标志来执行您的测试。我的 package.json 看起来像这样:

    "test": "mocha test --timeout 4000  --exit"
    

    所以当测试完成时控制台不会卡在打开状态。

    然后,想法是在批处理和节点之间创建通信。我用过 bash,但想法是一样的。

    如果测试已通过并且在执行后检查变量是否设置为 true,则基本上是写入 .env 文件。

    首先,要执行此操作,您需要使用此更新您的 test.js 文件。

    我使用envfile。你可以查看这个question

    //get the reference to your environment file and update
    
    //THIS is you have to add
    afterEach(function () {
        const state = this.currentTest.state;
        if (state !== "passed") {
            //write into .env: MYENVVAR=0
        }
    });
    

    然后,您将在 .env 文件中添加一个变量,以了解测试是否已正确执行。

    因此,下一步是读取批处理中的值,如果正常则继续。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      • 1970-01-01
      • 2020-06-14
      • 2021-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多