aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/to-migrate.ts
blob: fd42a5f126ef4424c09fa073aca20fa19fa08794 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { expect } from '@jest/globals';
import type {
  Migration,
  MigrationConstructor,
} from '../lib/config/migrations/types';
import type { RenovateConfig } from '../lib/config/types';
import { MigrationsService } from './../lib/config/migrations/migrations-service';

declare global {
  // eslint-disable-next-line @typescript-eslint/no-namespace
  namespace jest {
    interface Matchers<R> {
      toMigrate(
        originalConfig: RenovateConfig,
        expectedConfig: RenovateConfig,
        isMigrated?: boolean,
      ): R;
    }
  }
}

expect.extend({
  toMigrate(
    CustomMigration: MigrationConstructor,
    originalConfig: RenovateConfig,
    expectedConfig: RenovateConfig,
    isMigrated: boolean = true,
  ) {
    class CustomMigrationsService extends MigrationsService {
      public static override getMigrations(
        original: RenovateConfig,
        migrated: RenovateConfig,
      ): ReadonlyArray<Migration> {
        return [new CustomMigration(original, migrated)];
      }
    }

    const migratedConfig = CustomMigrationsService.run(originalConfig);

    if (
      MigrationsService.isMigrated(migratedConfig, originalConfig) !==
      isMigrated
    ) {
      return {
        message: (): string => `isMigrated should be ${isMigrated}`,
        pass: false,
      };
    }

    if (!this.equals(migratedConfig, expectedConfig)) {
      return {
        message: (): string =>
          `Migration failed\n\nReceived config:\n${JSON.stringify(
            migratedConfig,
          )}\n\nExpected config:\n${JSON.stringify(expectedConfig)}`,
        pass: false,
      };
    }

    return {
      message: (): string => 'Migration passed successfully',
      pass: true,
    };
  },
});