run-tests.js 7.4 KB
Newer Older
J
JJ Kasper 已提交
1 2
const path = require('path')
const _glob = require('glob')
3
const fs = require('fs').promises
4
const fetch = require('node-fetch')
J
JJ Kasper 已提交
5 6 7 8 9 10 11
const { promisify } = require('util')
const { Sema } = require('async-sema')
const { spawn, exec: execOrig } = require('child_process')

const glob = promisify(_glob)
const exec = promisify(execOrig)

12
const timings = []
J
JJ Kasper 已提交
13
const NUM_RETRIES = 2
14
const DEFAULT_CONCURRENCY = 2
15 16
const RESULTS_EXT = `.results.json`
const isTestJob = !!process.env.NEXT_TEST_JOB
17
const TIMINGS_API = `https://next-timings.jjsweb.site/api/timings`
J
JJ Kasper 已提交
18 19 20 21 22 23

;(async () => {
  let concurrencyIdx = process.argv.indexOf('-c')
  const concurrency =
    parseInt(process.argv[concurrencyIdx + 1], 10) || DEFAULT_CONCURRENCY

24
  const outputTimings = process.argv.indexOf('--timings') !== -1
J
JJ Kasper 已提交
25 26 27 28 29
  const groupIdx = process.argv.indexOf('-g')
  const groupArg = groupIdx !== -1 && process.argv[groupIdx + 1]

  console.log('Running tests with concurrency:', concurrency)
  let tests = process.argv.filter(arg => arg.endsWith('.test.js'))
30
  let prevTimings
J
JJ Kasper 已提交
31 32 33 34

  if (tests.length === 0) {
    tests = await glob('**/*.test.js', {
      nodir: true,
35
      cwd: path.join(__dirname, 'test'),
J
JJ Kasper 已提交
36
    })
37

38
    if (outputTimings && groupArg) {
39
      console.log('Fetching previous timings data')
40 41
      try {
        const timingsRes = await fetch(TIMINGS_API)
42

43 44
        if (!timingsRes.ok) {
          throw new Error(`request status: ${timingsRes.status}`)
45
        }
46 47 48 49
        prevTimings = await timingsRes.json()
        console.log('Fetched previous timings data successfully')
      } catch (err) {
        console.log(`Failed to fetch timings data`, err)
50 51
      }
    }
J
JJ Kasper 已提交
52 53 54 55 56
  }

  let testNames = [
    ...new Set(
      tests.map(f => {
57
        let name = `${f.replace(/\\/g, '/').replace(/\/test$/, '')}`
J
JJ Kasper 已提交
58 59 60
        if (!name.startsWith('test/')) name = `test/${name}`
        return name
      })
61
    ),
J
JJ Kasper 已提交
62 63 64 65 66 67 68 69 70 71 72
  ]

  if (groupArg) {
    const groupParts = groupArg.split('/')
    const groupPos = parseInt(groupParts[0], 10)
    const groupTotal = parseInt(groupParts[1], 10)
    const numPerGroup = Math.ceil(testNames.length / groupTotal)
    let offset = groupPos === 1 ? 0 : (groupPos - 1) * numPerGroup - 1
    // if there's an odd number of suites give the first group the extra
    if (testNames.length % 2 !== 0 && groupPos !== 1) offset++

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    if (prevTimings) {
      const groups = [[]]
      const groupTimes = [0]

      for (const testName of testNames) {
        let smallestGroup = groupTimes[0]
        let smallestGroupIdx = 0

        // get the samllest group time to add current one to
        for (let i = 1; i < groupTotal; i++) {
          if (!groups[i]) {
            groups[i] = []
            groupTimes[i] = 0
          }

          const time = groupTimes[i]
          if (time < smallestGroup) {
            smallestGroup = time
            smallestGroupIdx = i
          }
        }
        groups[smallestGroupIdx].push(testName)
        groupTimes[smallestGroupIdx] += prevTimings[testName] || 1
      }

      const curGroupIdx = groupPos - 1
      testNames = groups[curGroupIdx]

      console.log(
        'Current group previous accumulated times:',
        Math.round(groupTimes[curGroupIdx]) + 's'
      )
    } else {
      testNames = testNames.splice(offset, numPerGroup)
    }
  }
109 110
  console.log('Running tests:', '\n', ...testNames.map(name => `${name}\n`))

J
JJ Kasper 已提交
111 112 113 114 115 116 117
  const sema = new Sema(concurrency, { capacity: testNames.length })
  const jestPath = path.join(
    path.dirname(require.resolve('jest-cli/package')),
    'bin/jest.js'
  )
  const children = new Set()

118
  const runTest = (test = '', usePolling) =>
J
JJ Kasper 已提交
119
    new Promise((resolve, reject) => {
120
      const start = new Date().getTime()
J
JJ Kasper 已提交
121 122
      const child = spawn(
        'node',
123 124 125 126 127 128 129 130 131 132
        [
          jestPath,
          '--runInBand',
          '--forceExit',
          '--verbose',
          ...(isTestJob
            ? ['--json', `--outputFile=${test}${RESULTS_EXT}`]
            : []),
          test,
        ],
J
JJ Kasper 已提交
133
        {
134
          stdio: 'inherit',
135 136 137 138
          env: {
            ...process.env,
            ...(usePolling
              ? {
139 140
                  // Events can be finicky in CI. This switches to a more
                  // reliable polling method.
141 142 143 144 145
                  CHOKIDAR_USEPOLLING: 'true',
                  CHOKIDAR_INTERVAL: 500,
                }
              : {}),
          },
J
JJ Kasper 已提交
146 147
        }
      )
148 149
      children.add(child)
      child.on('exit', code => {
J
JJ Kasper 已提交
150 151
        children.delete(child)
        if (code) reject(new Error(`failed with code: ${code}`))
152
        resolve(new Date().getTime() - start)
153
      })
J
JJ Kasper 已提交
154 155 156 157 158 159 160 161 162
    })

  await Promise.all(
    testNames.map(async test => {
      await sema.acquire()
      let passed = false

      for (let i = 0; i < NUM_RETRIES + 1; i++) {
        try {
163
          const time = await runTest(test, i > 0)
164 165 166 167
          timings.push({
            file: test,
            time,
          })
J
JJ Kasper 已提交
168 169 170 171 172
          passed = true
          break
        } catch (err) {
          if (i < NUM_RETRIES) {
            try {
173 174 175 176
              const testDir = path.dirname(path.join(__dirname, test))
              console.log('Cleaning test files at', testDir)
              await exec(`git clean -fdx "${testDir}"`)
              await exec(`git checkout "${testDir}"`)
J
JJ Kasper 已提交
177 178 179 180 181 182 183
            } catch (err) {}
          }
        }
      }
      if (!passed) {
        console.error(`${test} failed to pass within ${NUM_RETRIES} retries`)
        children.forEach(child => child.kill())
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

        if (isTestJob) {
          try {
            const testsOutput = await fs.readFile(
              `${test}${RESULTS_EXT}`,
              'utf8'
            )
            console.log(
              `--test output start--`,
              testsOutput,
              `--test output end--`
            )
          } catch (err) {
            console.log(`Failed to load test output`, err)
          }
        }
J
JJ Kasper 已提交
200 201 202 203 204
        process.exit(1)
      }
      sema.release()
    })
  )
205 206

  if (outputTimings) {
207 208
    const curTimings = {}
    // let junitData = `<testsuites name="jest tests">`
209 210 211 212 213 214 215 216 217
    /*
      <testsuite name="/__tests__/bar.test.js" tests="1" errors="0" failures="0" skipped="0" timestamp="2017-10-10T21:56:49" time="0.323">
        <testcase classname="bar-should be bar" name="bar-should be bar" time="0.004">
        </testcase>
      </testsuite>
    */

    for (const timing of timings) {
      const timeInSeconds = timing.time / 1000
218 219 220 221 222 223 224 225 226 227 228
      curTimings[timing.file] = timeInSeconds

      // junitData += `
      //   <testsuite name="${timing.file}" file="${
      //   timing.file
      // }" tests="1" errors="0" failures="0" skipped="0" timestamp="${new Date().toJSON()}" time="${timeInSeconds}">
      //     <testcase classname="tests suite should pass" name="${
      //       timing.file
      //     }" time="${timeInSeconds}"></testcase>
      //   </testsuite>
      // `
229
    }
230 231 232 233 234 235 236 237 238 239 240 241
    // junitData += `</testsuites>`
    // console.log('output timing data to junit.xml')

    if (prevTimings) {
      try {
        const timingsRes = await fetch(TIMINGS_API, {
          method: 'POST',
          headers: {
            'content-type': 'application/json',
          },
          body: JSON.stringify({ timings: curTimings }),
        })
242

243 244 245 246 247 248 249 250 251 252 253
        if (!timingsRes.ok) {
          throw new Error(`request status: ${timingsRes.status}`)
        }
        console.log(
          'Sent updated timings successfully',
          await timingsRes.json()
        )
      } catch (err) {
        console.log('Failed to update timings data', err)
      }
    }
254
  }
J
JJ Kasper 已提交
255
})()