From 17fac31cd107188aa05771a4c944a0b7da176577 Mon Sep 17 00:00:00 2001 From: Alex <54153428+alexpietsch@users.noreply.github.com> Date: Sun, 9 Jun 2024 12:16:09 +0200 Subject: [PATCH] convert es6 tests to ts --- package.json | 2 +- ...arser.spec.js => attribute_parser.spec.ts} | 33 ++++++++++--------- spec-es6/{mini_test.js => mini_test.ts} | 12 +++---- src/public/app/services/attribute_parser.d.ts | 7 ++++ src/public/app/services/attribute_parser.js | 4 +-- src/public/app/services/utils.js | 2 +- tsconfig.json | 2 +- 7 files changed, 35 insertions(+), 27 deletions(-) rename spec-es6/{attribute_parser.spec.js => attribute_parser.spec.ts} (80%) rename spec-es6/{mini_test.js => mini_test.ts} (88%) create mode 100644 src/public/app/services/attribute_parser.d.ts diff --git a/package.json b/package.json index 1bc5e28d7..b5c7d6f47 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.ts similarity index 80% rename from spec-es6/attribute_parser.spec.js rename to spec-es6/attribute_parser.spec.ts index d1ef19096..ad96e7644 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.ts similarity index 88% rename from spec-es6/mini_test.js rename to spec-es6/mini_test.ts index cc6883a8b..1105981c0 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.ts new file mode 100644 index 000000000..58fe9b916 --- /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.js index 9665e3579..863e5f532 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, lexAndParse diff --git a/src/public/app/services/utils.js b/src/public/app/services/utils.js index b88146012..3b0d650c0 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.json index 0024dc4eb..77e3cdb5b 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