From 9f8661d99c7dead6398a0b1dbe580e5cab9ed5a8 Mon Sep 17 00:00:00 2001 From: James Lynch Date: Mon, 18 Sep 2023 19:54:37 +0100 Subject: [PATCH] Replaces mocha tests with jest --- package.json | 5 ++-- src/moment-date-regex.ts | 8 +----- tests/doc.test.ts | 48 ++++++++++++++++----------------- tests/file.test.ts | 28 +++++++++---------- tests/moment-date-regex.test.ts | 24 ++++++++++------- tests/placeholder.test.ts | 8 +++--- tests/setings.test.ts | 12 ++++----- 7 files changed, 65 insertions(+), 68 deletions(-) diff --git a/package.json b/package.json index 9a6afab..c1697db 100644 --- a/package.json +++ b/package.json @@ -24,9 +24,11 @@ "cross-env": "^7.0.3", "ignore-styles": "^5.0.1", "jest": "^29.6.4", + "jest-environment-jsdom": "^29.7.0", "jsdom": "^16.6.0", "jsdom-global": "^3.0.2", "mocha": "^10.2.0", + "moment": "^2.29.4", "nyc": "^15.1.0", "obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master", "rollup": "^2.52.1", @@ -34,8 +36,5 @@ "ts-node": "^9.1.1", "tslib": "^2.3.0", "typescript": "^4.3.4" - }, - "dependencies": { - "moment": "^2.29.1" } } diff --git a/src/moment-date-regex.ts b/src/moment-date-regex.ts index 66f7d87..d3519a4 100644 --- a/src/moment-date-regex.ts +++ b/src/moment-date-regex.ts @@ -1,5 +1,5 @@ import { DEFAULT_DATE_FORMAT, DATE_REGEX } from './constants'; -import * as moment from 'moment'; +import moment from "moment"; export default class MomentDateRegex { replace(input: string): string { @@ -34,12 +34,6 @@ export default class MomentDateRegex { } getMoment(now: Date, dateFormat: string) { - if((window as any).moment) { - return (window as any) - .moment(now) - .format(dateFormat) - } else { return moment(now).format(dateFormat); - } } } \ No newline at end of file diff --git a/tests/doc.test.ts b/tests/doc.test.ts index a0833d2..c14b978 100644 --- a/tests/doc.test.ts +++ b/tests/doc.test.ts @@ -1,4 +1,4 @@ -import { assert } from 'chai'; +import {describe, expect, beforeAll} from '@jest/globals'; import NRDoc from '../src/doc'; import { NoteRefactorSettings } from '../src/settings'; import { promises as fs } from 'fs'; @@ -17,17 +17,17 @@ describe("Note content - Content Only", () => { it("First line content", () => { const noteContent = doc.noteContent(content[0], content.slice(1), true); - assert.equal(firstLine(noteContent), "Hi there! I'm a note in your vault."); + expect(firstLine(noteContent)).toBe("Hi there! I'm a note in your vault."); }); it("Last line content", () => { const noteContent = doc.noteContent(content[0], content.slice(1), true); - assert.equal(lastLine(noteContent), "- How to [[Working with multiple notes|open multiple files side by side]]"); + expect(lastLine(noteContent)).toBe("- How to [[Working with multiple notes|open multiple files side by side]]"); }); it("Character count", () => { const noteContent = doc.noteContent(content[0], content.slice(1), true); - assert.equal(noteContent.length, 746); + expect(noteContent.length).toBe(746); }); }); @@ -44,22 +44,22 @@ describe("Note content - Content Only - Normalize header levels", () => { it("First line content", () => { const noteContent = doc.noteContent(content[0], content.slice(1), true); - assert.equal(firstLine(noteContent), "# I have questions."); + expect(firstLine(noteContent)).toBe("# I have questions."); }); it("Header 3 content", () => { const noteContent = doc.noteContent(content[0], content.slice(1), true); - assert.equal(toArray(noteContent)[4], "## Header 3"); + expect(toArray(noteContent)[4]).toBe("## Header 3"); }); it("Last line content", () => { const noteContent = doc.noteContent(content[0], content.slice(1), true); - assert.equal(lastLine(noteContent), "This is for testing normalizing header levels."); + expect(lastLine(noteContent)).toBe("This is for testing normalizing header levels."); }); it("Character count", () => { const noteContent = doc.noteContent(content[0], content.slice(1), true); - assert.equal(noteContent.length, 232); + expect(noteContent.length).toBe(232); }); }); @@ -76,27 +76,27 @@ describe("Note content - First Line as File Name, exclude first line", () => { it("First Line text", () => { const noteContent = doc.noteContent(content[0], content.slice(1)); - assert.equal(firstLine(noteContent), "At the same time, I'm also just a Markdown file sitting on your hard disk. It's all in plain text, so you don't need to worry about losing me in case [[Obsidian]] disappears one day."); + expect(firstLine(noteContent)).toBe("At the same time, I'm also just a Markdown file sitting on your hard disk. It's all in plain text, so you don't need to worry about losing me in case [[Obsidian]] disappears one day."); }); it("Last line text", () => { const noteContent = doc.noteContent(content[0], content.slice(1)); - assert.equal(lastLine(noteContent), "- How to [[Working with multiple notes|open multiple files side by side]]"); + expect(lastLine(noteContent)).toBe("- How to [[Working with multiple notes|open multiple files side by side]]"); }); it("External links preserved", () => { const noteContent = doc.noteContent(content[0], content.slice(1)); - assert.equal(toArray(noteContent)[9], '- How to use [Markdown](https://www.markdownguide.org) to [[Format your notes]]'); + expect(toArray(noteContent)[9]).toBe('- How to use [Markdown](https://www.markdownguide.org) to [[Format your notes]]'); }); it("Embeds preserved", () => { const noteContent = doc.noteContent(content[0], content.slice(1)); - assert.equal(toArray(noteContent)[7], '- How to ![[Create notes|create new notes]].'); + expect(toArray(noteContent)[7]).toBe('- How to ![[Create notes|create new notes]].'); }); it("Character count", () => { const noteContent = doc.noteContent(content[0], content.slice(1)); - assert.equal(noteContent.length, 709); + expect(noteContent.length).toBe(709); }); }); @@ -116,27 +116,27 @@ describe("Note content - First Line as File Name, first line as heading", () => it("First Line text", () => { const noteContent = doc.noteContent(content[0], content.slice(1)); - assert.equal(firstLine(noteContent), "# Hi there! I'm a note in your vault."); + expect(firstLine(noteContent)).toBe("# Hi there! I'm a note in your vault."); }); it("Last line text", () => { const noteContent = doc.noteContent(content[0], content.slice(1)); - assert.equal(lastLine(noteContent), "- How to [[Working with multiple notes|open multiple files side by side]]"); + expect(lastLine(noteContent)).toBe("- How to [[Working with multiple notes|open multiple files side by side]]"); }); it("External links preserved", () => { const noteContent = doc.noteContent(content[0], content.slice(1)); - assert.equal(toArray(noteContent)[11], '- How to use [Markdown](https://www.markdownguide.org) to [[Format your notes]]'); + expect(toArray(noteContent)[11]).toBe('- How to use [Markdown](https://www.markdownguide.org) to [[Format your notes]]'); }); it("Embeds preserved", () => { const noteContent = doc.noteContent(content[0], content.slice(1)); - assert.equal(toArray(noteContent)[9], '- How to ![[Create notes|create new notes]].'); + expect(toArray(noteContent)[9]).toBe('- How to ![[Create notes|create new notes]].'); }); it("Character count", () => { const noteContent = doc.noteContent(content[0], content.slice(1)); - assert.equal(noteContent.length, 748); + expect(noteContent.length).toBe(748); }); }); @@ -154,32 +154,32 @@ describe("Note content - First Line as File Name, first line as heading (modifie it("First Line text", () => { const noteContent = doc.noteContent(content[0], content.slice(1)); - assert.equal(firstLine(noteContent), "# Quick Start"); + expect(firstLine(noteContent)).toBe("# Quick Start"); }); it("Last line text", () => { const noteContent = doc.noteContent(content[0], content.slice(1)); - assert.equal(lastLine(noteContent), "## Workflows"); + expect(lastLine(noteContent)).toBe("## Workflows"); }); it("Internal links preserved", () => { const noteContent = doc.noteContent(content[0], content.slice(1)); - assert.equal(toArray(noteContent)[9], '- [[Keyboard shortcuts]]'); + expect(toArray(noteContent)[9]).toBe('- [[Keyboard shortcuts]]'); }); it("External links preserved", () => { const noteContent = doc.noteContent(content[0], content.slice(1)); - assert.equal(toArray(noteContent)[18], 'If you are a [Catalyst supporter](https://obsidian.md/pricing), and want to turn on Insider Builds, see [[Insider builds]].'); + expect(toArray(noteContent)[18]).toBe('If you are a [Catalyst supporter](https://obsidian.md/pricing), and want to turn on Insider Builds, see [[Insider builds]].'); }); it("Embeds preserved", () => { const noteContent = doc.noteContent(content[0], content.slice(1)); - assert.equal(toArray(noteContent)[20], '![Obsidian.md](https://obsidian.md/images/screenshot.png)'); + expect(toArray(noteContent)[20]).toBe('![Obsidian.md](https://obsidian.md/images/screenshot.png)'); }); it("Character count", () => { const noteContent = doc.noteContent(content[0], content.slice(1)); - assert.equal(noteContent.length, 1105); + expect(noteContent.length).toBe(1105); }); }); diff --git a/tests/file.test.ts b/tests/file.test.ts index 25f0de8..b829b6a 100644 --- a/tests/file.test.ts +++ b/tests/file.test.ts @@ -1,4 +1,4 @@ -import { assert } from 'chai'; +import {describe, expect, beforeAll, afterAll} from '@jest/globals'; import { mockDate} from './mocks/date'; import NRFile from '../src/file'; import { NoteRefactorSettings } from '../src/settings'; @@ -19,37 +19,37 @@ describe("File Name Prefix", () => { it("Correct prefix for YYYYMMDDHHmm", () => { settings.fileNamePrefix = '{{date:YYYYMMDDHHmm}}-'; const prefix = file.fileNamePrefix(); - assert.equal(prefix, '202012251117-'); + expect(prefix).toBe('202012251117-'); }); it("Correct prefix for YYYYMMDDHHmmss", () => { settings.fileNamePrefix = '{{date:YYYYMMDDHHmmss}}-'; const prefix = file.fileNamePrefix(); - assert.equal(prefix, '20201225111752-'); + expect(prefix).toBe('20201225111752-'); }); it("Correct prefix for word dates YYYY-MMMM-Do_dddd", () => { settings.fileNamePrefix = '{{date:YYYY-MMMM-Do_dddd}}-'; const prefix = file.fileNamePrefix(); - assert.equal(prefix, '2020-December-25th_Friday-'); + expect(prefix).toBe('2020-December-25th_Friday-'); }); it("Correct prefix for with text and date", () => { settings.fileNamePrefix = 'ZK_{{date:YYYYMMDDHHmm}}-'; const prefix = file.fileNamePrefix(); - assert.equal(prefix, 'ZK_202012251117-'); + expect(prefix).toBe('ZK_202012251117-'); }); it("No date in prefix", () => { settings.fileNamePrefix = 'Inbox-Note-'; const prefix = file.fileNamePrefix(); - assert.equal(prefix, 'Inbox-Note-'); + expect(prefix).toBe('Inbox-Note-'); }); it("No prefix", () => { settings.fileNamePrefix = ''; const prefix = file.fileNamePrefix(); - assert.equal(prefix, ''); + expect(prefix).toBe(''); }); afterAll(() => { @@ -70,38 +70,38 @@ describe("File Name Sanitisation", () => { it("No sanitisation should be required", () => { fileName = '-- This should be allowed & (e.g. £$\'=)'; const sanitised = file.sanitisedFileName(fileName); - assert.equal(sanitised, fileName); + expect(sanitised).toBe(fileName); }); it("Expected internal link sanitisation", () => { fileName = '[[Internal Link]] to something'; const sanitised = file.sanitisedFileName(fileName); - assert.equal(sanitised, 'Internal Link to something'); + expect(sanitised).toBe('Internal Link to something'); }); it("Expected external link sanitisation", () => { fileName = '[Obsidian](https://en.wikipedia.org/wiki/Obsidian)'; const sanitised = file.sanitisedFileName(fileName); - assert.equal(sanitised, 'Obsidian(httpsen.wikipedia.orgwikiObsidian)'); + expect(sanitised).toBe('Obsidian(httpsen.wikipedia.orgwikiObsidian)'); }); it("Heading sanitisation", () => { fileName = '## A Heading Goes Here'; const sanitised = file.sanitisedFileName(fileName); - assert.equal(sanitised, 'A Heading Goes Here'); + expect(sanitised).toBe('A Heading Goes Here'); }); it("Illegal file path character sanitisation (*\"\/<>:|?", () => { fileName = '**This has**\\/ "a lot" of : |characters??|' ; const sanitised = file.sanitisedFileName(fileName); - assert.equal(sanitised, 'This has a lot of illegal characters'); + expect(sanitised).toBe('This has a lot of illegal characters'); }); it("Should include prefix", () => { fileName = '## A Heading Goes Here'; settings.fileNamePrefix = 'ZK-{{date:YYYY-MMM-DD-HHmm}}-'; const sanitised = file.sanitisedFileName(fileName); - assert.equal(sanitised, 'ZK-2020-Dec-25-1117-A Heading Goes Here'); + expect(sanitised).toBe('ZK-2020-Dec-25-1117-A Heading Goes Here'); }); it("Idempotent sanitisation with no duplicate prefixes", () => { @@ -110,7 +110,7 @@ describe("File Name Sanitisation", () => { let sanitised = file.sanitisedFileName(fileName); sanitised = file.sanitisedFileName(sanitised); sanitised = file.sanitisedFileName(sanitised); - assert.equal(sanitised, 'ZK-2020-Dec-25-1117-A Heading Goes Here'); + expect(sanitised).toBe('ZK-2020-Dec-25-1117-A Heading Goes Here'); }); diff --git a/tests/moment-date-regex.test.ts b/tests/moment-date-regex.test.ts index de0a614..0e34c4f 100644 --- a/tests/moment-date-regex.test.ts +++ b/tests/moment-date-regex.test.ts @@ -1,4 +1,8 @@ -import { assert } from 'chai'; +/** + * @jest-environment jsdom + */ +import {describe, expect, beforeAll, afterAll} from '@jest/globals'; +import moment from 'moment'; import { mockDate } from './mocks/date' import MomentDateRegex from '../src/moment-date-regex'; const date = new Date(2020, 9, 31, 14, 25, 15); @@ -15,42 +19,42 @@ describe("Date formatting", () => { const input = 'Zettels/{{date}}'; const expectedOuput = 'Zettels/202010311425'; - assert.equal(momentRegex.replace(input), expectedOuput); + expect(momentRegex.replace(input)).toBe(expectedOuput); }); it("YYYYMMDD format", () => { const input = 'Zettels/{{date:YYYYMMDD}}'; const expectedOuput = 'Zettels/20201031'; - assert.equal(momentRegex.replace(input), expectedOuput); + expect(momentRegex.replace(input)).toBe(expectedOuput); }); it("Multiple dates", () => { const input = 'Zettels/{{date:YYYY}}/{{date:MMM}}/{{date:DD_ddd}}'; const expectedOuput = 'Zettels/2020/Oct/31_Sat'; - assert.equal(momentRegex.replace(input), expectedOuput); + expect(momentRegex.replace(input)).toBe(expectedOuput); }); it("Date path prefixing", () => { const input = '{{date:YYYY}}/{{date:MM}}/My Notes'; const expectedOuput = '2020/10/My Notes'; - assert.equal(momentRegex.replace(input), expectedOuput); + expect(momentRegex.replace(input)).toBe(expectedOuput); }); it("Text between date targets", () => { const input = '{{date:YYYY}}/Zettels/{{date:MMMM}}'; const expectedOuput = '2020/Zettels/October'; - assert.equal(momentRegex.replace(input), expectedOuput); + expect(momentRegex.replace(input)).toBe(expectedOuput); }); it("Date file name prefixing", () => { const input = '{{date:YYYYMMDDHHmm}}-My New Note'; const expectedOuput = '202010311425-My New Note'; - assert.equal(momentRegex.replace(input), expectedOuput); + expect(momentRegex.replace(input)).toBe(expectedOuput); }); afterAll(() => { @@ -64,20 +68,20 @@ describe("Non-date input", () => { const input = 'Inbox/New'; const expectedOuput = 'Inbox/New'; - assert.equal(momentRegex.replace(input), expectedOuput); + expect(momentRegex.replace(input)).toBe(expectedOuput); }); it("Input with date format without date target", () => { const input = 'Inbox/YYYY'; const expectedOuput = 'Inbox/YYYY'; - assert.equal(momentRegex.replace(input), expectedOuput); + expect(momentRegex.replace(input)).toBe(expectedOuput); }); it("Input with date format and partial date target", () => { const input = 'Inbox/{{date:YYYY'; const expectedOuput = 'Inbox/{{date:YYYY'; - assert.equal(momentRegex.replace(input), expectedOuput); + expect(momentRegex.replace(input)).toBe(expectedOuput); }); }); \ No newline at end of file diff --git a/tests/placeholder.test.ts b/tests/placeholder.test.ts index 91f6c33..6e7e384 100644 --- a/tests/placeholder.test.ts +++ b/tests/placeholder.test.ts @@ -1,4 +1,4 @@ -import { assert } from 'chai'; +import {describe, expect, beforeAll} from '@jest/globals'; import { NotePlaceholders } from '../src/placeholder'; let placholders = new NotePlaceholders(); let input:string = ''; @@ -20,17 +20,17 @@ $\sqrt{2}$`; it("Should preserve double $$ (case 1)", () => { output = placholders.newNoteContent.replace(input, replacement); - assert.equal(lineAt(output, 1), "$$\frac{a}{b}$$"); + expect(lineAt(output, 1)).toBe("$$\frac{a}{b}$$"); }); it("Should preserve double $$ (case 2)", () => { output = placholders.newNoteContent.replace(input, replacement); - assert.equal(lineAt(output, 3), "$$a^2 + b^2 = c^2$$"); + expect(lineAt(output, 3)).toBe("$$a^2 + b^2 = c^2$$"); }); it("Should preserve single $", () => { output = placholders.newNoteContent.replace(input, replacement); - assert.equal(lineAt(output, 5), "$\sqrt{2}$"); + expect(lineAt(output, 5)).toBe("$\sqrt{2}$"); }); }); diff --git a/tests/setings.test.ts b/tests/setings.test.ts index b462891..7988bb3 100644 --- a/tests/setings.test.ts +++ b/tests/setings.test.ts @@ -1,26 +1,26 @@ -import { assert } from 'chai'; +import {describe, expect, beforeAll, afterAll} from '@jest/globals'; import { Location, NoteRefactorSettings } from '../src/settings'; const settings = new NoteRefactorSettings(); describe("Note Refactor Settings defaults", () => { it("New file location ", () => { - assert.equal(settings.newFileLocation, Location.VaultFolder); + expect(settings.newFileLocation).toBe(Location.VaultFolder); }); it("Custom folder ", () => { - assert.equal(settings.customFolder, ''); + expect(settings.customFolder).toBe(''); }); it("Exclude first line in note", () => { - assert.isFalse(settings.excludeFirstLineInNote); + expect(settings.excludeFirstLineInNote).toBeFalsy(); }); it("Include first line as note heading", () => { - assert.isFalse(settings.includeFirstLineAsNoteHeading); + expect(settings.includeFirstLineAsNoteHeading).toBeFalsy(); }); it("Heading format", () => { - assert.equal(settings.headingFormat, '#'); + expect(settings.headingFormat).toBe('#'); }); }); \ No newline at end of file