Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tests for Creating a New Project #249

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"lint": "svelte-check --tsconfig tsconfig.json --fail-on-hints && eslint . --ext .ts",
"test:unit": "vitest",
"test:integration": "rollup --config rollup.config.js -w --environment BUILD:integrationTest",
"dev": "rollup --config rollup.config.js -w",
"build": "npm run lint && rollup --config rollup.config.js --environment BUILD:production"
},
Expand Down
11 changes: 10 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ const DEV_PLUGIN_CONFIG = {
),
};

const INT_TEST_PLUGIN_CONFIG = {
...DEV_PLUGIN_CONFIG,
input: "src/integration/main.ts",
};

const PROD_PLUGIN_CONFIG = {
...BASE_CONFIG,
output: {
Expand All @@ -75,6 +80,10 @@ const PROD_PLUGIN_CONFIG = {
plugins: getPlugins(),
};

const config = isProd ? PROD_PLUGIN_CONFIG : DEV_PLUGIN_CONFIG;
const config = isProd
? PROD_PLUGIN_CONFIG
: env.BUILD === "integrationTest"
? INT_TEST_PLUGIN_CONFIG
: DEV_PLUGIN_CONFIG;

export default config;
90 changes: 90 additions & 0 deletions src/integration/ConsoleReporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import type { TestReporter } from "./framework/report";

type StructuredResults = {
id: string;
name: string;
parent: StructuredResults | null;
result: Error | void;
};

/**
* Reports test restuls to the console.
*/
export class ConsoleReporter implements TestReporter {
private structure: StructuredResults = {
id: "root",
name: "root",
parent: null,
result: undefined,
};

private readonly structures = new Map<string, StructuredResults>();

/**
*
* @param verbose If you want to see all the running tests, and all tests that pass, set to `true`.
* @default verbose `false`
*/
constructor(private readonly verbose: boolean = false) {}

onCollectSuite(id: string, name: string): void {
const structure: StructuredResults = {
id,
name,
parent: this.structure,
result: undefined,
};
this.structure = structure;
this.structures.set(id, structure);
}

onSuiteCollected(id: string): void {
this.structure = this.structure.parent;
}

onCollectTest(id: string, name: string): void {
const structure: StructuredResults = {
id,
name,
parent: this.structure,
result: undefined,
};
this.structures.set(id, structure);
}

onStartSuite(id: string): void {}

onStartTest(id: string): void {
if (this.verbose) {
const structure = this.structures.get(id);
const fullName = buildName(structure);
console.log("Running", fullName);
}
}

onEndTest(
id: string,
result: unknown | void,
afterResult: void | Error
): void {
const structure = this.structures.get(id);
const fullName = buildName(structure);
if (!!result) {
console.log("failed", fullName);
} else if (this.verbose) {
console.log("passed", fullName);
}
}

onEndSuite(id: string): void {}
}

function buildName(structure: StructuredResults): string {
if (structure.parent) {
return buildName(structure.parent) + " > " + structure.name;
}
if (structure.id === "root") {
return "";
}
return structure.name;
}
21 changes: 21 additions & 0 deletions src/integration/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# What is this?
This folder contains potentially slow-running integration tests that require working with the obsidian API directly.

## Contracts Folder
The contracts folder provides contracts to interfaces that unit tests, and integration tests, can run against.

## Framework Folder
The framework folder is an ad-hoc testing framework that can run within obsidian.

# Why is it inside the src folder?
To make the rollup-typscript plugin not complain about typescript files being outside of the rootDir.

# Will it be included in the plugin?
No. There is a new build configuration that targets the main.ts file located here. It is a complete isolated build. When the normal build process is run, the normal main.ts file with the src folder is targeted, and nothing in this folder is imported, thus, nothing in this folder is bundled.

## How do I run the tests?
run `npm run test:integration`
Then open the test vault in obsidian.

## How do I add new tests?
The ad-hoc framework does not automatically detect tests within the tests folder, so, for now, a reference to your new test will need to be added to the list in the run.ts file.
Loading