Commit 7b933f67 authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Optimize test setup (#8644)

* Tweak test set-up

* Update azure config

* Add cleaning test files and windows handling

* Limit windows test to one at a time

* Tweak test settings

* Update tests splitting

* remove custom concurrency for azure

* Test Azure var

* Update Azure config

* bump

* Simplify cleaning up
parent 4c6c9cc8
......@@ -46,20 +46,10 @@ commands:
- run:
name: Run All Tests
command: |
yarn testall $(
node run-tests.js $(
circleci tests glob "test/**/*.test.*" | \
circleci tests split --split-by=timings --timings-type=classname
) || (
yarn git-clean
yarn git-reset
yarn testall $(
circleci tests glob "test/**/*.test.*" | \
circleci tests split --split-by=timings --timings-type=classname
)
circleci tests split
)
environment:
JEST_JUNIT_OUTPUT: 'reports/junit/js-test-results.xml'
JEST_JUNIT_CLASSNAME: '{filepath}'
test_browser:
parameters:
browser:
......
......@@ -4,9 +4,17 @@ pool:
strategy:
maxParallel: 10
matrix:
node-10:
node-10-1:
group: 1/2
node_version: ^10.10.0
node-8:
node-10-2:
group: 2/2
node_version: ^10.10.0
node-8-1:
group: 1/2
node_version: ^8.12.0
node-8-2:
group: 2/2
node_version: ^8.12.0
steps:
......@@ -20,5 +28,9 @@ steps:
displayName: 'Install dependencies'
- script: |
yarn test
yarn check
displayName: 'Yarn check'
- script: |
node run-tests.js -c 2 -g $(group)
displayName: 'Run tests'
......@@ -70,6 +70,7 @@
"@zeit/next-typescript": "1.1.2-canary.0",
"abort-controller": "3.0.0",
"amphtml-validator": "1.0.23",
"async-sema": "3.0.1",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "10.0.1",
"babel-jest": "24.8.0",
......
const path = require('path')
const _glob = require('glob')
const { promisify } = require('util')
const { Sema } = require('async-sema')
const { spawn, exec: execOrig } = require('child_process')
const glob = promisify(_glob)
const exec = promisify(execOrig)
const NUM_RETRIES = 2
const DEFAULT_CONCURRENCY = 3
;(async () => {
let concurrencyIdx = process.argv.indexOf('-c')
const concurrency =
parseInt(process.argv[concurrencyIdx + 1], 10) || DEFAULT_CONCURRENCY
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'))
if (tests.length === 0) {
tests = await glob('**/*.test.js', {
nodir: true,
cwd: path.join(__dirname, 'test')
})
}
let testNames = [
...new Set(
tests.map(f => {
let name = `${path
.dirname(f)
.replace(/\\/g, '/')
.replace(/\/test$/, '')}/`
if (!name.startsWith('test/')) name = `test/${name}`
return name
})
)
]
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++
testNames = testNames.splice(offset, numPerGroup)
}
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()
const runTest = (test = '') =>
new Promise((resolve, reject) => {
const child = spawn(
'node',
[jestPath, '--runInBand', '--forceExit', '--verbose', test],
{
stdio: 'inherit'
}
)
children.add(child)
child.on('exit', code => {
children.delete(child)
if (code) reject(new Error(`failed with code: ${code}`))
resolve()
})
})
await Promise.all(
testNames.map(async test => {
await sema.acquire()
let passed = false
for (let i = 0; i < NUM_RETRIES + 1; i++) {
try {
await runTest(test)
passed = true
break
} catch (err) {
if (i < NUM_RETRIES) {
try {
console.log('Cleaning test files for', test)
await exec(`git clean -fdx "${path.join(__dirname, test)}"`)
await exec(`git checkout "${path.join(__dirname, test)}"`)
} catch (err) {}
}
}
}
if (!passed) {
console.error(`${test} failed to pass within ${NUM_RETRIES} retries`)
children.forEach(child => child.kill())
process.exit(1)
}
sema.release()
})
)
})()
'use strict'
let globalTeardown
let globalTeardown = () => {}
const browser = global.bsBrowser
if (process.env.BROWSERSTACK) {
globalTeardown = () => global.browserStackLocal.killAllProcesses(() => {})
} else {
const chromedriver = require('chromedriver')
globalTeardown = () => chromedriver.stop()
}
module.exports = async () => {
......
......@@ -3089,6 +3089,11 @@ async-sema@3.0.0:
resolved "https://registry.yarnpkg.com/async-sema/-/async-sema-3.0.0.tgz#9e22d6783f0ab66a1cf330e21a905e39b3b3a975"
integrity sha512-zyCMBDl4m71feawrxYcVbHxv/UUkqm4nKJiLu3+l9lfiQha6jQ/9dxhrXLnzzBXVFqCTDwiUkZOz9XFbdEGQsg==
async-sema@3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/async-sema/-/async-sema-3.0.1.tgz#404225197ea43e3be91ac7d711fb7088f2469c79"
integrity sha512-fKT2riE8EHAvJEfLJXZiATQWqZttjx1+tfgnVshCDrH8vlw4YC8aECe0B8MU184g+aVRFVgmfxFlKZKaozSrNw==
async@^2.0.0, async@^2.1.5:
version "2.6.2"
resolved "https://registry.yarnpkg.com/async/-/async-2.6.2.tgz#18330ea7e6e313887f5d2f2a904bac6fe4dd5381"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment