Skip to content

Commit 4b0d238

Browse files
test: add random test sequencer (#184)
Signed-off-by: Jeffrey Tang <[email protected]>
1 parent da0e977 commit 4b0d238

File tree

5 files changed

+63
-3
lines changed

5 files changed

+63
-3
lines changed

DEV.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ Below we describe how you can set up local environment and contribute to `solo`.
2323
* In order to run E2E test, we need to set up cluster and install the chart.
2424
* Run `./test/e2e/setup-e2e.sh`
2525
* Run `npm run test-e2e`
26+
27+
* Tests are run in random order. The random seed value is shown as message such as:
28+
`Using timestamp seed 1711414247085 for random test order`
29+
30+
* If you like to rerun tests with the same seed, use environment variable `RANDOM_SEED=<integer_number>` with `npm run test-e2e` command.
31+
* Example: `RANDOM_SEED=20 npm run test-e2e`,
32+
and you should see an output like: `Using preset seed 20 for random test order`

jest.config.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const config = {
2121
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(mjs?)$',
2222
moduleFileExtensions: ['js', 'mjs'],
2323
verbose: true,
24-
reporters: ['default', 'jest-junit']
24+
reporters: ['default', 'jest-junit'],
25+
testSequencer: './test/testSequencer.mjs'
2526
}
2627

2728
export default config

package-lock.json

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
},
5151
"devDependencies": {
5252
"@jest/globals": "^29.7.0",
53+
"@jest/test-sequencer": "^29.7.0",
5354
"eslint": "^8.57.0",
5455
"eslint-config-standard": "^17.1.0",
5556
"eslint-plugin-headers": "^1.1.2",
@@ -64,7 +65,8 @@
6465
"remark-lint-list-item-indent": "^3.1.2",
6566
"remark-lint-unordered-list-marker-style": "^3.1.2",
6667
"remark-preset-lint-consistent": "^5.1.2",
67-
"remark-preset-lint-recommended": "^6.1.3"
68+
"remark-preset-lint-recommended": "^6.1.3",
69+
"seedrandom": "^3.0.5"
6870
},
6971
"repository": {
7072
"type": "git",

test/testSequencer.mjs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright (C) 2024 Hedera Hashgraph, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the ""License"");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an ""AS IS"" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
import Sequencer from '@jest/test-sequencer'
18+
import Seedrandom from 'seedrandom'
19+
import { NewLogger } from '../src/core/logging.mjs'
20+
import chalk from 'chalk'
21+
22+
export default class testSequencer extends Sequencer.default {
23+
logger = NewLogger('debug')
24+
sort (tests) {
25+
// get value of environment variable RANDOM_SEED if it is set
26+
// or use current timestamp
27+
let seed
28+
if (process.env.RANDOM_SEED) {
29+
seed = process.env.RANDOM_SEED
30+
this.logger.showUser(chalk.green(`Using preset seed ${seed} for random test order`))
31+
} else {
32+
seed = new Date().getTime().toString()
33+
this.logger.showUser(chalk.green(`Using timestamp seed ${seed} for random test order`))
34+
}
35+
36+
const randomNumGenerator = new Seedrandom(seed)
37+
const copyTests = Array.from(tests)
38+
39+
// use randomNumGenerator.int32() to generate random even or odd nuber
40+
return copyTests.sort((testA, testB) => (randomNumGenerator.int32() % 2 === 0 ? -1 : 1))
41+
}
42+
}

0 commit comments

Comments
 (0)