Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
- name: Get NPM Version
id: package-version
uses: martinbeentjes/[email protected]
- name: Run Tests
run: xvfb-run -a yarn test
- name: Check if version exists
id: check-version
env:
Expand Down
30 changes: 30 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Unit Tests

on:
push:
branches: ["main", "master"]
pull_request:
branches: ["main", "master"]

jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
name: Test on ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Use Node.js 22
uses: actions/setup-node@v4
with:
node-version: 22
cache: 'yarn'
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Run Tests (Linux)
if: runner.os == 'Linux'
run: xvfb-run -a yarn test
- name: Run Tests (Windows/Mac)
if: runner.os != 'Linux'
run: yarn test
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"compile": "tsc -p ./",
"lint": "eslint src --ext ts",
"watch": "tsc -watch -p ./",
"pretest": "npm run compile && npm run lint",
"pretest": "npm run compile",
"test": "node ./out/test/runTest.js"
},
"devDependencies": {
Expand All @@ -88,7 +88,7 @@
"lodash": "^4.17.21",
"mocha": "^11.7.5",
"typescript": "^5.9.3",
"vscode-test": "^1.3.0"
"@vscode/test-electron": "^2.4.1"
},
"dependencies": {
"@types/temp": "^0.9.4",
Expand Down
22 changes: 22 additions & 0 deletions src/test/runTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as path from "path";
import { runTests } from "@vscode/test-electron";

async function main() {
try {
// The folder containing the Extension Manifest package.json
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath = path.resolve(__dirname, "../../");

// The path to test runner
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, "./suite/index");

// Download VS Code, unzip it and run the integration test
await runTests({ extensionDevelopmentPath, extensionTestsPath });
} catch (err) {
console.error("Failed to run tests");
process.exit(1);
}
}

main();
19 changes: 19 additions & 0 deletions src/test/suite/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as assert from "assert";
import * as vscode from "vscode";
import { getConfig } from "../../config";

suite("Configuration Test Suite", () => {
vscode.window.showInformationMessage("Start config tests.");

test("Default configuration values", () => {
const config = getConfig();
assert.strictEqual(config.ps2pdf, "ps2pdf");
assert.strictEqual(config.pdftocairo, "pdftocairo");
assert.strictEqual(config.pdfinfo, "pdfinfo");
});
test("Configuration should be readable", () => {
const config = vscode.workspace.getConfiguration("postscript-preview");
assert.ok(config.has("path.ps2pdf"));
assert.ok(config.has("path.pdftocairo"));
});
});
31 changes: 31 additions & 0 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as assert from "assert";
import * as vscode from "vscode";

suite("Extension Test Suite", () => {
vscode.window.showInformationMessage("Start extension tests.");

test("Extension should be present", () => {
assert.ok(
vscode.extensions.getExtension("ahnafnafee.postscript-preview")
);
});

test("Extension should activate", async () => {
const ext = vscode.extensions.getExtension(
"ahnafnafee.postscript-preview"
);
assert.ok(ext, "Extension not found");

// Activate if not active
if (!ext.isActive) {
await ext.activate();
}

assert.strictEqual(ext.isActive, true);
});

test("Command should be registered", async () => {
const commands = await vscode.commands.getCommands(true);
assert.ok(commands.includes("postscript-preview.sidePreview"));
});
});
35 changes: 35 additions & 0 deletions src/test/suite/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as path from "path";
import * as Mocha from "mocha";
import { glob } from "glob";

export async function run(): Promise<void> {
// Create the mocha test
const mocha = new Mocha({
ui: "tdd",
color: true,
});

const testsRoot = path.resolve(__dirname, "..");

// Use glob to find all files ending in .test.js
const files = await glob("**/**.test.js", { cwd: testsRoot });

// Add files to the test suite
files.forEach((f) => mocha.addFile(path.resolve(testsRoot, f)));

return new Promise((resolve, reject) => {
try {
// Run the mocha test
mocha.run((failures) => {
if (failures > 0) {
reject(new Error(`${failures} tests failed.`));
} else {
resolve();
}
});
} catch (err) {
console.error(err);
reject(err);
}
});
}
Loading