SHARE
TWEET

Untitled

a guest Jun 18th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { promisify } = require('util')
  2. const { spawn } = require('child_process')
  3.  
  4. const originalGlob = require('glob')
  5. const { loadOptions } = require('mocha/lib/cli/options')
  6. const unparse = require('yargs-unparser')
  7. const { aliases } = require('mocha/lib/cli/run-option-metadata')
  8.  
  9. const mochaPath = require.resolve('mocha/bin/_mocha')
  10.  
  11. const glob = promisify(originalGlob)
  12.  
  13. const getMochaArgs = path => {
  14.   if (!Array.isArray(path)) {
  15.     path = [path]
  16.   }
  17.   const mochaArgs = loadOptions([...path, '--timeout', '30000'])
  18.   return [].concat(mochaPath, unparse(mochaArgs, { alias: aliases }))
  19. }
  20.  
  21. const children = []
  22.  
  23. const runTests = testFile =>
  24.   new Promise((resolve, reject) => {
  25.     const child = spawn(process.execPath, getMochaArgs(testFile), {
  26.       env: {
  27.         NODE_ENV: 'test',
  28.       },
  29.     })
  30.  
  31.     children.push(child)
  32.  
  33.     child.stdout.on('data', data => {
  34.       const line = data.toString().trim()
  35.       if (line) {
  36.         console.log(`${testFile}: ${line}`)
  37.       }
  38.     })
  39.  
  40.     child.stderr.on('data', data => {
  41.       const line = data.toString().trim()
  42.       if (line) {
  43.         console.error(`${testFile}: ${line}`)
  44.       }
  45.     })
  46.  
  47.     child.on('exit', (code, signal) => {
  48.       const index = children.indexOf(child)
  49.       if (index !== -1) {
  50.         children.splice(index, 1)
  51.       }
  52.       if (code || signal) {
  53.         reject(new Error(`something bad happend ${code || signal}`))
  54.       } else {
  55.         resolve()
  56.       }
  57.     })
  58.   })
  59.  
  60. glob('tests/**/*.spec.js')
  61.   .then(testFiles => {
  62.     return Promise.all(testFiles.map(runTests))
  63.   })
  64.   .then(() => {
  65.     console.log('all tests pass')
  66.     process.exit(0)
  67.   })
  68.   .catch(err => {
  69.     console.error('some tests failed', err)
  70.     process.exit(1)
  71.   })
  72.  
  73. // terminate children.
  74. process.on('SIGINT', () => {
  75.   children.forEach(child => {
  76.     child.kill('SIGINT') // calls runner.abort()
  77.     child.kill('SIGTERM') // if that didn't work, we're probably in an infinite loop, so make it die.
  78.   })
  79. })
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top