diff --git a/.dockerignore b/.dockerignore index 4efd730c1..ad1631bd8 100644 --- a/.dockerignore +++ b/.dockerignore @@ -136,6 +136,7 @@ src/ .pm2.config.js .prettierignore .prettierrc +jsconfig.json CHANGELOG.md CODE_OF_CONDUCT.md CONTRIBUTING.md diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 000000000..b395a4546 --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + // "checkJs": true, + // "strict": true + "lib": ["ES2022"], + "moduleResolution": "nodenext", + "resolveJsonModule": true + }, + "exclude": ["dist", "node_modules"] +} diff --git a/package-lock.json b/package-lock.json index 4b7949e91..7963f23e1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -55,6 +55,7 @@ "devDependencies": { "@commitlint/cli": "^17.6.7", "@commitlint/config-conventional": "^17.6.7", + "@types/jest": "^29.5.3", "autocannon": "^7.12.0", "esbuild": "^0.18.14", "esbuild-plugin-glob": "^2.2.2", @@ -2531,6 +2532,16 @@ "@types/istanbul-lib-report": "*" } }, + "node_modules/@types/jest": { + "version": "29.5.3", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-29.5.3.tgz", + "integrity": "sha512-1Nq7YrO/vJE/FYnqYyw0FS8LdrjExSgIiHyKg7xPpn+yi8Q4huZryKnkJatN1ZRH89Kw2v33/8ZMB7DuZeSLlA==", + "dev": true, + "dependencies": { + "expect": "^29.0.0", + "pretty-format": "^29.0.0" + } + }, "node_modules/@types/json-schema": { "version": "7.0.12", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.12.tgz", diff --git a/package.json b/package.json index 17bb2c411..6820af710 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "devDependencies": { "@commitlint/cli": "^17.6.7", "@commitlint/config-conventional": "^17.6.7", + "@types/jest": "^29.5.3", "autocannon": "^7.12.0", "esbuild": "^0.18.14", "esbuild-plugin-glob": "^2.2.2", diff --git a/src/plugins/doc-to-txt/plugin.test.js b/src/plugins/doc-to-txt/plugin.test.js index 86a1a723c..a552c03bb 100644 --- a/src/plugins/doc-to-txt/plugin.test.js +++ b/src/plugins/doc-to-txt/plugin.test.js @@ -6,6 +6,9 @@ const sensible = require("@fastify/sensible"); const plugin = require("."); describe("DOC-to-TXT conversion plugin", () => { + /** + * @type {Fastify.FastifyInstance} + */ let server; beforeAll(async () => { diff --git a/src/plugins/docx-to-html/plugin.test.js b/src/plugins/docx-to-html/plugin.test.js index f414ec8c3..5914d4e34 100644 --- a/src/plugins/docx-to-html/plugin.test.js +++ b/src/plugins/docx-to-html/plugin.test.js @@ -7,6 +7,9 @@ const sensible = require("@fastify/sensible"); const plugin = require("."); describe("DOCX-to-HTML conversion plugin", () => { + /** + * @type {Fastify.FastifyInstance} + */ let server; beforeAll(async () => { diff --git a/src/plugins/embed-html-images/plugin.test.js b/src/plugins/embed-html-images/plugin.test.js index 326701bc3..558180b6d 100644 --- a/src/plugins/embed-html-images/plugin.test.js +++ b/src/plugins/embed-html-images/plugin.test.js @@ -8,6 +8,9 @@ const getConfig = require("../../config"); describe("Embed-HTML-Images plugin", () => { let config; + /** + * @type {Fastify.FastifyInstance} + */ let server; beforeAll(async () => { diff --git a/src/plugins/html-to-txt/plugin.test.js b/src/plugins/html-to-txt/plugin.test.js index 263bdb9e4..8b1d23be5 100644 --- a/src/plugins/html-to-txt/plugin.test.js +++ b/src/plugins/html-to-txt/plugin.test.js @@ -7,6 +7,9 @@ const sensible = require("@fastify/sensible"); const plugin = require("."); describe("HTML-to-TXT conversion plugin", () => { + /** + * @type {Fastify.FastifyInstance} + */ let server; beforeAll(async () => { diff --git a/src/plugins/image-to-txt/plugin.test.js b/src/plugins/image-to-txt/plugin.test.js index 5b655475e..a95f882cb 100644 --- a/src/plugins/image-to-txt/plugin.test.js +++ b/src/plugins/image-to-txt/plugin.test.js @@ -9,6 +9,9 @@ const getConfig = require("../../config"); describe("Image-to-TXT conversion plugin", () => { let config; + /** + * @type {Fastify.FastifyInstance} + */ let server; beforeAll(async () => { diff --git a/src/plugins/pdf-to-html/plugin.test.js b/src/plugins/pdf-to-html/plugin.test.js index 49243906a..16038e12d 100644 --- a/src/plugins/pdf-to-html/plugin.test.js +++ b/src/plugins/pdf-to-html/plugin.test.js @@ -21,6 +21,9 @@ const artifacts = describe("PDF-to-HTML conversion plugin", () => { let config; + /** + * @type {Fastify.FastifyInstance} + */ let server; beforeAll(async () => { @@ -73,8 +76,8 @@ describe("PDF-to-HTML conversion plugin", () => { "./test_resources/test_files/pdf_1.3_NHS_Constitution.pdf" ), query: { - lastPageToConvert: 2, - ignoreImages: false, + lastPageToConvert: "2", + ignoreImages: "false", ...query, }, headers: { @@ -97,10 +100,10 @@ describe("PDF-to-HTML conversion plugin", () => { httpEquiv: expect.stringMatching(/^content-type$/iu), }); expect( - dom.window.document.head.querySelector("title").textContent + dom.window.document.head.querySelector("title")?.textContent ).toMatch(/^docsmith_pdf-to-html_/u); // String found in first paragraph of the test document - expect(dom.window.document.querySelector("p").textContent).toMatch( + expect(dom.window.document.querySelector("p")?.textContent).toMatch( /for\sEngland\s/u ); // String found in last paragraph of the test document @@ -167,8 +170,8 @@ describe("PDF-to-HTML conversion plugin", () => { "./test_resources/test_files/pdf_1.3_NHS_Constitution.pdf" ), query: { - lastPageToConvert: 1, - ignoreImages: false, + lastPageToConvert: "1", + ignoreImages: "false", }, headers: { "content-type": "application/pdf", diff --git a/src/plugins/pdf-to-txt/plugin.test.js b/src/plugins/pdf-to-txt/plugin.test.js index 1456cf968..b0244ec3d 100644 --- a/src/plugins/pdf-to-txt/plugin.test.js +++ b/src/plugins/pdf-to-txt/plugin.test.js @@ -14,6 +14,9 @@ const imageToTxt = require("../image-to-txt"); describe("PDF-to-TXT conversion plugin", () => { let config; + /** + * @type {Fastify.FastifyInstance} + */ let server; beforeAll(async () => { @@ -67,8 +70,8 @@ describe("PDF-to-TXT conversion plugin", () => { "./test_resources/test_files/pdf_1.3_NHS_Constitution.pdf" ), query: { - firstPageToConvert: 2, - lastPageToConvert: 2, + firstPageToConvert: "2", + lastPageToConvert: "2", ...query, }, headers: { @@ -87,12 +90,12 @@ describe("PDF-to-TXT conversion plugin", () => { it.each([ { testName: "Converts PDF file to TXT using OCR", - query: { ocr: true }, + query: { ocr: "true" }, }, { testName: "Converts PDF file to TXT using OCR and ignore invalid `test` query string param ", - query: { ocr: true, test: "test" }, + query: { ocr: "true", test: "test" }, }, ])("$testName", async ({ query }) => { const response = await server.inject({ @@ -102,8 +105,8 @@ describe("PDF-to-TXT conversion plugin", () => { "./test_resources/test_files/pdf_1.3_NHS_Constitution.pdf" ), query: { - firstPageToConvert: 1, - lastPageToConvert: 2, + firstPageToConvert: "1", + lastPageToConvert: "2", ...query, }, headers: { @@ -137,9 +140,9 @@ describe("PDF-to-TXT conversion plugin", () => { "./test_resources/test_files/pdf_1.3_NHS_Constitution.pdf" ), query: { - firstPageToConvert: 2, - generateHtmlMetaFile: true, - lastPageToConvert: 2, + firstPageToConvert: "2", + generateHtmlMetaFile: "true", + lastPageToConvert: "2", }, headers: { "content-type": "application/pdf", @@ -160,14 +163,14 @@ describe("PDF-to-TXT conversion plugin", () => { httpEquiv: expect.stringMatching(/^content-type$/iu), }); expect( - dom.window.document.head.querySelector("title").textContent + dom.window.document.head.querySelector("title")?.textContent ).toMatch(/^docsmith_pdf-to-txt_/u); // String found at the start of the HTML document - expect(dom.window.document.querySelector("pre").textContent).toMatch( + expect(dom.window.document.querySelector("pre")?.textContent).toMatch( "The NHS belongs to the people" ); // String found at the end of the HTML document - expect(dom.window.document.querySelector("pre").textContent).toMatch( + expect(dom.window.document.querySelector("pre")?.textContent).toMatch( /a full and transparent debate with the public, patients and staff.$/mu ); expect(response.statusCode).toBe(200); @@ -184,8 +187,8 @@ describe("PDF-to-TXT conversion plugin", () => { testName: "is not a valid PDF file for OCR", read: true, query: { - lastPageToConvert: 1, - ocr: true, + lastPageToConvert: "1", + ocr: "true", }, }, ])( @@ -224,7 +227,7 @@ describe("PDF-to-TXT conversion plugin", () => { testName: "poppler.pdfToCairo() for OCR", funcName: "pdfToCairo", query: { - ocr: true, + ocr: "true", }, }, ])( @@ -241,7 +244,7 @@ describe("PDF-to-TXT conversion plugin", () => { "./test_resources/test_files/pdf_1.3_NHS_Constitution.pdf" ), query: { - lastPageToConvert: 1, + lastPageToConvert: "1", ...query, }, headers: { diff --git a/src/plugins/rtf-to-html/plugin.test.js b/src/plugins/rtf-to-html/plugin.test.js index c312872d9..fbbda3670 100644 --- a/src/plugins/rtf-to-html/plugin.test.js +++ b/src/plugins/rtf-to-html/plugin.test.js @@ -21,6 +21,9 @@ const artifacts = describe("RTF-to-HTML conversion plugin", () => { let config; + /** + * @type {Fastify.FastifyInstance} + */ let server; beforeAll(async () => { @@ -76,7 +79,7 @@ describe("RTF-to-HTML conversion plugin", () => { httpEquiv: expect.stringMatching(/^content-type$/iu), }); expect( - dom.window.document.head.querySelector("title").textContent + dom.window.document.head.querySelector("title")?.textContent ).toMatch(/^docsmith_rtf-to-html_/u); // Check all images are removed expect(dom.window.document.querySelectorAll("img")).toHaveLength(0); diff --git a/src/plugins/tidy-css/plugin.test.js b/src/plugins/tidy-css/plugin.test.js index 7aadcb1ca..024ffaf3a 100644 --- a/src/plugins/tidy-css/plugin.test.js +++ b/src/plugins/tidy-css/plugin.test.js @@ -8,6 +8,9 @@ const getConfig = require("../../config"); describe("Tidy-CSS plugin", () => { let config; + /** + * @type {Fastify.FastifyInstance} + */ let server; beforeAll(async () => { @@ -112,17 +115,17 @@ describe("Tidy-CSS plugin", () => { // Check CSS is combined into one style tag expect(dom.window.document.querySelectorAll("style")).toHaveLength(1); // Check font-family is set to expected value - expect(style.innerHTML).toMatch(expected?.fonts || /./u); + expect(style?.innerHTML).toMatch(expected?.fonts || /./u); // Check background-color is set to expected value - expect(style.innerHTML).toMatch(expected?.backgroundColor || /./u); + expect(style?.innerHTML).toMatch(expected?.backgroundColor || /./u); // Check page-break-inside is set to avoid - expect(style.innerHTML).toMatch( + expect(style?.innerHTML).toMatch( file !== "html_valid_no_style_type.html" ? /page-break-inside:avoid/u : /./u ); // Check CSS is tidied and minified - expect(style.innerHTML).not.toMatch(/;\}|