From fab4c1a216474cd5fa347e6e5e885710410e0d72 Mon Sep 17 00:00:00 2001 From: Joseph Baylon Date: Thu, 1 Sep 2022 07:56:31 -0700 Subject: [PATCH] Detox/E2E: Add custom test sequencer (#6611) * Detox/E2E: Add custom test sequencer * Fix lint * Added explicit dev dependency on sequencer --- detox/e2e/config.js | 1 + detox/e2e/custom_sequencer.js | 31 +++++++++++++++++++++++++++++++ detox/package-lock.json | 1 + detox/package.json | 1 + 4 files changed, 34 insertions(+) create mode 100644 detox/e2e/custom_sequencer.js diff --git a/detox/e2e/config.js b/detox/e2e/config.js index bfb89b83d..b8d09a885 100644 --- a/detox/e2e/config.js +++ b/detox/e2e/config.js @@ -8,6 +8,7 @@ module.exports = { maxWorkers: 1, testEnvironment: './environment', testRunner: 'jest-circus/runner', + testSequencer: './custom_sequencer.js', testTimeout: 120000, testRegex: '\\.e2e\\.ts$', transform: { diff --git a/detox/e2e/custom_sequencer.js b/detox/e2e/custom_sequencer.js new file mode 100644 index 000000000..827166cb2 --- /dev/null +++ b/detox/e2e/custom_sequencer.js @@ -0,0 +1,31 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +const Sequencer = require('@jest/test-sequencer').default; + +class CustomSequencer extends Sequencer { + /** + * Select tests for shard requested via --shard=shardIndex/shardCount + * Sharding is applied before sorting + */ + shard(tests, {shardIndex, shardCount}) { + const shardSize = Math.ceil(tests.length / shardCount); + const shardStart = shardSize * (shardIndex - 1); + const shardEnd = shardSize * shardIndex; + + return [...tests].sort((a, b) => (a.path > b.path ? 1 : -1)).slice(shardStart, shardEnd); + } + + /** + * Sort test to determine order of execution + * Sorting is applied after sharding + */ + sort(tests) { + // Test structure information + // https://github.com/facebook/jest/blob/6b8b1404a1d9254e7d5d90a8934087a9c9899dab/packages/jest-runner/src/types.ts#L17-L21 + const copyTests = Array.from(tests); + return copyTests.sort((testA, testB) => (testA.path > testB.path ? 1 : -1)); + } +} + +module.exports = CustomSequencer; diff --git a/detox/package-lock.json b/detox/package-lock.json index 2fbd47bd6..af39188ae 100644 --- a/detox/package-lock.json +++ b/detox/package-lock.json @@ -10,6 +10,7 @@ "@babel/plugin-transform-modules-commonjs": "7.18.6", "@babel/plugin-transform-runtime": "7.18.10", "@babel/preset-env": "7.18.10", + "@jest/test-sequencer": "28.1.3", "@types/jest": "28.1.6", "@types/tough-cookie": "4.0.2", "@types/uuid": "8.3.4", diff --git a/detox/package.json b/detox/package.json index 9e81ee512..6e53f2569 100644 --- a/detox/package.json +++ b/detox/package.json @@ -8,6 +8,7 @@ "@babel/plugin-transform-modules-commonjs": "7.18.6", "@babel/plugin-transform-runtime": "7.18.10", "@babel/preset-env": "7.18.10", + "@jest/test-sequencer": "28.1.3", "@types/jest": "28.1.6", "@types/tough-cookie": "4.0.2", "@types/uuid": "8.3.4",