Skip to content

Instantly share code, notes, and snippets.

@tily
Last active December 20, 2017 13:59
Show Gist options
  • Save tily/ed73dc17ee8a07c638901f13e95955be to your computer and use it in GitHub Desktop.
Save tily/ed73dc17ee8a07c638901f13e95955be to your computer and use it in GitHub Desktop.
mocha のテストに無理やり連番を振って番号指定でテスト実行できるようにする ref: https://qiita.com/tily/items/767d1d5acaabe9dcf4fb
const Mocha = require("mocha");
const originalRun = Mocha.prototype.run;
Mocha.prototype.run = function (fn) {
let i = 1
this.loadFiles(() => {
this.suite.eachTest((test) => {
test.title = `#${i}: ${test.title}`
i += 1
})
})
return originalRun.apply(this, [fn])
}
const assert = require("assert");
describe("Numbered tests", ()=> {
for(let i = 0; i < 15; i += 1) {
it("test hogehoge", () => {
assert(1 == 1)
})
}
})
$ mocha --require mocha_ext.js test.js
Numbered tests
✓ #1: test hogehoge
✓ #2: test hogehoge
✓ #3: test hogehoge
✓ #4: test hogehoge
✓ #5: test hogehoge
✓ #6: test hogehoge
✓ #7: test hogehoge
✓ #8: test hogehoge
✓ #9: test hogehoge
✓ #10: test hogehoge
✓ #11: test hogehoge
✓ #12: test hogehoge
✓ #13: test hogehoge
✓ #14: test hogehoge
✓ #15: test hogehoge
15 passing (11ms)
$ mocha --require mocha_ext.js test.js --grep '#8:'
Numbered tests
✓ #8: test hogehoge
1 passing (10ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment