Skip to content

Commit 95baaa5

Browse files
authored
Merge pull request #6 from scraggo/shuffle-order
Shuffle order
2 parents 44b894a + 051db8e commit 95baaa5

File tree

10 files changed

+68
-11
lines changed

10 files changed

+68
-11
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": ["airbnb-base", "plugin:prettier/recommended"],
33
"plugins": ["prettier"],
4+
"root": true,
45
"rules": {
56
"import/prefer-default-export": 0,
67
"prettier/prettier": "error"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ Found a typo? Want to add details or make a correction? This repo is open-source
506506
Articles:
507507

508508
- [Node.js & JavaScript Testing Best Practices (2020) - Medium](https://medium.com/@me_37286/yoni-goldberg-javascript-nodejs-testing-best-practices-2b98924c9347)
509+
- [An Overview of JavaScript Testing in 2020 - Medium](https://medium.com/welldone-software/an-overview-of-javascript-testing-7ce7298b9870)
509510
- <https://npmcompare.com/compare/ava,jest,mocha,mocha-parallel-tests>
510511
- <https://www.slant.co/versus/12696/12697/~mocha_vs_jest>
511512
- <https://stackshare.io/stackups/ava-vs-mocha>
@@ -524,7 +525,7 @@ More on Jest:
524525

525526
More on AVA:
526527

527-
- <https://github.com/avajs/awesome-ava#readme>
528+
- <https://github.com/avajs/awesome-ava>
528529
- <http://zpalexander.com/migrating-from-mocha-to-ava/>
529530
- [AVA, low-config testing for JavaScript - hello JS](https://blog.hellojs.org/ava-low-config-testing-for-javascript-71bd2d958745)
530531
- [Ava Test Runner - A Fresh Take On JavaScript Testing and Growing an Open-Source Project - Sessions by Pusher](https://pusher.com/sessions/meetup/the-js-roundabout/ava-test-runner-a-fresh-take-on-javascript-testing-and-growing-an-open-source-project)

docs/test-runner.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ 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+
27+
![speed-test-results.png](../images/speed-test-results.png)
28+
2529
## Running the tests
2630

2731
`npm install` to install all the packages.

images/speed-test-results.png

165 KB
Loading

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: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
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 DOTS = '. '.repeat(16);
8+
const testRunners = shuffle(TEST_RUNNERS);
69

710
// UTILS
811

@@ -59,6 +62,10 @@ const runScript = scriptObj => {
5962
return Date.now() - start;
6063
};
6164

65+
const logTestTitle = name => {
66+
console.log('\n\n', DOTS, '\n running tests for', name, '\n', DOTS, '\n\n');
67+
};
68+
6269
// sort, format, and log test results
6370
const logResults = resultsArr => {
6471
resultsArr
@@ -96,16 +103,16 @@ const main = () => {
96103
for (const name of testRunners) {
97104
const { run } = testData[name];
98105

99-
console.log('running tests for', name, '\n. . . . . . . . ');
106+
logTestTitle(name);
100107

101108
testData[name].executionTime = run();
102109
testResults.push(testData[name]);
103110
}
104111

105112
console.log(`-*-*-*-*-*-*-*-*-*-*-
106113
RESULTS
107-
-*-*-*-*-*-*-*-*-*-*-`);
108-
114+
-*-*-*-*-*-*-*-*-*-*-\n`);
115+
console.log('order:', testRunners, '\n');
109116
logResults(testResults);
110117
} catch (error) {
111118
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/.eslintrc.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"rules": {
3+
"func-names": 0
4+
},
5+
"globals": {
6+
"expect": true
7+
}
8+
}

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)