/*! * @license * Copyright Squiz Australia Pty Ltd. All Rights Reserved. */ import * as runtimeImageConstants from '../../job/constants/RuntimeImage.constants'; import { DEPRECATED_RUNTIME_IMAGES, RECOMMENDED_RUNTIME_IMAGE, SUPPORTED_RUNTIME_IMAGES, } from '../../job/constants/RuntimeImage.constants'; import { ManifestValidationError } from '../errors/ManifestValidationError'; import { validateAndDefaultRuntimeImage } from './validateRuntimeImage'; describe('validateAndDefaultRuntimeImage', () => { afterEach(() => { jest.restoreAllMocks(); }); it('returns the recommended runtime image when image is undefined', () => { expect(validateAndDefaultRuntimeImage(undefined)).toEqual(RECOMMENDED_RUNTIME_IMAGE); }); it('returns the recommended runtime image when image is null', () => { expect(validateAndDefaultRuntimeImage(null)).toEqual(RECOMMENDED_RUNTIME_IMAGE); }); it.each(SUPPORTED_RUNTIME_IMAGES.filter((image) => !DEPRECATED_RUNTIME_IMAGES.includes(image)))( 'returns "%s" when it is supported and not deprecated', (supportedImage) => { expect(validateAndDefaultRuntimeImage(supportedImage)).toEqual(supportedImage); }, ); it('throws ManifestValidationError when the image is not supported', () => { expect(() => validateAndDefaultRuntimeImage('node:18')).toThrow(ManifestValidationError); expect(() => validateAndDefaultRuntimeImage('node:18')).toThrow(/Unsupported runtime image "node:18"/); }); it('throws ManifestValidationError when the image is deprecated', () => { jest.spyOn(runtimeImageConstants, 'isSupportedRuntimeImage').mockReturnValue(true); jest.spyOn(runtimeImageConstants, 'isDeprecatedRuntimeImage').mockReturnValue(true); expect(() => validateAndDefaultRuntimeImage('node:20')).toThrow(ManifestValidationError); expect(() => validateAndDefaultRuntimeImage('node:20')).toThrow(/deprecated/); }); });