【问题标题】:JS unit testing: run tests on file changes (like nodemon)JS 单元测试:对文件更改运行测试(如 nodemon)
【发布时间】:2012-07-07 19:25:07
【问题描述】:

我有两个关于 JS 单元测试的问题:

1) 是否有一些工具允许在某些文件更改时自动运行 javascript 单元测试(例如 nodemon 在 js 更改时重新启动 node.js)。

2) 这种策略是否适合(有效)运行单元测试?

谢谢, 亚历克斯

【问题讨论】:

  • 试试 testem。它会在文件更改时持续运行您的单元测试
  • 好的,我去看看!谢谢!

标签: javascript unit-testing node.js testing


【解决方案1】:

对于那些致力于使用 nodemon 的人,nodemon -x "npm test" 对我有用。

一点解释

nodemon --help 说:

-x, --exec app ........... execute script with "app", ie. -x "python -v".

在我们的例子中,npm test 被设置为通过配置我们的 package.json 来运行测试

例如:

"scripts": {
  "test": "mocha"
},

【讨论】:

    【解决方案2】:

    使用 jest 时,不需要 nodemon。只需在 package.json 中将test 脚本命令设置为jest --watchAll 如下:

    "scripts": {
      "test": "jest --watchAll"
    }
    

    【讨论】:

      【解决方案3】:

      查看 grunt 构建系统和 watch 任务。您可以设置 grunt 以监视文件更改,然后运行您想要的任何任务(测试、lint、编译等)。

      https://github.com/cowboy/grunt

      本教程涵盖了一些想法。 http://javascriptplayground.com/blog/2012/04/grunt-js-command-line-tutorial

      【讨论】:

        【解决方案4】:

        这是我的 package.json 的 sn-p:

        "scripts": {
          "develop": "nodemon ./src/server.js --watch src --watch __tests__ -x \"yarn run test\"",
          "test": "mocha ./__tests__/**/*.js --timeout 10000" }
        

        develop 脚本是我运行我的 express 服务器的命令行(入口:./src/server.js),观看包含我所有服务器/API 代码的 /src 目录,观看我所有的 /__tests__ 目录规范,最后告诉 nodemon 在每次重新启动/运行之前执行附带的语句-x \"yarn run test\"

        yarn run testnpm run test 没有什么不同。我更喜欢 yarn 而不是 npm,所以这部分取决于你。重要的是 JSON 值中的 \" 标记......没有它,它将失败,因为参数将被错误地标记。

        此设置允许我从服务器/API 代码或编写/修复规范触发更改,并在通过 nodemon 重新启动服务器之前触发完整的测试运行。

        干杯!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-04-03
          • 2012-01-08
          • 2019-07-22
          • 1970-01-01
          • 2017-05-07
          • 2012-11-28
          • 1970-01-01
          相关资源
          最近更新 更多