Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit eff6ca3

Browse files
authored
Merge pull request #173 from TriliumNext/feat/ts-unit-and-integration-tests
feat: TS unit and integration tests
2 parents 653fba3 + 6a7eb9b commit eff6ca3

31 files changed

+1814
-1700
lines changed

package-lock.json

Lines changed: 15 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
"build-frontend-docs": "rm -rf ./docs/frontend_api && ./node_modules/.bin/jsdoc -c jsdoc-conf.json -d ./docs/frontend_api src/public/app/entities/*.js src/public/app/services/frontend_script_api.js src/public/app/widgets/basic_widget.js src/public/app/widgets/note_context_aware_widget.js src/public/app/widgets/right_panel_widget.js",
2929
"build-docs": "npm run build-backend-docs && npm run build-frontend-docs",
3030
"webpack": "webpack -c webpack.config.ts",
31-
"test-jasmine": "TRILIUM_DATA_DIR=~/trilium/data-test jasmine",
32-
"test-es6": "node -r esm spec-es6/attribute_parser.spec.js ",
31+
"test-jasmine": "TRILIUM_DATA_DIR=./data-test ts-node ./node_modules/.bin/jasmine",
32+
"test-es6": "ts-node -r esm spec-es6/attribute_parser.spec.ts",
3333
"test": "npm run test-jasmine && npm run test-es6",
3434
"postinstall": "rimraf ./node_modules/canvas"
3535
},
@@ -121,6 +121,7 @@
121121
"@types/express-session": "^1.18.0",
122122
"@types/html": "^1.0.4",
123123
"@types/ini": "^4.1.0",
124+
"@types/jasmine": "^5.1.4",
124125
"@types/jsdom": "^21.1.6",
125126
"@types/mime-types": "^2.1.4",
126127
"@types/multer": "^1.4.11",

spec-es6/attribute_parser.spec.js renamed to spec-es6/attribute_parser.spec.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
import attributeParser from '../src/public/app/services/attribute_parser.js';
2-
import {describe, it, expect, execute} from './mini_test.js';
1+
import * as attributeParser from '../src/public/app/services/attribute_parser.js';
2+
3+
import {describe, it, expect, execute} from './mini_test';
34

