From 7b4e61eccc222e8f33361e9529e29b5853fcdc94 Mon Sep 17 00:00:00 2001 From: Mike Lischke Date: Tue, 26 Dec 2023 15:31:59 +0100 Subject: [PATCH] Tests use ESM again A bit surprising, given that I had so much trouble with decorators and the previous configuration. But now it seems to work well together (ESM + TC39 decorators) Signed-off-by: Mike Lischke --- .vscode/launch.json | 2 ++ jest.config.ts | 7 ++--- package.json | 6 ++--- tests/java/io/Unicode.spec.ts | 6 ++--- tests/java/lang/Appendable.spec.ts | 6 ++--- tests/java/util/List.spec.ts | 2 +- tests/test-helpers.ts | 42 ------------------------------ tests/tsconfig.json | 4 +-- 8 files changed, 18 insertions(+), 57 deletions(-) delete mode 100644 tests/test-helpers.ts diff --git a/.vscode/launch.json b/.vscode/launch.json index b9f2da6..1d18501 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,6 +9,8 @@ "runtimeArgs": [ "--experimental-vm-modules", "--no-warnings", + "--loader", + "ts-node/esm", "${workspaceRoot}/node_modules/.bin/jest", "${file}", "--no-coverage", diff --git a/jest.config.ts b/jest.config.ts index c7f55dd..f252469 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -108,7 +108,7 @@ const config: Config = { "node", ], - //extensionsToTreatAsEsm: [".ts"], + extensionsToTreatAsEsm: [".ts"], // A map from regular expressions to module names or to arrays of module names that allow to stub out resources // with a single module @@ -127,7 +127,7 @@ const config: Config = { // notifyMode: "failure-change", // A preset that is used as a base for Jest's configuration - preset: "ts-jest", + //preset: "ts-jest/presets/default-esm", // Run tests from one or more projects // projects: undefined, @@ -152,7 +152,7 @@ const config: Config = { // A list of paths to directories that Jest should use to search for files in roots: [ - "tests", + "./tests", ], // Allows you to use a custom runner instead of Jest's default test runner @@ -210,6 +210,7 @@ const config: Config = { transform: { "^.+\\.ts$": ["ts-jest", { tsconfig: "./tests/tsconfig.json", + useESM: true, }], }, diff --git a/package.json b/package.json index b092e55..982cedd 100644 --- a/package.json +++ b/package.json @@ -42,9 +42,9 @@ "prepublishOnly": "npm run build && npm run test", "hello-world": "ts-node src/runner ./examples/HelloWorld.ts", "build": "tsc", - "test": "jest --testMatch [ \"**/tests/**/*.spec.ts\" ] --no-coverage --watchAll=false --max-worker=1", - "test-ci": "jest --testMatch [ \"**/tests/**/*.spec.ts\" ] --no-coverage --watchAll=false --silent", - "test-coverage": "jest --testMatch [ \"**/tests/**/*.spec.ts\" ] --coverage --silent", + "test": "jest --testMatch [ \"**/tests/**/*.spec.ts\" ] --no-coverage --watchAll=false --max-worker=10", + "test-ci": "npm run test -- --silent", + "test-coverage": "npm run test -- --coverage --silent", "lint": "eslint \"./src/**/*.ts\"" } } diff --git a/tests/java/io/Unicode.spec.ts b/tests/java/io/Unicode.spec.ts index 27bce90..73786c7 100644 --- a/tests/java/io/Unicode.spec.ts +++ b/tests/java/io/Unicode.spec.ts @@ -4,12 +4,12 @@ */ import { S } from "../../../src/templates.js"; -import { runTest } from "../../test-helpers.js"; +import { Unicode } from "../../jdk/java/io/Unicode.js"; describe("Unicode Tests", () => { - xit("Base", async () => { + xit("Base", () => { // For now disabled, as we need a text encoder with more than just UTF-8. - await runTest("tests/jdk/java/io/Unicode.ts", "main", [[S`utf-16le`, S`little`, S`false`]]); + Unicode.main([S`utf-16le`, S`little`, S`false`]); }); }); diff --git a/tests/java/lang/Appendable.spec.ts b/tests/java/lang/Appendable.spec.ts index c616b37..0718229 100644 --- a/tests/java/lang/Appendable.spec.ts +++ b/tests/java/lang/Appendable.spec.ts @@ -3,10 +3,10 @@ * Licensed under the MIT License. See License.txt in the project root for license information. */ -import { runTest } from "../../test-helpers.js"; +import { Basic } from "../../jdk/java/lang/Appendable/Basic.js"; describe("Tests", () => { - it("Basic", async () => { - await runTest("tests/jdk/java/lang/Appendable/Basic.ts", "main"); + it("Basic", () => { + Basic.main([]); }); }); diff --git a/tests/java/util/List.spec.ts b/tests/java/util/List.spec.ts index bba6490..391af2d 100644 --- a/tests/java/util/List.spec.ts +++ b/tests/java/util/List.spec.ts @@ -14,7 +14,7 @@ describe("List Tests", () => { testNG.run(ListDefaults); }); - describe.only("JDK ListFactories", () => { + describe("JDK ListFactories", () => { const testNG = new TestNG(); testNG.run(ListFactories); }); diff --git a/tests/test-helpers.ts b/tests/test-helpers.ts deleted file mode 100644 index 30913d7..0000000 --- a/tests/test-helpers.ts +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) Mike Lischke. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - */ - -import * as path from "path"; - -import { JavaString } from "../src/java/lang/String.js"; - -/** - * Loads the given file and runs the entry method. - * - * @param fileName The file to load. - * @param entry The entry method to run. - * @param parameters The parameters to pass to the entry method. This is an array of arrays, where each - * sub-array contains the parameters for one call. - * - * @throws Error if the file does not contain exactly one class or the entry method is not found. - * @throws Error if the entry method throws an error. - */ -export const runTest = async (fileName: string, entry: string, parameters?: JavaString[][]): Promise => { - // Load the given file and check if it contains a class with the entry method. - const module = await import(path.resolve(fileName)) as { [key: string]: { [key: string]: Function; }; }; - const keys = Object.keys(module); - if (keys.length !== 1) { - throw new Error("File must contain exactly one exported class."); - } - - const clazz = module[keys[0]] as { [key: string]: Function; }; - if (clazz[entry] instanceof Function) { - // Run the entry method. - if (parameters) { - for (const parameter of parameters) { - await clazz[entry](parameter); - } - } else { - clazz[entry](); - } - } else { - throw new Error(`No method ${entry} found in file ${fileName}.`); - } -}; diff --git a/tests/tsconfig.json b/tests/tsconfig.json index 47351b0..d043d4a 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -1,8 +1,8 @@ { "compilerOptions": { - "moduleResolution": "Node", + "moduleResolution": "Node16", "target": "ES2022", - "module": "CommonJS", + "module": "Node16", "noEmit": true, "declaration": true, "sourceMap": true,