|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +jest.mock('../../affected-packages', () => ({ |
| 11 | + ALWAYS_RUN_JEST_INTEGRATION_CONFIGS: ['always/jest.integration.config.js'], |
| 12 | + CRITICAL_FILES_JEST_INTEGRATION_TESTS: ['CRITICAL_INT'], |
| 13 | + CRITICAL_FILES_JEST_UNIT_TESTS: ['CRITICAL_UNIT'], |
| 14 | + getAffectedPackages: jest.fn(), |
| 15 | + listChangedFiles: jest.fn(), |
| 16 | + filterFilesByPackages: (files: string[], pkgs: Set<string>) => |
| 17 | + files.filter((f) => [...pkgs].some((pkg) => f.startsWith(pkg))), |
| 18 | + touchedCriticalFiles: (files: string[], critical: string[]) => |
| 19 | + files.some((f) => critical.includes(f)), |
| 20 | +})); |
| 21 | + |
| 22 | +jest.mock('./jest_configs', () => ({ SHARD_ANNOTATION_SEP: '||shard=' })); |
| 23 | + |
| 24 | +import type { SelectiveTestingContext } from './selective_testing'; |
| 25 | +import { |
| 26 | + filterJestIntegrationConfigsByAffected, |
| 27 | + filterJestUnitConfigsByAffected, |
| 28 | +} from './selective_testing'; |
| 29 | + |
| 30 | +const context = ( |
| 31 | + affected: string[], |
| 32 | + changed: string[] = ['irrelevant.ts'] |
| 33 | +): SelectiveTestingContext => ({ |
| 34 | + affectedPackages: new Set(affected), |
| 35 | + prChangedFiles: changed, |
| 36 | +}); |
| 37 | + |
| 38 | +describe('filterJestIntegrationConfigsByAffected', () => { |
| 39 | + it('drops unaffected configs but re-adds always-run configs', () => { |
| 40 | + const configs = [ |
| 41 | + 'always/jest.integration.config.js', |
| 42 | + 'other/jest.integration.config.js', |
| 43 | + 'affected/jest.integration.config.js', |
| 44 | + ]; |
| 45 | + |
| 46 | + const result = filterJestIntegrationConfigsByAffected(configs, context(['affected/'])); |
| 47 | + |
| 48 | + expect(result).toEqual( |
| 49 | + expect.arrayContaining([ |
| 50 | + 'always/jest.integration.config.js', |
| 51 | + 'affected/jest.integration.config.js', |
| 52 | + ]) |
| 53 | + ); |
| 54 | + expect(result).not.toContain('other/jest.integration.config.js'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('re-adds an always-run config even when no package is affected', () => { |
| 58 | + const configs = ['always/jest.integration.config.js', 'other/jest.integration.config.js']; |
| 59 | + |
| 60 | + const result = filterJestIntegrationConfigsByAffected(configs, context([])); |
| 61 | + |
| 62 | + expect(result).toEqual(['always/jest.integration.config.js']); |
| 63 | + }); |
| 64 | + |
| 65 | + it('restores every shard of an always-run config', () => { |
| 66 | + const configs = [ |
| 67 | + 'always/jest.integration.config.js||shard=1/2', |
| 68 | + 'always/jest.integration.config.js||shard=2/2', |
| 69 | + 'other/jest.integration.config.js', |
| 70 | + ]; |
| 71 | + |
| 72 | + const result = filterJestIntegrationConfigsByAffected(configs, context(['nothing/'])); |
| 73 | + |
| 74 | + expect(result).toEqual([ |
| 75 | + 'always/jest.integration.config.js||shard=1/2', |
| 76 | + 'always/jest.integration.config.js||shard=2/2', |
| 77 | + ]); |
| 78 | + }); |
| 79 | + |
| 80 | + it('returns all configs unchanged when a critical file changed', () => { |
| 81 | + const configs = ['other/jest.integration.config.js']; |
| 82 | + |
| 83 | + const result = filterJestIntegrationConfigsByAffected(configs, context([], ['CRITICAL_INT'])); |
| 84 | + |
| 85 | + expect(result).toEqual(configs); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +describe('filterJestUnitConfigsByAffected', () => { |
| 90 | + it('does not force always-run integration configs into unit runs', () => { |
| 91 | + const configs = ['always/jest.integration.config.js', 'affected/jest.config.js']; |
| 92 | + |
| 93 | + const result = filterJestUnitConfigsByAffected(configs, context(['affected/'])); |
| 94 | + |
| 95 | + expect(result).toEqual(['affected/jest.config.js']); |
| 96 | + }); |
| 97 | +}); |
0 commit comments