Skip to content

Commit c7f2af7

Browse files
committed
Shuffle test runners
1 parent bfa7ef6 commit c7f2af7

File tree

6 files changed

+49
-9
lines changed

6 files changed

+49
-9
lines changed

docs/test-runner.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ This application is a test-runner that can:
2222

2323
The number and length of the authored tests simulate a "true" test run in a significantly sized enterprise codebase. Each test runner has a template that will run the _same exact_ test blocks and take the _same exact_ amount of time in each block. (This is done with a `setTimeout` with a time that increases with each iteration of the loop that generates the test block.)
2424

25+
To account for a bias in ordering, the scripts corresponding to each test runner are shuffled. This ensures that the suites for each test runner are never called in the same sequence.
26+
2527
## Running the tests
2628

2729
`npm install` to install all the packages.

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const { fullCircle } = require('./fullCircle');
22
const { range } = require('./range');
3+
const { shuffle } = require('./shuffle');
34

45
module.exports = {
56
fullCircle,
67
range,
8+
shuffle,
79
};

src/runAllTests.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
const execa = require('execa');
2+
const { shuffle } = require('./');
23

34
// CONSTANTS
45

5-
const testRunners = ['ava', 'jest', 'mocha', 'parallel'];
6+
const TEST_RUNNERS = ['ava', 'jest', 'mocha', 'parallel'];
7+
const testRunners = shuffle(TEST_RUNNERS);
68

79
// UTILS
810

@@ -104,8 +106,8 @@ const main = () => {
104106

105107
console.log(`-*-*-*-*-*-*-*-*-*-*-
106108
RESULTS
107-
-*-*-*-*-*-*-*-*-*-*-`);
108-
109+
-*-*-*-*-*-*-*-*-*-*-\n`);
110+
console.log('order:', testRunners, '\n');
109111
logResults(testResults);
110112
} catch (error) {
111113
console.error(error);

src/shuffle.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Fisher Yates Shuffle
3+
* @param {Array} arrInput
4+
* @param {Object} [options={}]
5+
* @param {boolean} [options.clone=false]
6+
* @returns {Array} shuffled array
7+
*/
8+
function shuffle(arrInput, { clone = false } = {}) {
9+
const arr = clone ? arrInput.slice() : arrInput;
10+
11+
for (let i = arr.length - 1; i > 0; i -= 1) {
12+
const j = Math.floor(Math.random() * (i + 1));
13+
[arr[i], arr[j]] = [arr[j], arr[i]];
14+
}
15+
16+
return arr;
17+
}
18+
19+
module.exports = { shuffle };

test/src/range.spec.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// const { expect } = require('chai');
2-
31
const { range } = require('../../src');
42

53
describe('range', function() {
@@ -17,9 +15,9 @@ describe('range', function() {
1715
expect(res).to.deep.equal([]);
1816
});
1917
it('throws error under bad conditions', function() {
20-
expect(() => range()).to.throw;
21-
expect(() => range(-1)).to.throw;
22-
expect(() => range(-2, -1)).to.throw;
23-
expect(() => range(2, 1)).to.throw;
18+
expect(() => range()).to.throw();
19+
expect(() => range(-1)).to.throw();
20+
expect(() => range(-2, -1)).to.throw();
21+
expect(() => range(2, 1)).to.throw();
2422
});
2523
});

test/src/shuffle.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { range, shuffle } = require('../../src');
2+
3+
describe('shuffle', function() {
4+
it('returns array of same length', function() {
5+
expect(shuffle([0, 1])).to.have.length(2);
6+
expect(shuffle([0, 1, 2])).to.have.length(3);
7+
expect(shuffle([0, 1, 2, 3])).to.have.length(4);
8+
});
9+
10+
it('eventually shuffles cloned array', function() {
11+
const arr = [0, 1];
12+
const NUM_OF_RUNS = 5;
13+
const runs = range(0, NUM_OF_RUNS).map(() => shuffle(arr, { clone: true }));
14+
15+
expect(runs).to.deep.include([1, 0]);
16+
});
17+
});

0 commit comments

Comments
 (0)