45
describe("Lexing", () => {
56
it("simple label", () => {
6-
expect(attributeParser.lex("#label").map(t => t.text))
7+
expect(attributeParser.lex("#label").map((t: any) => t.text))
78
.toEqual(["#label"]);
89
});
910

1011
it("simple label with trailing spaces", () => {
11-
expect(attributeParser.lex(" #label ").map(t => t.text))
12+
expect(attributeParser.lex(" #label ").map((t: any) => t.text))
1213
.toEqual(["#label"]);
1314
});
1415

1516
it("inherited label", () => {
16-
expect(attributeParser.lex("#label(inheritable)").map(t => t.text))
17+
expect(attributeParser.lex("#label(inheritable)").map((t: any) => t.text))
1718
.toEqual(["#label", "(", "inheritable", ")"]);
1819

19-
expect(attributeParser.lex("#label ( inheritable ) ").map(t => t.text))
20+
expect(attributeParser.lex("#label ( inheritable ) ").map((t: any) => t.text))
2021
.toEqual(["#label", "(", "inheritable", ")"]);
2122
});
2223

2324
it("label with value", () => {
24-
expect(attributeParser.lex("#label=Hallo").map(t => t.text))
25+
expect(attributeParser.lex("#label=Hallo").map((t: any) => t.text))
2526
.toEqual(["#label", "=", "Hallo"]);
2627
});
2728

@@ -32,25 +33,25 @@ describe("Lexing", () => {
3233
});
3334

3435
it("relation with value", () => {
35-
expect(attributeParser.lex('~relation=#root/RclIpMauTOKS/NFi2gL4xtPxM').map(t => t.text))
36+
expect(attributeParser.lex('~relation=#root/RclIpMauTOKS/NFi2gL4xtPxM').map((t: any) => t.text))
3637
.toEqual(["~relation", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"]);
3738
});
3839

3940
it("use quotes to define value", () => {
40-
expect(attributeParser.lex("#'label a'='hello\"` world'").map(t => t.text))
41+
expect(attributeParser.lex("#'label a'='hello\"` world'").map((t: any) => t.text))
4142
.toEqual(["#label a", "=", 'hello"` world']);
4243

43-
expect(attributeParser.lex('#"label a" = "hello\'` world"').map(t => t.text))
44+
expect(attributeParser.lex('#"label a" = "hello\'` world"').map((t: any) => t.text))
4445
.toEqual(["#label a", "=", "hello'` world"]);
4546

46-
expect(attributeParser.lex('#`label a` = `hello\'" world`').map(t => t.text))
47+
expect(attributeParser.lex('#`label a` = `hello\'" world`').map((t: any) => t.text))
4748
.toEqual(["#label a", "=", "hello'\" world"]);
4849
});
4950
});
5051

5152
describe("Parser", () => {
5253
it("simple label", () => {
53-
const attrs = attributeParser.parse(["#token"].map(t => ({text: t})));
54+
const attrs = attributeParser.parse(["#token"].map((t: any) => ({text: t})));
5455

5556
expect(attrs.length).toEqual(1);
5657
expect(attrs[0].type).toEqual('label');
@@ -60,7 +61,7 @@ describe("Parser", () => {
6061
});
6162

6263
it("inherited label", () => {
63-
const attrs = attributeParser.parse(["#token", "(", "inheritable", ")"].map(t => ({text: t})));
64+
const attrs = attributeParser.parse(["#token", "(", "inheritable", ")"].map((t: any) => ({text: t})));
6465

6566
expect(attrs.length).toEqual(1);
6667
expect(attrs[0].type).toEqual('label');
@@ -70,7 +71,7 @@ describe("Parser", () => {
7071
});
7172

7273
it("label with value", () => {
73-
const attrs = attributeParser.parse(["#token", "=", "val"].map(t => ({text: t})));
74+
const attrs = attributeParser.parse(["#token", "=", "val"].map((t: any) => ({text: t})));
7475

7576
expect(attrs.length).toEqual(1);
7677
expect(attrs[0].type).toEqual('label');
@@ -79,14 +80,14 @@ describe("Parser", () => {
7980
});
8081

8182
it("relation", () => {
82-
let attrs = attributeParser.parse(["~token", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"].map(t => ({text: t})));
83+
let attrs = attributeParser.parse(["~token", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"].map((t: any) => ({text: t})));
8384

8485
expect(attrs.length).toEqual(1);
8586
expect(attrs[0].type).toEqual('relation');
8687
expect(attrs[0].name).toEqual("token");
8788
expect(attrs[0].value).toEqual('NFi2gL4xtPxM');
8889

89-
attrs = attributeParser.parse(["~token", "=", "#NFi2gL4xtPxM"].map(t => ({text: t})));
90+
attrs = attributeParser.parse(["~token", "=", "#NFi2gL4xtPxM"].map((t: any) => ({text: t})));
9091

9192
expect(attrs.length).toEqual(1);
9293
expect(attrs[0].type).toEqual('relation');

spec-es6/mini_test.js renamed to spec-es6/mini_test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
export function describe(name, cb) {
1+
export function describe(name: string, cb: () => any) {
22
console.log(`Running ${name}`);
33

44
cb();
55
}
66

7-
export async function it(name, cb) {
7+
export async function it(name: string, cb: () => any) {
88
console.log(` Running ${name}`);
99

1010
cb();
1111
}
1212

1313
let errorCount = 0;
1414

15-
export function expect(val) {
15+
export function expect(val: any) {
1616
return {
17-
toEqual: comparedVal => {
17+
toEqual: (comparedVal: any) => {
1818
const jsonVal = JSON.stringify(val);
1919
const comparedJsonVal = JSON.stringify(comparedVal);
2020

@@ -44,11 +44,11 @@ export function expect(val) {
4444
errorCount++;
4545
}
4646
},
47-
toThrow: errorMessage => {
47+
toThrow: (errorMessage: any) => {
4848
try {
4949
val();
5050
}
51-
catch (e) {
51+
catch (e: any) {
5252
if (e.message !== errorMessage) {
5353
console.trace("toThrow caught exception, but messages differ");
5454
console.error(`expected: ${errorMessage}`);

spec/etapi/app_info.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

spec/etapi/app_info.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import etapi = require("../support/etapi");
2+
3+
etapi.describeEtapi("app_info", () => {
4+
it("get", async () => {
5+
const appInfo = await etapi.getEtapi("app-info");
6+
expect(appInfo.clipperProtocolVersion).toEqual("1.0");
7+
});
8+
});

spec/etapi/backup.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

spec/etapi/backup.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import etapi = require("../support/etapi");
2+
3+
etapi.describeEtapi("backup", () => {
4+
it("create", async () => {
5+
const response = await etapi.putEtapiContent("backup/etapi_test");
6+
expect(response.status).toEqual(204);
7+
});
8+
});

spec/etapi/import.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

spec/etapi/import.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import etapi = require("../support/etapi");
2+
import fs = require("fs");
3+
import path = require("path");
4+
5+
etapi.describeEtapi("import", () => {
6+
// temporarily skip this test since test-export.zip is missing
7+
xit("import", async () => {
8+
const zipFileBuffer = fs.readFileSync(
9+
path.resolve(__dirname, "test-export.zip")
10+
);
11+
12+
const response = await etapi.postEtapiContent(
13+
"notes/root/import",
14+
zipFileBuffer
15+
);
16+
expect(response.status).toEqual(201);
17+
18+
const { note, branch } = await response.json();
19+
20+
expect(note.title).toEqual("test-export");
21+
expect(branch.parentNoteId).toEqual("root");
22+
23+
const content = await (
24+
await etapi.getEtapiContent(`notes/${note.noteId}/content`)
25+
).text();
26+
expect(content).toContain("test export content");
27+
});
28+
});

0 commit comments

Comments
 (0)