From f43f8e6331a30a19fb5fdebc51d1c5ae913d0346 Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Sat, 2 Sep 2023 20:19:53 +0100 Subject: [PATCH] Dev: ESLint run on largest files first [fix] --- jest-eslint.config.js | 1 + test/support/eslintSequencer.js | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 test/support/eslintSequencer.js diff --git a/jest-eslint.config.js b/jest-eslint.config.js index b5d4fed5..1f3c140a 100644 --- a/jest-eslint.config.js +++ b/jest-eslint.config.js @@ -10,6 +10,7 @@ module.exports = { testEnvironment: 'node', runner: 'jest-runner-eslint', + testSequencer: '/test/support/eslintSequencer.js', testMatch: ['/**/*.(js|cjs|mjs|jsx)'], // Jest by default uses a number of workers equal to number of CPU cores minus 1. // Github Actions runners provide 2 cores and running with 2 workers is faster than 1. diff --git a/test/support/eslintSequencer.js b/test/support/eslintSequencer.js new file mode 100644 index 00000000..49b484d4 --- /dev/null +++ b/test/support/eslintSequencer.js @@ -0,0 +1,26 @@ +/* -------------------- + * livepack + * Custom test sequencer for Jest ESLint + * ------------------*/ + +'use strict'; + +// Modules +const fs = require('fs'), + Sequencer = require('@jest/test-sequencer').default; + +// Exports + +// Custom sequencer to ensure the largest files are run first. +// `jest-runner-eslint` doesn't seem to sequence automatically. +// https://github.com/jest-community/jest-runner-eslint/issues/204 +// TODO: Remove this workaround once above issue is fixed. + +module.exports = class CustomSequencer extends Sequencer { + sort(tests) { + tests = super.sort(tests); + const testsWithFileSizes = tests.map(test => ({test, size: fs.statSync(test.path).size})); + testsWithFileSizes.sort((t1, t2) => (t1.size > t2.size ? -1 : t1.size < t2.size ? 1 : 0)); + return testsWithFileSizes.map(({test}) => test); + } +};