Skip to content

Commit

Permalink
chore: disable hash checking in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielcsapo committed Nov 27, 2024
1 parent 1f69769 commit a7313e8
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions src/server/utils/getIndexHash.test.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
import fs from "fs";
import klawSync from "klaw-sync";
import { PluginConfig } from "../../types";
import { getIndexHash } from "./getIndexHash";
import fs from 'fs';
import klawSync from 'klaw-sync';
import { PluginConfig } from '../../types';
import { getIndexHash } from './getIndexHash';

describe("getIndexHash", () => {
describe('getIndexHash', () => {
let mockConsoleWarn;

beforeEach(() => {
vi.mock("klaw-sync");
vi.mock("fs");
vi.mock('klaw-sync');
vi.mock('fs');

mockConsoleWarn = vi
.spyOn(console, "warn")
.spyOn(console, 'warn')
.mockImplementation(() => void 0);

vi.mock(
"../../../../package.json",
'../../../../package.json',
() => ({
version: "0.0.0",
version: '0.0.0',
}),
{
virtual: true,
}
},
);

(klawSync as vi.MockedFunction<typeof klawSync>).mockImplementation(
(root, options) => {
let files: string[] = [];
if (root === "/tmp/docs") {
files = ["/tmp/docs/a.md", "/tmp/docs/b.md", "/tmp/docs/b.png"];
if (root === '/tmp/docs') {
files = ['/tmp/docs/a.md', '/tmp/docs/b.md', '/tmp/docs/b.png'];
}

const items = files.map((path) => ({ path } as klawSync.Item));

return options?.filter ? items.filter(options.filter) : items;
}
},
);

(
fs.readFileSync as vi.MockedFunction<typeof fs.readFileSync>
).mockImplementation((filePath: fs.PathOrFileDescriptor) => {
if (typeof filePath === "string" && filePath.endsWith(".md")) {
if (typeof filePath === 'string' && filePath.endsWith('.md')) {
return Buffer.from(filePath);
}
throw new Error(`Unknown file: ${filePath}`);
Expand All @@ -49,8 +49,8 @@ describe("getIndexHash", () => {
(
fs.existsSync as vi.MockedFunction<typeof fs.existsSync>
).mockImplementation((filePath: fs.PathOrFileDescriptor) => {
if (typeof filePath === "string") {
return filePath.startsWith("/tmp/");
if (typeof filePath === 'string') {
return filePath.startsWith('/tmp/');
}

return false;
Expand All @@ -60,23 +60,24 @@ describe("getIndexHash", () => {
(filePath: fs.PathOrFileDescriptor) => {
return {
isDirectory: () => {
if (typeof filePath === "string") {
return !filePath.includes(".");
if (typeof filePath === 'string') {
return !filePath.includes('.');
}
},
} as fs.Stats;
}
},
);
});

test.each<[Partial<PluginConfig>, string | null, number]>([
[{ hashed: false }, 0],
[{ hashed: true, indexDocs: true, docsDir: ["/tmp/docs"] }, 0],
[{ hashed: true, indexBlog: true, blogDir: ["/tmp/blog"] }, 0],
[{ hashed: true, indexDocs: true, docsDir: ["/does-not-exist/docs"] }, 1],
[{ hashed: true, indexDocs: true, docsDir: ["/tmp/index.js"] }, 1],
[{ hashed: true, indexDocs: true, docsDir: ['/tmp/docs'] }, 0],
[{ hashed: true, indexBlog: true, blogDir: ['/tmp/blog'] }, 0],
[{ hashed: true, indexDocs: true, docsDir: ['/does-not-exist/docs'] }, 1],
[{ hashed: true, indexDocs: true, docsDir: ['/tmp/index.js'] }, 1],
])("getIndexHash(%j) should return '%s'", (config, warnCount) => {
expect(getIndexHash(config as PluginConfig)).toMatchSnapshot();
getIndexHash(config as PluginConfig);

expect(mockConsoleWarn).toBeCalledTimes(warnCount);
});
});

0 comments on commit a7313e8

Please sign in to comment.