...
This commit is contained in:
parent
fc5a65e9f7
commit
8bd31c526f
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,2 @@
|
|||||||
node_modules
|
node_modules
|
||||||
lib
|
lib*
|
||||||
|
BIN
.yarn/install-state.gz
vendored
BIN
.yarn/install-state.gz
vendored
Binary file not shown.
47
package.json
47
package.json
@ -1,47 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "obsidian-testing-framework",
|
"name": "obsidian-testing-framework-parent",
|
||||||
|
"private": true,
|
||||||
"packageManager": "yarn@4.5.1",
|
"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": "",
|
|
||||||
"files": [
|
|
||||||
"./bin/*",
|
|
||||||
"./lib/*"
|
|
||||||
],
|
|
||||||
"exports": {
|
|
||||||
".": {
|
|
||||||
"default": "./lib/index.js",
|
|
||||||
"types": "./lib/index.d.ts"
|
|
||||||
},
|
|
||||||
"./utils": {
|
|
||||||
"default": "./lib/utils.js",
|
|
||||||
"types": "./lib/utils.d.ts"
|
|
||||||
},
|
|
||||||
"./fixture": {
|
|
||||||
"default": "./lib/fixtures.js",
|
|
||||||
"types": "./lib/fixtures.d.ts"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"main": "./lib/index.js",
|
|
||||||
"typings": "./lib/index.d.ts",
|
|
||||||
"scripts": {
|
|
||||||
"build": "tsc",
|
|
||||||
"lint": "tslint -c tslint.json src/**/*.ts",
|
|
||||||
"prepublish": "npm run build"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"@playwright/test": "^1.48.1",
|
|
||||||
"@types/node": "^22.7.8",
|
|
||||||
"obsidian": "latest",
|
|
||||||
"playwright": "^1.48.1",
|
|
||||||
"vitest": "^2.1.3"
|
|
||||||
},
|
|
||||||
"workspaces": [
|
"workspaces": [
|
||||||
"./*"
|
"./packages/*"
|
||||||
],
|
]
|
||||||
"type": "module"
|
|
||||||
}
|
}
|
||||||
|
39
packages/obsidian-testing-framework/package.json
Normal file
39
packages/obsidian-testing-framework/package.json
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
{
|
||||||
|
"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": "",
|
||||||
|
"exports": {
|
||||||
|
".": {
|
||||||
|
"types": "./lib/index.d.ts",
|
||||||
|
"default": "./lib/index.js"
|
||||||
|
},
|
||||||
|
"./utils": {
|
||||||
|
"types": "./lib/utils.d.ts",
|
||||||
|
"default": "./lib/utils.js"
|
||||||
|
},
|
||||||
|
"./fixture": {
|
||||||
|
"types": "./lib/fixtures.d.ts",
|
||||||
|
"default": "./lib/fixtures.js"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"main": "./lib/index.js",
|
||||||
|
"typings": "./lib/index.d.ts",
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc",
|
||||||
|
"lint": "tslint -c tslint.json src/**/*.ts",
|
||||||
|
"prepublish": "npm run build"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@playwright/test": "^1.48.1",
|
||||||
|
"obsidian": "latest",
|
||||||
|
"playwright": "^1.48.1",
|
||||||
|
"vitest": "^2.1.3"
|
||||||
|
},
|
||||||
|
"type": "module"
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
import { App } from "obsidian";
|
import { App } from "obsidian";
|
||||||
import { ElectronApplication, Page } from "playwright";
|
import { ElectronApplication, Page } from "playwright";
|
||||||
import { ObsidianTestingConfig } from "src";
|
import { ObsidianTestingConfig } from "./index";
|
||||||
|
|
||||||
export interface ObsidianTestFixtures {
|
export interface ObsidianTestFixtures {
|
||||||
electronApp: ElectronApplication;
|
electronApp: ElectronApplication;
|
@ -43,7 +43,7 @@ const obsidianTestFixtures: Fixtures<
|
|||||||
{ auto: true },
|
{ auto: true },
|
||||||
],
|
],
|
||||||
app: [async ({ page }, run) => {
|
app: [async ({ page }, run) => {
|
||||||
const app = await page.evaluate<App>("window.app");
|
const app: App = await page.evaluate("window.app");
|
||||||
await run(app);
|
await run(app);
|
||||||
}, {auto: true}],
|
}, {auto: true}],
|
||||||
obsidian: [{}, {option: true}]
|
obsidian: [{}, {option: true}]
|
114
packages/obsidian-testing-framework/src/tester.ts.not
Normal file
114
packages/obsidian-testing-framework/src/tester.ts.not
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
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<Parameters<typeof _electron.launch>[0]>;
|
||||||
|
export type TestOptions = Omit<RawOptions, "executablePath"> & {
|
||||||
|
vault: string;
|
||||||
|
};
|
||||||
|
export class ObsidianTester {
|
||||||
|
#loadPromise: Promise<void>;
|
||||||
|
|
||||||
|
public electronApp: ElectronApplication | null = null;
|
||||||
|
public page: Page;
|
||||||
|
public app: App;
|
||||||
|
|
||||||
|
constructor(options: Partial<TestOptions & { vault: string }>) {
|
||||||
|
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<unknown>
|
||||||
|
): Promise<unknown> {
|
||||||
|
await this.#loadPromise;
|
||||||
|
return await callback(this.app);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async doWithVault(
|
||||||
|
callback: (vault: Vault) => Promise<unknown>
|
||||||
|
): Promise<unknown> {
|
||||||
|
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<string> {
|
||||||
|
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<App>("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();
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "ESNext",
|
"target": "ES6",
|
||||||
"module": "ESNext",
|
"module": "ESNext",
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
"lib": [
|
"lib": [
|
||||||
@ -8,7 +8,7 @@
|
|||||||
"DOM",
|
"DOM",
|
||||||
"ES2018"
|
"ES2018"
|
||||||
],
|
],
|
||||||
"outDir": "lib",
|
"outDir": "packages/obsidian-testing-framework/lib",
|
||||||
"baseUrl": ".",
|
"baseUrl": ".",
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"alwaysStrict": true,
|
"alwaysStrict": true,
|
||||||
@ -37,5 +37,5 @@
|
|||||||
"typings/**/*",
|
"typings/**/*",
|
||||||
"src/**/*"
|
"src/**/*"
|
||||||
],
|
],
|
||||||
"exclude": ["test-project/**/*"]
|
"exclude": ["packages/test-project/**/*.ts"]
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
import { expect } from '@playwright/test';
|
import { expect } from '@playwright/test';
|
||||||
import {test} from "obsidian-testing-framework"
|
import {test} from "../../lib.not"
|
||||||
test('something', async ({ page }) => {
|
test('something', async ({ page }) => {
|
||||||
expect(page).toHaveURL(/obsidian\.md/i);
|
expect(page).toHaveURL(/obsidian\.md/i);
|
||||||
});
|
});
|
@ -13,7 +13,7 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"obsidian-testing-framework": "workspace:^"
|
"obsidian-testing-framework": "workspace:*"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@playwright/test": "^1.48.1",
|
"@playwright/test": "^1.48.1",
|
@ -1,5 +1,5 @@
|
|||||||
import { defineConfig, devices } from '@playwright/test';
|
import { defineConfig, devices } from '@playwright/test';
|
||||||
import { ObsidianTestingConfig } from 'obsidian-testing-framework';
|
import * as fix from "obsidian-testing-framework/fixture";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read environment variables from file.
|
* Read environment variables from file.
|
33
yarn.lock
33
yarn.lock
@ -607,15 +607,6 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"@types/node@npm:^22.7.8":
|
|
||||||
version: 22.7.8
|
|
||||||
resolution: "@types/node@npm:22.7.8"
|
|
||||||
dependencies:
|
|
||||||
undici-types: "npm:~6.19.2"
|
|
||||||
checksum: 10c0/3d3b3a2ec5a57ca4fd37b34dce415620993ca5f87cea2c728ffe73aa31446dbfe19c53171c478447bd7d78011ef4845a46ab2f0dc38e699cc75b3d100a60c690
|
|
||||||
languageName: node
|
|
||||||
linkType: hard
|
|
||||||
|
|
||||||
"@types/tern@npm:*":
|
"@types/tern@npm:*":
|
||||||
version: 0.23.9
|
version: 0.23.9
|
||||||
resolution: "@types/tern@npm:0.23.9"
|
resolution: "@types/tern@npm:0.23.9"
|
||||||
@ -1828,9 +1819,9 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"obsidian-sample-plugin@workspace:test-project":
|
"obsidian-sample-plugin@workspace:packages/test-project":
|
||||||
version: 0.0.0-use.local
|
version: 0.0.0-use.local
|
||||||
resolution: "obsidian-sample-plugin@workspace:test-project"
|
resolution: "obsidian-sample-plugin@workspace:packages/test-project"
|
||||||
dependencies:
|
dependencies:
|
||||||
"@playwright/test": "npm:^1.48.1"
|
"@playwright/test": "npm:^1.48.1"
|
||||||
"@types/node": "npm:^16.11.6"
|
"@types/node": "npm:^16.11.6"
|
||||||
@ -1839,21 +1830,26 @@ __metadata:
|
|||||||
builtin-modules: "npm:3.3.0"
|
builtin-modules: "npm:3.3.0"
|
||||||
esbuild: "npm:0.17.3"
|
esbuild: "npm:0.17.3"
|
||||||
obsidian: "npm:latest"
|
obsidian: "npm:latest"
|
||||||
obsidian-testing-framework: "workspace:^"
|
obsidian-testing-framework: "workspace:*"
|
||||||
tslib: "npm:2.4.0"
|
tslib: "npm:2.4.0"
|
||||||
typescript: "npm:4.7.4"
|
typescript: "npm:4.7.4"
|
||||||
languageName: unknown
|
languageName: unknown
|
||||||
linkType: soft
|
linkType: soft
|
||||||
|
|
||||||
"obsidian-testing-framework@workspace:., obsidian-testing-framework@workspace:^":
|
"obsidian-testing-framework-parent@workspace:.":
|
||||||
version: 0.0.0-use.local
|
version: 0.0.0-use.local
|
||||||
resolution: "obsidian-testing-framework@workspace:."
|
resolution: "obsidian-testing-framework-parent@workspace:."
|
||||||
|
languageName: unknown
|
||||||
|
linkType: soft
|
||||||
|
|
||||||
|
"obsidian-testing-framework@workspace:*, obsidian-testing-framework@workspace:packages/obsidian-testing-framework":
|
||||||
|
version: 0.0.0-use.local
|
||||||
|
resolution: "obsidian-testing-framework@workspace:packages/obsidian-testing-framework"
|
||||||
dependencies:
|
dependencies:
|
||||||
"@codemirror/language": "https://github.com/lishid/cm-language"
|
"@codemirror/language": "https://github.com/lishid/cm-language"
|
||||||
"@codemirror/state": "npm:^6.0.1"
|
"@codemirror/state": "npm:^6.0.1"
|
||||||
"@codemirror/view": "npm:^6.0.1"
|
"@codemirror/view": "npm:^6.0.1"
|
||||||
"@playwright/test": "npm:^1.48.1"
|
"@playwright/test": "npm:^1.48.1"
|
||||||
"@types/node": "npm:^22.7.8"
|
|
||||||
obsidian: "npm:latest"
|
obsidian: "npm:latest"
|
||||||
playwright: "npm:^1.48.1"
|
playwright: "npm:^1.48.1"
|
||||||
typescript: "npm:^5.6.3"
|
typescript: "npm:^5.6.3"
|
||||||
@ -2382,13 +2378,6 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"undici-types@npm:~6.19.2":
|
|
||||||
version: 6.19.8
|
|
||||||
resolution: "undici-types@npm:6.19.8"
|
|
||||||
checksum: 10c0/078afa5990fba110f6824823ace86073b4638f1d5112ee26e790155f481f2a868cc3e0615505b6f4282bdf74a3d8caad715fd809e870c2bb0704e3ea6082f344
|
|
||||||
languageName: node
|
|
||||||
linkType: hard
|
|
||||||
|
|
||||||
"unique-filename@npm:^3.0.0":
|
"unique-filename@npm:^3.0.0":
|
||||||
version: 3.0.0
|
version: 3.0.0
|
||||||
resolution: "unique-filename@npm:3.0.0"
|
resolution: "unique-filename@npm:3.0.0"
|
||||||
|
Loading…
Reference in New Issue
Block a user