Skip to content

Commit

Permalink
checkenvFile test migrated from jest to vitest (#2849)
Browse files Browse the repository at this point in the history
  • Loading branch information
abbi4code authored Dec 25, 2024
1 parent f60d17d commit 4132c7d
Showing 1 changed file with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
import fs from 'fs';
import { checkEnvFile } from './checkEnvFile';
import { vi } from 'vitest';

jest.mock('fs');
/**
* This file contains unit tests for the `checkEnvFile` function.
*
* The tests cover:
* - Behavior when the `.env` file is missing required keys and appending them appropriately.
* - Ensuring no changes are made when all keys are present in the `.env` file.
*
* These tests utilize Vitest for test execution and mock the `fs` module to simulate file operations.
*/

vi.mock('fs');

describe('checkEnvFile', () => {
beforeEach(() => {
jest.resetAllMocks();
vi.resetAllMocks();
});

it('should append missing keys to the .env file', () => {
const envContent = 'EXISTING_KEY=existing_value\n';
const envExampleContent =
'EXISTING_KEY=existing_value\nNEW_KEY=default_value\n';

jest
.spyOn(fs, 'readFileSync')
vi.spyOn(fs, 'readFileSync')
.mockReturnValueOnce(envContent)
.mockReturnValueOnce(envExampleContent)
.mockReturnValueOnce(envExampleContent);

jest.spyOn(fs, 'appendFileSync');
vi.spyOn(fs, 'appendFileSync');

checkEnvFile();

Expand All @@ -33,12 +43,11 @@ describe('checkEnvFile', () => {
const envContent = 'EXISTING_KEY=existing_value\n';
const envExampleContent = 'EXISTING_KEY=existing_value\n';

jest
.spyOn(fs, 'readFileSync')
vi.spyOn(fs, 'readFileSync')
.mockReturnValueOnce(envContent)
.mockReturnValueOnce(envExampleContent);

jest.spyOn(fs, 'appendFileSync');
vi.spyOn(fs, 'appendFileSync');

checkEnvFile();

Expand Down

0 comments on commit 4132c7d

Please sign in to comment.