簡體   English   中英

如何使用Grunt和Mocha按mtime順序運行單元測試

[英]How to run unit tests in mtime order with Grunt and Mocha

因此,當您執行TDD時,是否要等待它運行所有測試,直到您正在進行該測試? 這需要太多時間。 當我趕時間時,我將測試文件重命名為aaaaaaaaaaaaaaaaaa_testsomething.test.js之類,因此它首先運行,並且我很快看到錯誤。

我不喜歡這種方法,並且肯定有解決方案,但是找不到。 那么,用Mocha按mtime順序運行單元測試的最簡單方法是什么? 有-sort選項,但是它僅按名稱對文件進行排序。 如何按修改時間對它們進行排序?

這是我的Gruntfile.js:

 module.exports = function(grunt) { grunt.initConfig({ watch: { tests: { files: ['**/*.js', '!**/node_modules/**'], tasks: ['mochacli:local'] } }, mochacli: { options: { require: ['assert'], reporter: 'spec', bail: true, timeout: 6000, sort: true, files: ['tests/*.js'] }, local: { timeout: 25000 } } }); grunt.loadNpmTasks('grunt-mocha-cli'); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.registerTask('test', ['mochacli:local']); grunt.registerTask('livetests', [ 'watch:tests']); }; 

注意:它不是重復的。 我不想每次保存源代碼文件時都編輯測試或Gruntfile.js。 我在問如何修改Grunt任務,以便它首先從最后修改的* .test.js文件運行測試。 如標題中所述,按mtime對單元測試進行排序。

一個簡單的場景:我在編輯器中打開test1.test.js,對其進行更改,按Ctrl + B組合鍵,然后從test1.test.js然后是test4.test.js運行單元測試。 我打開test4.test.js,按Ctrl + S,Ctrl + B,然后從test4.test.js然后是test1.test.js運行測試

我正在考慮使用某些Grunt插件首先對文件進行排序,因此我可以將其結果放在帶有grunt.config.set('mochacli.options.files', 'tests/recent.js,tests/older.js', ....);的'tests / * grunt.config.set('mochacli.options.files', 'tests/recent.js,tests/older.js', ....); 但是我在那里找不到可以用作中間件的任何東西,也不想發明自行車,因為我確信已經實現了此功能。

如果您使用的是Mocha,則可以僅對您感興趣的測試設置。

describe(function () {
  // these tests will be skipped
});
describe.only(function () {
  // these tests will run
})

不想發明自行車,因為我確信已經有一些東西可以實施了。

...有時您必須騎自行車;)


這可以通過在您的Gruntfile.js注冊一個中間定制任務來動態執行以下操作來實現:

  1. 獲得用於所有單元測試文件(文件路徑.js利用) grunt.file.expand與適當的通配圖案。
  2. 按文件mtime / modified-date對每個匹配的文件路徑進行排序。
  3. 使用grunt.config使用按時間順序排序的文件路徑配置mochacli.options.file數組
  4. 使用grunt.task.run運行mochacli任務中定義的local 目標

Gruntfile.js

如下配置您的Gruntfile.js

module.exports = function(grunt) {

  // Additional built-in node module.
  var statSync = require('fs').statSync;

  grunt.initConfig({
    watch: {
      tests: {
        files: ['**/*.js', '!**/node_modules/**', '!Gruntfile.js'],
        tasks: ['runMochaTests']
      }
    },
    mochacli: {
      options: {
        require: ['assert'],
        reporter: 'spec',
        bail: true,
        timeout: 6000,
        files: [] // <-- Intentionally empty, to be generated dynamically.
      },
      local: {
        timeout: 25000
      }
    }
  });

  grunt.loadNpmTasks('grunt-mocha-cli');
  grunt.loadNpmTasks('grunt-contrib-watch');

  /**
   * Custom task to dynamically configure the `mochacli.options.files` Array.
   * All filepaths that match the given globbing pattern(s), which is specified
   # via the `grunt.file.expand` method, will be sorted chronologically via each
   * file(s) latest modified date (i.e. mtime).
   */
  grunt.registerTask('runMochaTests', function configMochaTask() {
    var sortedPaths = grunt.file.expand({ filter: 'isFile' }, 'tests/**/*.js')
      .map(function(filePath) {
        return {
          fpath: filePath,
          modtime: statSync(filePath).mtime.getTime()
        }
      })
      .sort(function (a, b) {
        return a.modtime - b.modtime;
      })
      .map(function (info) {
        return info.fpath;
      })
      .reverse();

    grunt.config('mochacli.options.files', sortedPaths);
    grunt.task.run(['mochacli:local']);
  });

  grunt.registerTask('test', ['runMochaTests']);
  grunt.registerTask('livetests', [ 'watch:tests']);

};

補充筆記

使用上面的配置。 通過CLI運行$ grunt livetests ,然后保存修改后的測試文件,將使Mocha根據文件的最后修改日期按時間順序運行每個測試文件(即,最新的修改文件將首先運行,最后修改的文件將運行持續)。 同樣的邏輯在運行$ grunt test也適用。

但是,如果你想摩卡先運行最新修改的文件,但在正常的順序運行中的其他文件(按名稱即),然后將自定義runMochaTests任務在Gruntfile.js上面應該有以下邏輯,而不是取代:

/**
 * Custom task to dynamically configure the `mochacli.options.files` Array.
 * The filepaths that match the given globbing pattern(s), which is specified
 # via the `grunt.file.expand` method, will be in normal sort order (by name).
 * However, the most recently modified file will be repositioned as the first
 * item in the `filePaths` Array (0-index position).
 */
grunt.registerTask('runMochaTests', function configMochaTask() {
  var filePaths = grunt.file.expand({ filter: 'isFile' }, 'tests/**/*.js')
    .map(function(filePath) {
      return filePath
    });

  var latestModifiedFilePath = filePaths.map(function(filePath) {
      return {
        fpath: filePath,
        modtime: statSync(filePath).mtime.getTime()
      }
    })
    .sort(function (a, b) {
      return a.modtime - b.modtime;
    })
    .map(function (info) {
      return info.fpath;
    })
    .reverse()[0];

  filePaths.splice(filePaths.indexOf(latestModifiedFilePath), 1);
  filePaths.unshift(latestModifiedFilePath);

  grunt.config('mochacli.options.files', filePaths);
  grunt.task.run(['mochacli:local']);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM