Files
directus/api/src/utils/validate-env.test.ts
Azri Kahar e899244ef3 Add unit tests to several API utility functions (#16662)
* add unit tests to several API util functions

* fix timezone tests to account for daylight saving

* add a note for future reference

* Update api/src/utils/get-date-formatted.test.ts

Co-authored-by: Brainslug <br41nslug@users.noreply.github.com>

* remove unnecessary note as it is not an issue

* fix getEnv mock in validate-env test

Co-authored-by: Brainslug <br41nslug@users.noreply.github.com>
Co-authored-by: Rijk van Zanten <rijkvanzanten@me.com>
2022-12-23 16:51:49 +00:00

42 lines
892 B
TypeScript

import { afterEach, beforeAll, expect, test, vi } from 'vitest';
import { validateEnv } from './validate-env';
import logger from '../logger';
vi.mock('../env', () => ({
getEnv: vi.fn().mockReturnValue({
PRESENT_TEST_VARIABLE: true,
}),
}));
vi.mock('../logger', () => ({
default: {
error: vi.fn(),
},
}));
vi.mock('process', () => ({
exit: vi.fn(),
}));
beforeAll(() => {
vi.spyOn(process, 'exit').mockImplementation(() => undefined as never);
});
afterEach(() => {
vi.clearAllMocks();
});
test('should not have any error when key is present', () => {
validateEnv(['PRESENT_TEST_VARIABLE']);
expect(logger.error).not.toHaveBeenCalled();
expect(process.exit).not.toHaveBeenCalled();
});
test('should have error when key is missing', () => {
validateEnv(['ABSENT_TEST_VARIABLE']);
expect(logger.error).toHaveBeenCalled();
expect(process.exit).toHaveBeenCalled();
});