generate.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const template = require('./template')
  2. let config
  3. const fs = require('fs-extra')
  4. const path = require('path')
  5. const globby = require('globby')
  6. const { runner } = require('mocha-headless-chrome')
  7. if (process.argv[2]) {
  8. config = require(`./${process.argv[2]}.config`)
  9. } else {
  10. config = require('./runner.config')
  11. }
  12. /**
  13. * Generate templates and run tests
  14. */
  15. const tests = []
  16. const cwd = process.cwd()
  17. const tmpDir = path.join(cwd, 'tmp', 'browser')
  18. fs.ensureDirSync(tmpDir)
  19. fs.copySync(path.join(cwd, 'test', 'browser', 'common.js'), path.join(tmpDir, 'common.js'))
  20. let numTests = 0
  21. let passedTests = 0
  22. let failedTests = 0
  23. /** Will run the runners in a series */
  24. function runSerial(tasks) {
  25. var result = Promise.resolve()
  26. start = Date.now()
  27. tasks.forEach(task => {
  28. result = result.then(result => {
  29. if (result && result.result && result.result.stats) {
  30. const stats = result.result.stats
  31. numTests += stats.tests
  32. passedTests += stats.passes
  33. failedTests += stats.failures
  34. }
  35. return task()
  36. }, err => {
  37. console.log(err)
  38. failedTests += 1
  39. })
  40. })
  41. return result
  42. }
  43. Object.entries(config).forEach(entry => {
  44. const test = entry[1]
  45. const paths = globby.sync(test.src)
  46. const templateString = template(paths, test.options.helpers, test.options.specs)
  47. fs.writeFileSync(path.join(cwd, test.options.outfile), templateString)
  48. tests.push(() => {
  49. const file = 'http://localhost:8081/' + test.options.outfile
  50. console.log(file)
  51. return runner({
  52. file,
  53. timeout: 3500,
  54. args: ['disable-web-security']
  55. })
  56. })
  57. })
  58. module.exports = () => runSerial(tests).then(() => {
  59. if (failedTests > 0) {
  60. process.stderr.write(failedTests + ' Failed, ' + passedTests + ' passed\n');
  61. } else {
  62. process.stdout.write('All Passed ' + passedTests + ' run\n');
  63. }
  64. if (failedTests) {
  65. process.on('exit', function() { process.reallyExit(1); });
  66. }
  67. process.exit()
  68. }, err => {
  69. process.stderr.write(err.message);
  70. process.exit()
  71. })