refactor(website): vi.mock imports (#16885)
Some checks are pending
Argos CI Screenshots / take screenshots (push) Waiting to run
Publish codecov report from main branch / Run tests and push coverage result (push) Waiting to run
e2e-kubernetes-tests-main / Run All E2E tests (push) Waiting to run
e2e-tests-main / Run All E2E tests (push) Waiting to run
e2e-tests-main / windows-11-arm update e2e tests - custom-extensions (push) Waiting to run
e2e-tests-main / windows-2025 update e2e tests - custom-extensions (push) Waiting to run
e2e-tests-main / windows-11-arm update e2e tests - vanilla (push) Waiting to run
e2e-tests-main / windows-2025 update e2e tests - vanilla (push) Waiting to run
e2e-tests-main / macos-15-intel update e2e tests (push) Waiting to run
e2e-tests-main / macos-26 update e2e tests (push) Waiting to run
Managed configuration tests / Managed configuration tests - macos-latest (push) Waiting to run
Managed configuration tests / Managed configuration tests - ubuntu-latest (push) Waiting to run
Managed configuration tests / Managed configuration tests - windows-2025 (push) Waiting to run
next build / Build / windows-2025 (push) Blocked by required conditions
next build / Release (push) Blocked by required conditions
next build / Tagging (push) Waiting to run
next build / Build / macos-15 (push) Blocked by required conditions
next build / Build / ubuntu-24.04 (push) Blocked by required conditions
Publish NPM packages to npmjs.com using OIDC / Prepare version info (push) Waiting to run
Publish NPM packages to npmjs.com using OIDC / Publish to npm (push) Blocked by required conditions
Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
Publish Website / Build and deploy website (push) Waiting to run

chore(website): prefer-import-in-mock

Signed-off-by: axel7083 <42176370+axel7083@users.noreply.github.com>
This commit is contained in:
axel7083 2026-04-20 10:43:58 +02:00 committed by GitHub
parent 5c232ebeba
commit c9c493eca2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 18 additions and 31 deletions

View file

@ -16,29 +16,15 @@
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import { existsSync } from 'node:fs';
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { beforeEach, expect, test, vi } from 'vitest';
import { createNotesFiles } from './release-notes-parser';
const mocks = vi.hoisted(() => ({
existsSyncMock: vi.fn(),
mkdirMock: vi.fn(),
readFileMock: vi.fn(),
writeFileMock: vi.fn(),
}));
vi.mock('node:fs', () => {
return {
default: {
existsSync: mocks.existsSyncMock,
promises: {
mkdir: mocks.mkdirMock,
readFile: mocks.readFileMock,
writeFile: mocks.writeFileMock,
},
},
};
});
vi.mock(import('node:fs/promises'));
vi.mock(import('node:fs'));
const notesInfo =
'--- title: test1\nslug: podman-desktop-release-test1\nimage: /img/blog/podman-desktop-release-test1/test1.png\n---';
@ -71,23 +57,23 @@ const params = {
beforeEach(() => {
vi.resetAllMocks();
mocks.readFileMock.mockReturnValue(notesInfo + notesText + notesPoints);
mocks.existsSyncMock.mockReturnValue(true);
vi.mocked(readFile).mockResolvedValue(notesInfo + notesText + notesPoints);
vi.mocked(existsSync).mockReturnValue(true);
defaultParseFrontMatterMock.mockResolvedValue(defaultParseFrontMatterResultMock);
});
test('create release-notes directory when it does not exist', async () => {
mocks.existsSyncMock.mockReturnValue(false);
vi.mocked(existsSync).mockReturnValue(false);
await createNotesFiles(params);
expect(mocks.mkdirMock).toHaveBeenCalled();
expect(mkdir).toHaveBeenCalled();
});
test('Do not create release-nnotes directory when it exists', async () => {
await createNotesFiles(params);
expect(mocks.mkdirMock).not.toHaveBeenCalled();
expect(mkdir).not.toHaveBeenCalled();
});
test('parse provided release notes as expected', async () => {
await createNotesFiles(params);
expect(mocks.writeFileMock).toBeCalledWith('./static/release-notes/1.0.json', JSON.stringify(jsonResult));
expect(writeFile).toBeCalledWith('./static/release-notes/1.0.json', JSON.stringify(jsonResult));
});

View file

@ -16,7 +16,8 @@
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import fs from 'node:fs';
import { existsSync } from 'node:fs';
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import type {
DefaultParseFrontMatter,
@ -41,7 +42,7 @@ export async function createNotesFiles(
const version = versionMatch ? versionMatch[0] : '';
if (version) {
const folderName = './static/release-notes';
const fileContent = await fs.promises.readFile(params.filePath, { encoding: 'utf-8' });
const fileContent = await readFile(params.filePath, { encoding: 'utf-8' });
const resultText = fileContent.split('---', 3);
// get release image url
@ -65,15 +66,15 @@ export async function createNotesFiles(
const jsonInput = { image: imageUrl, blog: blogUrl, title: titleText, summary: summaryText };
if (!fs.existsSync(folderName)) {
if (!existsSync(folderName)) {
try {
await fs.promises.mkdir(folderName);
await mkdir(folderName);
} catch (error) {
// directory already exists
}
}
await fs.promises.writeFile(`${folderName}/${version}.json`, JSON.stringify(jsonInput));
await writeFile(`${folderName}/${version}.json`, JSON.stringify(jsonInput));
}
}
return result;

View file

@ -5,7 +5,7 @@ import { beforeEach, describe, expect, test, vi } from 'vitest';
import { extractNavigationFromSidebar, generateJsonOverviewFile } from './sidebar-content-parser';
// Mock the writeFile function
vi.mock('node:fs/promises', () => ({
vi.mock(import('node:fs/promises'), () => ({
writeFile: vi.fn(),
}));