-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jest-circus runs children in shuffled order #12922
Changes from 6 commits
e43ca99
bbb1cf3
4c1d07c
99ac4cd
882f920
4df23a9
dc6e64f
3d09cfb
f8860e2
0bec19f
3a1d712
95590dc
14349bd
549988d
4ae8ff2
967b32e
561146c
2f95b65
50bada0
b78fdc2
3f33508
d84caa1
b5c34fe
b9c5139
1b412b4
c45244c
e1e73a0
2152031
6bb9449
164d6fa
79401d1
b6c4da4
19cf2b5
ca3a582
2007d47
9361b80
4d61ba2
a087ce6
7b4bd2b
b175652
54cc38c
974bc5b
b4e448f
816b8e6
4ba33d4
7b40ca5
ebf01d5
9172451
0797346
9c0ff4e
604499d
001d150
30b3452
8661cad
81d526b
825fdd9
7512738
888f565
a6f6982
57e9836
b844e1b
bbc70a1
d4509cb
6ce659d
7bdad9f
08ed42c
5fd2e2a
2bffe52
f5b919c
2688290
ec85fb8
94c0740
e212ed9
5946e84
ebe2485
c5145a6
4705449
af913e2
a6b4d06
e4f0b24
c4baaaa
87547f3
8bfa4c8
0202230
3a0a4f1
43d6f8b
7bd3fe1
cff3995
7c8bfc1
c3c6516
d57bf1c
1137ce1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,4 @@ export {default as pluralize} from './pluralize'; | |
export {default as formatTime} from './formatTime'; | ||
export {default as tryRealpath} from './tryRealpath'; | ||
export {default as requireOrImportModule} from './requireOrImportModule'; | ||
export {default as shuffleArray, rngBuilder} from './shuffleArray'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we move this into |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,38 @@ | ||||||
// Mulberry 32 taken from | ||||||
SimenB marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
// https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript | ||||||
export function mulberry32(seed: number): {next: () => number} { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SimenB instead of coding a dedicated random generator within jest, we may leverage existing libraries such as pure-rand (the library backing fast-check) or any other one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It comes with a pretty optimized and random rng called xoroshiro (the exact same ring as math random of V8 but in a seeded version) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds reasonable 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just seen the head of the PR leveraged prando. But it has far less traction than pure-rand 🤔 (pure-rand downloads being mostly due to fast-check) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tbh I found the first rng package that seemed to fit the needs. It won't be hard to swap to pure-rand. I'll make the change after I create a new PR just exposing the seed argument. |
||||||
let state = seed; | ||||||
|
||||||
function next(): number { | ||||||
state |= 0; | ||||||
state = (state + 0x6d2b79f5) | 0; | ||||||
let t = Math.imul(state ^ (state >>> 15), state | 1); | ||||||
t ^= t + Math.imul(t ^ (t >>> 7), t | 61); | ||||||
return ((t ^ (t >>> 14)) >>> 0) / 4294967296; | ||||||
} | ||||||
|
||||||
return {next}; | ||||||
} | ||||||
|
||||||
export const rngBuilder = mulberry32; | ||||||
|
||||||
// Fisher-Yates shuffle | ||||||
// This is performed in-place | ||||||
export default function shuffleArray<T>( | ||||||
array: Array<T>, | ||||||
random: () => number = Math.random, | ||||||
): Array<T> { | ||||||
const length = array == null ? 0 : array.length; | ||||||
if (!length) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return []; | ||||||
} | ||||||
let index = -1; | ||||||
const lastIndex = length - 1; | ||||||
while (++index < length) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be a |
||||||
const rand = index + Math.floor(random() * (lastIndex - index + 1)); | ||||||
const value = array[index]; | ||||||
array[index] = array[rand]; | ||||||
array[rand] = value; | ||||||
} | ||||||
return array; | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should always set
seet
even ifrandomize
isn't passed, right? Otherwise the types lie