-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Schedule previously failing tests to run first
Fixes #1546. Co-authored-by: Mark Wubben <mark@novemberborn.net>
- Loading branch information
1 parent
2eebb60
commit d742672
Showing
10 changed files
with
146 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const writeFileAtomic = require('write-file-atomic'); | ||
const isCi = require('./is-ci'); | ||
|
||
const FILE_NAME_FAILING_TEST = 'failing-test.json'; | ||
|
||
module.exports.storeFailedTestFiles = (runStatus, cacheDir) => { | ||
if (isCi || !cacheDir) { | ||
return; | ||
} | ||
|
||
writeFileAtomic(path.join(cacheDir, FILE_NAME_FAILING_TEST), JSON.stringify(runStatus.getFailedTestFiles())); | ||
}; | ||
|
||
// Order test-files, so that files with failing tests come first | ||
module.exports.failingTestsFirst = (selectedFiles, cacheDir, cacheEnabled) => { | ||
if (isCi || cacheEnabled === false) { | ||
return selectedFiles; | ||
} | ||
|
||
const filePath = path.join(cacheDir, FILE_NAME_FAILING_TEST); | ||
let failedTestFiles; | ||
try { | ||
failedTestFiles = JSON.parse(fs.readFileSync(filePath)); | ||
} catch { | ||
return selectedFiles; | ||
} | ||
|
||
return [...selectedFiles].sort((f, s) => { | ||
if (failedTestFiles.some(tf => tf === f) && failedTestFiles.some(tf => tf === s)) { | ||
return 0; | ||
} | ||
|
||
if (failedTestFiles.some(tf => tf === f)) { | ||
return -1; | ||
} | ||
|
||
if (failedTestFiles.some(tf => tf === s)) { | ||
return 1; | ||
} | ||
|
||
return 0; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const test = require('ava'); | ||
|
||
test('pass', t => { | ||
t.log(Date.now()); | ||
t.pass(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const test = require('ava'); | ||
|
||
test('fail', t => { | ||
t.log(Date.now()); | ||
t.fail(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
files: [ | ||
"*.js" | ||
], | ||
cache: false | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"ava": { | ||
"files": [ | ||
"*.js" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const test = require('@ava/test'); | ||
const exec = require('../helpers/exec'); | ||
|
||
const options = { | ||
// The scheduler only works when not in CI, so trick it into believing it is | ||
// not in CI even when it's being tested by AVA's CI. | ||
env: {AVA_FORCE_CI: 'not-ci'} | ||
}; | ||
|
||
function getTimestamps(stats) { | ||
return {passed: BigInt(stats.getLogs(stats.passed[0])), failed: BigInt(stats.getLogs(stats.failed[0]))}; | ||
} | ||
|
||
test.serial('failing tests come first', async t => { | ||
try { | ||
await exec.fixture(['1pass.js', '2fail.js'], options); | ||
} catch {} | ||
|
||
try { | ||
await exec.fixture(['-t', '--concurrency=1', '1pass.js', '2fail.js'], options); | ||
} catch (error) { | ||
const timestamps = getTimestamps(error.stats); | ||
t.true(timestamps.failed < timestamps.passed); | ||
} | ||
}); | ||
|
||
test.serial('scheduler disabled when cache empty', async t => { | ||
await exec.fixture(['reset-cache'], options); // `ava reset-cache` resets the cache but does not run tests. | ||
try { | ||
await exec.fixture(['-t', '--concurrency=1', '1pass.js', '2fail.js'], options); | ||
} catch (error) { | ||
const timestamps = getTimestamps(error.stats); | ||
t.true(timestamps.passed < timestamps.failed); | ||
} | ||
}); | ||
|
||
test.serial('scheduler disabled when cache disabled', async t => { | ||
try { | ||
await exec.fixture(['1pass.js', '2fail.js'], options); | ||
} catch {} | ||
|
||
try { | ||
await exec.fixture(['-t', '--concurrency=1', '--config', 'disabled-cache.cjs', '1pass.js', '2fail.js'], options); | ||
} catch (error) { | ||
const timestamps = getTimestamps(error.stats); | ||
t.true(timestamps.passed < timestamps.failed); | ||
} | ||
}); | ||
|
||
test.serial('scheduler disabled in CI', async t => { | ||
try { | ||
await exec.fixture(['1pass.js', '2fail.js'], {env: {AVA_FORCE_CI: 'ci'}}); | ||
} catch {} | ||
|
||
try { | ||
await exec.fixture(['-t', '--concurrency=1', '--config', 'disabled-cache.cjs', '1pass.js', '2fail.js'], options); | ||
} catch (error) { | ||
const timestamps = getTimestamps(error.stats); | ||
t.true(timestamps.passed < timestamps.failed); | ||
} | ||
}); |