Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
convert es6 tests to ts
Alex17fac31cd107
diff --git a/package.json b/package.jsonindex 1bc5e28d73..b5c7d6f475 100644--- a/package.json+++ b/package.json@@ -29,7 +29,7 @@"build-docs": "npm run build-backend-docs && npm run build-frontend-docs","webpack": "webpack -c webpack.config.js","test-jasmine": "TRILIUM_DATA_DIR=./data-test ts-node ./node_modules/.bin/jasmine",- "test-es6": "node -r esm spec-es6/attribute_parser.spec.js ",+ "test-es6": "ts-node spec-es6/attribute_parser.spec.ts ","test": "npm run test-jasmine && npm run test-es6","postinstall": "rimraf ./node_modules/canvas"},diff --git a/spec-es6/attribute_parser.spec.js b/spec-es6/attribute_parser.spec.tssimilarity index 80%rename from spec-es6/attribute_parser.spec.jsrename to spec-es6/attribute_parser.spec.tsindex d1ef190969..ad96e7644f 100644--- a/spec-es6/attribute_parser.spec.js+++ b/spec-es6/attribute_parser.spec.ts@@ -1,27 +1,28 @@-import attributeParser from '../src/public/app/services/attribute_parser.js';-import {describe, it, expect, execute} from './mini_test.js';+import * as attributeParser from '../src/public/app/services/attribute_parser.js';++import {describe, it, expect, execute} from './mini_test';describe("Lexing", () => {it("simple label", () => {- expect(attributeParser.lex("#label").map(t => t.text))+ expect(attributeParser.lex("#label").map((t: any) => t.text)).toEqual(["#label"]);});it("simple label with trailing spaces", () => {- expect(attributeParser.lex(" #label ").map(t => t.text))+ expect(attributeParser.lex(" #label ").map((t: any) => t.text)).toEqual(["#label"]);});it("inherited label", () => {- expect(attributeParser.lex("#label(inheritable)").map(t => t.text))+ expect(attributeParser.lex("#label(inheritable)").map((t: any) => t.text)).toEqual(["#label", "(", "inheritable", ")"]);- expect(attributeParser.lex("#label ( inheritable ) ").map(t => t.text))+ expect(attributeParser.lex("#label ( inheritable ) ").map((t: any) => t.text)).toEqual(["#label", "(", "inheritable", ")"]);});it("label with value", () => {- expect(attributeParser.lex("#label=Hallo").map(t => t.text))+ expect(attributeParser.lex("#label=Hallo").map((t: any) => t.text)).toEqual(["#label", "=", "Hallo"]);});@@ -32,25 +33,25 @@ describe("Lexing", () => {});it("relation with value", () => {- expect(attributeParser.lex('~relation=#root/RclIpMauTOKS/NFi2gL4xtPxM').map(t => t.text))+ expect(attributeParser.lex('~relation=#root/RclIpMauTOKS/NFi2gL4xtPxM').map((t: any) => t.text)).toEqual(["~relation", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"]);});it("use quotes to define value", () => {- expect(attributeParser.lex("#'label a'='hello\"` world'").map(t => t.text))+ expect(attributeParser.lex("#'label a'='hello\"` world'").map((t: any) => t.text)).toEqual(["#label a", "=", 'hello"` world']);- expect(attributeParser.lex('#"label a" = "hello\'` world"').map(t => t.text))+ expect(attributeParser.lex('#"label a" = "hello\'` world"').map((t: any) => t.text)).toEqual(["#label a", "=", "hello'` world"]);- expect(attributeParser.lex('#`label a` = `hello\'" world`').map(t => t.text))+ expect(attributeParser.lex('#`label a` = `hello\'" world`').map((t: any) => t.text)).toEqual(["#label a", "=", "hello'\" world"]);});});describe("Parser", () => {it("simple label", () => {- const attrs = attributeParser.parse(["#token"].map(t => ({text: t})));+ const attrs = attributeParser.parse(["#token"].map((t: any) => ({text: t})));expect(attrs.length).toEqual(1);expect(attrs[0].type).toEqual('label');@@ -60,7 +61,7 @@ describe("Parser", () => {});it("inherited label", () => {- const attrs = attributeParser.parse(["#token", "(", "inheritable", ")"].map(t => ({text: t})));+ const attrs = attributeParser.parse(["#token", "(", "inheritable", ")"].map((t: any) => ({text: t})));expect(attrs.length).toEqual(1);expect(attrs[0].type).toEqual('label');@@ -70,7 +71,7 @@ describe("Parser", () => {});it("label with value", () => {- const attrs = attributeParser.parse(["#token", "=", "val"].map(t => ({text: t})));+ const attrs = attributeParser.parse(["#token", "=", "val"].map((t: any) => ({text: t})));expect(attrs.length).toEqual(1);expect(attrs[0].type).toEqual('label');@@ -79,14 +80,14 @@ describe("Parser", () => {});it("relation", () => {- let attrs = attributeParser.parse(["~token", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"].map(t => ({text: t})));+ let attrs = attributeParser.parse(["~token", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"].map((t: any) => ({text: t})));expect(attrs.length).toEqual(1);expect(attrs[0].type).toEqual('relation');expect(attrs[0].name).toEqual("token");expect(attrs[0].value).toEqual('NFi2gL4xtPxM');- attrs = attributeParser.parse(["~token", "=", "#NFi2gL4xtPxM"].map(t => ({text: t})));+ attrs = attributeParser.parse(["~token", "=", "#NFi2gL4xtPxM"].map((t: any) => ({text: t})));expect(attrs.length).toEqual(1);expect(attrs[0].type).toEqual('relation');diff --git a/spec-es6/mini_test.js b/spec-es6/mini_test.tssimilarity index 88%rename from spec-es6/mini_test.jsrename to spec-es6/mini_test.tsindex cc6883a8bc..1105981c08 100644--- a/spec-es6/mini_test.js+++ b/spec-es6/mini_test.ts@@ -1,10 +1,10 @@-export function describe(name, cb) {+export function describe(name: string, cb: () => any) {console.log(`Running ${name}`);cb();}-export async function it(name, cb) {+export async function it(name: string, cb: () => any) {console.log(` Running ${name}`);cb();@@ -12,9 +12,9 @@ export async function it(name, cb) {let errorCount = 0;-export function expect(val) {+export function expect(val: any) {return {- toEqual: comparedVal => {+ toEqual: (comparedVal: any) => {const jsonVal = JSON.stringify(val);const comparedJsonVal = JSON.stringify(comparedVal);@@ -44,11 +44,11 @@ export function expect(val) {errorCount++;}},- toThrow: errorMessage => {+ toThrow: (errorMessage: any) => {try {val();}- catch (e) {+ catch (e: any) {if (e.message !== errorMessage) {console.trace("toThrow caught exception, but messages differ");console.error(`expected: ${errorMessage}`);diff --git a/src/public/app/services/attribute_parser.d.ts b/src/public/app/services/attribute_parser.d.tsnew file mode 100644index 0000000000..58fe9b9169--- /dev/null+++ b/src/public/app/services/attribute_parser.d.ts@@ -0,0 +1,7 @@+declare module 'attribute_parser';+++export function lex(str: string): any[]+export function parse(tokens: any[], str?: string, allowEmptyRelations?: boolean): any[]+export function lexAndParse(str: string, allowEmptyRelations?: boolean): any[]+diff --git a/src/public/app/services/attribute_parser.js b/src/public/app/services/attribute_parser.jsindex 9665e35792..863e5f5324 100644--- a/src/public/app/services/attribute_parser.js+++ b/src/public/app/services/attribute_parser.js@@ -1,4 +1,4 @@-import utils from "./utils.js";+const utils = require("./utils.js");function lex(str) {str = str.trim();@@ -222,7 +222,7 @@ function lexAndParse(str, allowEmptyRelations = false) {return parse(tokens, str, allowEmptyRelations);}-export default {+module.exports = {lex,parse,lexAndParsediff --git a/src/public/app/services/utils.js b/src/public/app/services/utils.jsindex b88146012e..3b0d650c04 100644--- a/src/public/app/services/utils.js+++ b/src/public/app/services/utils.js@@ -505,7 +505,7 @@ function createImageSrcUrl(note) {return `api/images/${note.noteId}/${encodeURIComponent(note.title)}?timestamp=${Date.now()}`;}-export default {+module.exports = {reloadFrontendApp,parseDate,formatDateISO,diff --git a/tsconfig.json b/tsconfig.jsonindex 0024dc4eb6..77e3cdb5b4 100644--- a/tsconfig.json+++ b/tsconfig.json@@ -11,7 +11,7 @@"downlevelIteration": true,"skipLibCheck": true},- "include": ["./src/**/*.js", "./src/**/*.ts", "./*.ts", "./spec/**/*.ts"],+ "include": ["./src/**/*.js", "./src/**/*.ts", "./*.ts", "./spec/**/*.ts", "./spec-es6/**/*.ts"],"exclude": ["./node_modules/**/*"],"ts-node": {"files": true