diff --git a/.yarn/install-state.gz b/.yarn/install-state.gz index 5c1ac69..c50f978 100644 Binary files a/.yarn/install-state.gz and b/.yarn/install-state.gz differ diff --git a/package.json b/package.json index 163d3d2..ce24e2c 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,9 @@ "name": "obsidian-testing-framework", "packageManager": "yarn@4.5.1", "dependencies": { + "@codemirror/language": "https://github.com/lishid/cm-language", + "@codemirror/state": "^6.0.1", + "@codemirror/view": "^6.0.1", "typescript": "^5.6.3" }, "version": "", @@ -17,6 +20,10 @@ "./utils": { "default": "./lib/utils.js", "types": "./lib/utils.d.ts" + }, + "./fixture": { + "default": "./lib/fixtures.js", + "types": "./lib/fixtures.d.ts" } }, "main": "./lib/index.js", @@ -35,5 +42,6 @@ }, "workspaces": [ "./*" - ] + ], + "type": "module" } diff --git a/src/index.ts b/src/index.ts index 5ca3023..b00d1d2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { App } from "obsidian"; import { test as base } from "@playwright/test"; -import { _electron as electron, ElectronApplication, Page } from "playwright"; +import { _electron as electron } from "playwright"; import { Fixtures } from "@playwright/test"; import { execSync } from "child_process"; import path from "path"; diff --git a/src/tester.ts b/src/tester.ts deleted file mode 100644 index f9c0a70..0000000 --- a/src/tester.ts +++ /dev/null @@ -1,114 +0,0 @@ -import { - _electron, - _electron as electron, - ElectronApplication, - Page, -} from "playwright"; -import {expect} from "vitest" -import { App, TFile, Vault } from "obsidian"; -import { execSync } from "child_process"; -import path from "path"; -import { EOL } from "os"; -type RawOptions = NonNullable[0]>; -export type TestOptions = Omit & { - vault: string; -}; -export class ObsidianTester { - #loadPromise: Promise; - - public electronApp: ElectronApplication | null = null; - public page: Page; - public app: App; - - constructor(options: Partial) { - const args = [...(options.args || [])]; - if (options.vault) - args.push(`obsidian://open?vault=${encodeURI(options.vault)}`); - this.#loadPromise = new Promise((res, rej) => { - electron - .launch({ - ...options, - args, - executablePath: ObsidianTester.getExe(), - }) - .then((v) => { - this.postInit(v).then(() => { - res(); - }); - }) - .catch((e) => rej(e)); - }); - } - public get vault(): Vault { - return this.app.vault - } - - public async doWithApp( - callback: (app: App) => Promise - ): Promise { - await this.#loadPromise; - return await callback(this.app); - } - - public async doWithVault( - callback: (vault: Vault) => Promise - ): Promise { - await this.#loadPromise; - return await callback(this.app.vault); - } - - public async assertFileEquals(path: string, expectedContent: string, cached: boolean = true) { - await this.#loadPromise; - const fileContent = await this.readFile(path, cached); - - expect(fileContent).toEqual(this.normalizeEOL(expectedContent)); - } - public async assertLineEquals(path: string, lineNumber: number, expectedContent: string, cached: boolean = true) { - await this.#loadPromise; - const fileContent = await this.readFile(path, cached); - - expect(fileContent.split("\n")[lineNumber]).toEqual(this.normalizeEOL(expectedContent)); - } - - public async assertLinesEqual(filePath: string, start: number, end: number, expected: string, cached: boolean = true) { - await this.#loadPromise; - const fileContent = await this.readFile(filePath, cached); - const lines = fileContent.split("\n").slice(start, end); - const expectedLines = this.normalizeEOL(expected).split("\n"); - expect(lines.every((l, i) => l == expectedLines[i])).toEqual(true); - } - - public getFile(file: string): TFile { - let f = this.app.vault.getFileByPath(file); - if(!f) { - throw new Error("File does not exist in vault."); - } - return f; - } - - normalizeEOL(str: string): string { - return str.split(/\r\n|\r|\n/).join("\n"); - } - - async readFile(path: string, cached: boolean = true): Promise { - await this.#loadPromise; - const file = this.getFile(path); - return this.normalizeEOL(await (cached ? this.app.vault.cachedRead(file) : this.app.vault.read(file))); - } - - private async postInit(electronApp: ElectronApplication) { - this.electronApp = electronApp; - this.page = await this.electronApp.firstWindow(); - this.app = await this.page.evaluate("window.app"); - } - - public static getExe(): string { - if (process.platform == "win32") { - return path.join(process.env.LOCALAPPDATA!, "Obsidian", "Obsidian.exe"); - } - if (process.platform == "darwin") { - throw new Error("use a non-toy operating system, dumbass"); - } - return execSync("command -v obsidian").toString(); - } -} diff --git a/src/util.ts b/src/util.ts index b3dc3c4..5b7b507 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,8 +1,5 @@ import { App, TFile } from "obsidian"; -import { PlaywrightTestArgs } from "playwright/test"; import { expect } from "vitest"; -import { ObsidianTestFixtures } from "./fixtures"; -import { test } from "src"; // type TestArgs = Parameters[2]>[0]; diff --git a/test-project/.gitignore b/test-project/.gitignore index e09a007..488a472 100644 --- a/test-project/.gitignore +++ b/test-project/.gitignore @@ -19,4 +19,8 @@ main.js data.json # Exclude macOS Finder (System Explorer) View States -.DS_Store +.DS_Store +/test-results/ +/playwright-report/ +/blob-report/ +/playwright/.cache/ diff --git a/test-project/tests/example.spec.ts b/test-project/e2e/example.spec.ts similarity index 100% rename from test-project/tests/example.spec.ts rename to test-project/e2e/example.spec.ts diff --git a/test-project/package.json b/test-project/package.json index 3d7a2b4..d7a20bb 100644 --- a/test-project/package.json +++ b/test-project/package.json @@ -1,29 +1,29 @@ { - "name": "obsidian-sample-plugin", - "version": "1.0.0", - "private": true, - "description": "This is a sample plugin for Obsidian (https://obsidian.md)", - "main": "main.js", - "scripts": { - "dev": "node esbuild.config.mjs", - "build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production", - "version": "node version-bump.mjs && git add manifest.json versions.json" - }, - "keywords": [], - "author": "", - "license": "MIT", - "dependencies": { - "obsidian-testing-framework": "workspace:^" - }, - "devDependencies": { - "@playwright/test": "^1.48.1", - "@types/node": "^16.11.6", - "@typescript-eslint/eslint-plugin": "5.29.0", - "@typescript-eslint/parser": "5.29.0", - "builtin-modules": "3.3.0", - "esbuild": "0.17.3", - "obsidian": "latest", - "tslib": "2.4.0", - "typescript": "4.7.4" - } + "name": "obsidian-sample-plugin", + "version": "1.0.0", + "private": true, + "description": "This is a sample plugin for Obsidian (https://obsidian.md)", + "main": "main.js", + "scripts": { + "dev": "node esbuild.config.mjs", + "build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production", + "version": "node version-bump.mjs && git add manifest.json versions.json" + }, + "keywords": [], + "author": "", + "license": "MIT", + "dependencies": { + "obsidian-testing-framework": "workspace:^" + }, + "devDependencies": { + "@playwright/test": "^1.48.1", + "@types/node": "^16.11.6", + "@typescript-eslint/eslint-plugin": "5.29.0", + "@typescript-eslint/parser": "5.29.0", + "builtin-modules": "3.3.0", + "esbuild": "0.17.3", + "obsidian": "latest", + "tslib": "2.4.0", + "typescript": "4.7.4" + } } diff --git a/test-project/playwright.config.ts b/test-project/playwright.config.ts index 7278860..61dc813 100644 --- a/test-project/playwright.config.ts +++ b/test-project/playwright.config.ts @@ -1,4 +1,5 @@ import { defineConfig, devices } from '@playwright/test'; +import { ObsidianTestingConfig } from 'obsidian-testing-framework'; /** * Read environment variables from file. @@ -11,9 +12,9 @@ import { defineConfig, devices } from '@playwright/test'; /** * See https://playwright.dev/docs/test-configuration. */ -export default defineConfig({ +export default defineConfig({ testDir: './tests', - + /* Run tests in files in parallel */ fullyParallel: true, /* Fail the build on CI if you accidentally left test.only in the source code. */ @@ -31,6 +32,7 @@ export default defineConfig({ /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ trace: 'on-first-retry', + }, /* Configure projects for major browsers */ diff --git a/tsconfig.json b/tsconfig.json index d1a5281..5c20e42 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,6 +5,8 @@ "moduleResolution": "node", "lib": [ "es2015", + "DOM", + "ES2018" ], "outDir": "lib", "baseUrl": ".", diff --git a/yarn.lock b/yarn.lock index c183cd4..bfc60f7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5,6 +5,38 @@ __metadata: version: 8 cacheKey: 10c0 +"@codemirror/language@https://github.com/lishid/cm-language": + version: 6.10.1 + resolution: "@codemirror/language@https://github.com/lishid/cm-language.git#commit=2644bfc27afda707a7e1f3aedaf3ca7120f63cd9" + dependencies: + "@codemirror/state": "npm:^6.0.0" + "@codemirror/view": "npm:^6.23.0" + "@lezer/common": "npm:^1.1.0" + "@lezer/highlight": "npm:^1.0.0" + "@lezer/lr": "npm:^1.0.0" + style-mod: "npm:^4.0.0" + checksum: 10c0/03f95ccf8b575f71b71ba7acd9061b3dffdef915936051323a7e7a19d65922ede673f7fdfd2fef51b2667d42df3d01ca0193cb5837694b0b8e19712ac1218c95 + languageName: node + linkType: hard + +"@codemirror/state@npm:^6.0.0, @codemirror/state@npm:^6.0.1, @codemirror/state@npm:^6.4.0": + version: 6.4.1 + resolution: "@codemirror/state@npm:6.4.1" + checksum: 10c0/cdab74d0ca4e262531a257ac419c9c44124f3ace8b0ca1262598a9218fbb6fd8f0afeb4b5ed2f64552a9573a0fc5d55481d4b9b05e9505ef729f9bd0f9469423 + languageName: node + linkType: hard + +"@codemirror/view@npm:^6.0.1, @codemirror/view@npm:^6.23.0": + version: 6.34.1 + resolution: "@codemirror/view@npm:6.34.1" + dependencies: + "@codemirror/state": "npm:^6.4.0" + style-mod: "npm:^4.1.0" + w3c-keyname: "npm:^2.2.4" + checksum: 10c0/cbb562ee7d6a443214e7f7c9822e207b506ef41ce31f331374aa9065931da8b40d6284081e21076579c2e6ea7c2d7b4e7ddf4b12058a667841428d29c5ae9a3e + languageName: node + linkType: hard + "@esbuild/aix-ppc64@npm:0.21.5": version: 0.21.5 resolution: "@esbuild/aix-ppc64@npm:0.21.5" @@ -341,6 +373,31 @@ __metadata: languageName: node linkType: hard +"@lezer/common@npm:^1.0.0, @lezer/common@npm:^1.1.0": + version: 1.2.3 + resolution: "@lezer/common@npm:1.2.3" + checksum: 10c0/fe9f8e111080ef94037a34ca2af1221c8d01c1763ba5ecf708a286185c76119509a5d19d924c8842172716716ddce22d7834394670c4a9432f0ba9f3b7c0f50d + languageName: node + linkType: hard + +"@lezer/highlight@npm:^1.0.0": + version: 1.2.1 + resolution: "@lezer/highlight@npm:1.2.1" + dependencies: + "@lezer/common": "npm:^1.0.0" + checksum: 10c0/51b4c08596a0dfeec6a7b7ed90a7f2743ab42e7e8ff8b89707fd042860e4e133dbd8243639fcaf077305ae6c303aa74e69794015eb16cb34741f5ac6721f283c + languageName: node + linkType: hard + +"@lezer/lr@npm:^1.0.0": + version: 1.4.2 + resolution: "@lezer/lr@npm:1.4.2" + dependencies: + "@lezer/common": "npm:^1.0.0" + checksum: 10c0/22bb5d0d4b33d0de5eb0706b7e5b5f2d20f570e112d9110009bd35b62ff10f2eb4eff8da4cf373dd4ddf5e06a304120b8f039add7ed9997c981c13945d5329cd + languageName: node + linkType: hard + "@nodelib/fs.scandir@npm:2.1.5": version: 2.1.5 resolution: "@nodelib/fs.scandir@npm:2.1.5" @@ -1792,6 +1849,9 @@ __metadata: version: 0.0.0-use.local resolution: "obsidian-testing-framework@workspace:." dependencies: + "@codemirror/language": "https://github.com/lishid/cm-language" + "@codemirror/state": "npm:^6.0.1" + "@codemirror/view": "npm:^6.0.1" "@playwright/test": "npm:^1.48.1" "@types/node": "npm:^22.7.8" obsidian: "npm:latest" @@ -2192,6 +2252,13 @@ __metadata: languageName: node linkType: hard +"style-mod@npm:^4.0.0, style-mod@npm:^4.1.0": + version: 4.1.2 + resolution: "style-mod@npm:4.1.2" + checksum: 10c0/ad4d870b3642b0e42ecc7be0e106dd14b7af11985e34fee8de34e5e38c3214bfc96fa7055acea86d75a3a59ddea3f6a8c6641001a66494d7df72d09685e3fadb + languageName: node + linkType: hard + "tar@npm:^6.1.11, tar@npm:^6.2.1": version: 6.2.1 resolution: "tar@npm:6.2.1" @@ -2446,6 +2513,13 @@ __metadata: languageName: node linkType: hard +"w3c-keyname@npm:^2.2.4": + version: 2.2.8 + resolution: "w3c-keyname@npm:2.2.8" + checksum: 10c0/37cf335c90efff31672ebb345577d681e2177f7ff9006a9ad47c68c5a9d265ba4a7b39d6c2599ceea639ca9315584ce4bd9c9fbf7a7217bfb7a599e71943c4c4 + languageName: node + linkType: hard + "which@npm:^2.0.1": version: 2.0.2 resolution: "which@npm:2.0.2"