From a7d63f408853a0b83dbd5ba7b2a3e7d2101a8c2c Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Thu, 30 Nov 2023 21:04:54 +0200 Subject: [PATCH] Update tests to pass in Windows --- package.json | 3 ++- src/tests/markdowndb.spec.ts | 20 +++++++++++++------- src/tests/recursiveWalkDir.spec.ts | 8 +++++++- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index aa7d4b2..110c8a7 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "pretest": "npm run build && node ./dist/src/bin/index.js ./__mocks__/content", "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js", "build": "tsc --p tsconfig.lib.json", + "watch": "tsc --p tsconfig.lib.json --watch", "changeset": "changeset", "prepublishOnly": "npm run build", "release": "changeset publish" @@ -68,4 +69,4 @@ "ts-node": "^10.9.1", "typescript": "^5.0.4" } -} +} \ No newline at end of file diff --git a/src/tests/markdowndb.spec.ts b/src/tests/markdowndb.spec.ts index cc6375f..3c5f31b 100644 --- a/src/tests/markdowndb.spec.ts +++ b/src/tests/markdowndb.spec.ts @@ -2,6 +2,7 @@ import { MarkdownDB } from "../lib/markdowndb"; import { recursiveWalkDir } from "../lib/recursiveWalkDir"; import { File, MddbFile, Table } from "../lib/schema"; +import path from "path"; /** * @jest-environment node @@ -73,7 +74,7 @@ describe("MarkdownDB - default config", () => { `${pathToContentFixture}/blog/blog3.mdx`, `${pathToContentFixture}/blog/blog2.mdx`, `${pathToContentFixture}/blog0.mdx`, - ]; + ].map(normalizePathSeparator); expect(dbFilesPaths).toHaveLength(expectedPaths.length); dbFilesPaths.forEach((p) => { @@ -88,7 +89,7 @@ describe("MarkdownDB - default config", () => { const expectedPaths = [ `${pathToContentFixture}/blog/blog3.mdx`, `${pathToContentFixture}/blog/blog2.mdx`, - ]; + ].map(normalizePathSeparator); expect(dbFilesPaths).toHaveLength(expectedPaths.length); dbFilesPaths.forEach((p) => { @@ -102,7 +103,7 @@ describe("MarkdownDB - default config", () => { const expectedPaths = [ `${pathToContentFixture}/assets/datopian-logo.png`, - ]; + ].map(normalizePathSeparator); expect(dbFilesPaths).toHaveLength(expectedPaths.length); dbFilesPaths.forEach((p) => { @@ -119,7 +120,7 @@ describe("MarkdownDB - default config", () => { const expectedPaths = [ `${pathToContentFixture}/blog/blog1.mdx`, `${pathToContentFixture}/blog/blog2.mdx`, - ]; + ].map(normalizePathSeparator); expect(dbFilesPaths).toHaveLength(expectedPaths.length); dbFilesPaths.forEach((p) => { @@ -133,7 +134,7 @@ describe("MarkdownDB - default config", () => { }); const dbFilesPaths = dbFiles.map((f) => f.file_path); - const expectedPaths = [`${pathToContentFixture}/blog/blog1.mdx`]; + const expectedPaths = [`${pathToContentFixture}/blog/blog1.mdx`].map(normalizePathSeparator); expect(dbFilesPaths).toHaveLength(expectedPaths.length); dbFilesPaths.forEach((p) => { @@ -147,7 +148,7 @@ describe("MarkdownDB - default config", () => { }); const dbFilesPaths = dbFiles.map((f) => f.file_path); - const expectedPaths = [`${pathToContentFixture}/blog/blog2.mdx`]; + const expectedPaths = [`${pathToContentFixture}/blog/blog2.mdx`].map(normalizePathSeparator); expect(dbFilesPaths).toHaveLength(expectedPaths.length); dbFilesPaths.forEach((p) => { @@ -162,7 +163,7 @@ describe("MarkdownDB - default config", () => { extensions: ["md", "mdx"], }); const dbFilesPaths = dbFiles.map((f) => f.file_path); - const expectedPaths = [`${pathToContentFixture}/news/news1.mdx`]; + const expectedPaths = [`${pathToContentFixture}/news/news1.mdx`].map(normalizePathSeparator); expect(dbFilesPaths).toHaveLength(expectedPaths.length); dbFilesPaths.forEach((p) => { @@ -324,3 +325,8 @@ describe("MarkdownDB - custom config", () => { }); }); }); + +function normalizePathSeparator(filePath: string): string { + // Use path.sep to dynamically determine the path separator based on the OS + return filePath.split("/").join(path.sep); +} diff --git a/src/tests/recursiveWalkDir.spec.ts b/src/tests/recursiveWalkDir.spec.ts index eeebf37..c0a752c 100644 --- a/src/tests/recursiveWalkDir.spec.ts +++ b/src/tests/recursiveWalkDir.spec.ts @@ -1,4 +1,5 @@ import { recursiveWalkDir } from "../lib/recursiveWalkDir"; +import path from "path"; describe("recursiveWalkDir", () => { const contentFixturePath = "__mocks__/content/blog"; @@ -12,7 +13,7 @@ describe("recursiveWalkDir", () => { `${contentFixturePath}/blog3.mdx`, `${contentFixturePath}/blog2.mdx`, `${contentFixturePath}/blog1.mdx`, - ]; + ].map(normalizePathSeparator); test("should return all files in a directory", async () => { const paths = recursiveWalkDir(contentFixturePath); @@ -23,3 +24,8 @@ describe("recursiveWalkDir", () => { }); }); }); + +function normalizePathSeparator(filePath: string): string { + // Use path.sep to dynamically determine the path separator based on the OS + return filePath.split("/").join(path.sep); +}