Files
languageservices/workflow-parser/src/xlang.test.ts
T
eric sciple 656a821a94 ESM migration: Add .js extensions for node16 moduleResolution (#257)
Migrate expressions, workflow-parser, and languageservice packages to use
proper ESM imports with .js extensions that work with node16 moduleResolution.

Changes:
- Update tsconfig.build.json in each package to use module: node16 and
  moduleResolution: node16
- Add .js extensions to all relative import paths (Option B approach)
- Fix yaml internal type imports in workflow-parser by defining local types
- Add skipLibCheck to handle @types/node compatibility issues
- Add TypeScript 5.8.3 override in root package.json
- Add ESM migration plan documentation

The languageserver package is deferred due to test hang issues that need
further investigation.

Related #154 - Upgrade moduleResolution from node to node16 or nodenext
Related #110 - Published ESM code has imports without file extensions
Related #64 - expressions: ERR_MODULE_NOT_FOUND attempting to run example
Related #146 - Can not import @actions/workflow-parser

Test results:
- expressions: 1068 tests passed
- workflow-parser: 292 tests passed
- languageservice: 452 tests passed

* docs: update ESM migration plan with findings

- Update languageserver blocker: vscode-languageserver v8.0.2 lacks ESM
  exports (not a test hang issue)
- Document that Option B (manual .js extensions) was chosen over Option A
  due to ts-jest compatibility issues
- Add workaround for yaml package internal types (LinePos, NodeBase)
- Update migration status table with accurate reason for deferral
- Add skipLibCheck note for @types/node compatibility
2025-12-18 13:35:48 -06:00

126 lines
4.1 KiB
TypeScript

import * as fs from "fs";
import * as path from "path";
import * as YAML from "yaml";
import {convertWorkflowTemplate} from "./model/convert.js";
import {NoOperationTraceWriter} from "./templates/trace-writer.js";
import {File} from "./workflows/file.js";
import {FileProvider} from "./workflows/file-provider.js";
import {fileIdentifier, FileReference} from "./workflows/file-reference.js";
import {parseWorkflow} from "./workflows/workflow-parser.js";
interface TestOptions {
"include-source"?: boolean;
skip?: string[];
}
const nullTrace = new NoOperationTraceWriter();
const testFiles = "./testdata/reader";
const skippedTestsFile = "./testdata/skipped-tests.txt";
describe("x-lang tests", () => {
const files = fs.readdirSync(testFiles);
const skippedTests = new Set(fs.readFileSync(skippedTestsFile, "utf8").split(/\n/));
for (const file of files) {
const fileName = path.join(testFiles, file);
const fileStat = fs.statSync(fileName);
if (fileStat.isDirectory()) {
throw new Error("sub-directories are not supported");
}
if (path.extname(fileName) !== ".yml" && path.extname(fileName) !== ".yaml") {
throw new Error("only y(a)ml files are supported " + file);
}
const inputFile = fs.readFileSync(fileName, "utf8");
const testDocs: string[] = inputFile.split(/\r?\n---\r?\n/);
expect(testDocs.length).toBeGreaterThanOrEqual(3);
const testOptions = YAML.parse(testDocs[0]) as TestOptions;
const unsupportedTest = skippedTests.has(file);
const test = async () => {
const testFileName = ".github/workflows" + fileName.substring(fileName.lastIndexOf("/"));
const testInput = testDocs[1];
const expectedTemplate = testDocs[testDocs.length - 1].trim();
// For reusable workflow tests, additional workflows are passed in as pairs of
// file names and file contents
const reusableWorkflows: Record<string, File> = {};
if (fileName.indexOf("reusable") !== -1) {
for (let i = 2; i < testDocs.length - 1; i = i + 2) {
reusableWorkflows[testDocs[i]] = {
name: testDocs[i],
content: testDocs[i + 1]
};
}
}
const testFileProvider: FileProvider = {
getFileContent: (ref: FileReference) => {
const file = reusableWorkflows[fileIdentifier(ref)];
if (file) {
return Promise.resolve(file);
}
throw new Error("File not found: " + fileName);
}
};
const parseResult = parseWorkflow(
{
name: testFileName,
content: testInput
},
nullTrace
);
expect(parseResult.value).not.toBeUndefined();
const workflowTemplate = await convertWorkflowTemplate(
parseResult.context,
parseResult.value!, // eslint-disable-line @typescript-eslint/no-non-null-assertion
testFileProvider,
{
fetchReusableWorkflowDepth: 1
}
);
// Unless this tests is only used by TypeScript, remove the events for now.
// TODO: Remove this once we parse events everywhere
const includeEvents =
testOptions.skip !== undefined && contains(testOptions.skip, "Go") && contains(testOptions.skip, "C#");
if (!includeEvents) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
delete (workflowTemplate as any).events;
}
// Other parsers don't have a partial template when there are errors
if (workflowTemplate.errors) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
delete (workflowTemplate as any).jobs;
}
const result = JSON.stringify(workflowTemplate, null, " ");
expect(result).toBe(expectedTemplate);
};
if (unsupportedTest) {
it.failing(fileName, test);
} else {
it(fileName, test);
}
}
});
// Case-insensitive contains
function contains(arr: string[] | undefined, term: string): boolean {
if (!arr) {
return false;
}
return arr.map(x => x.toLowerCase()).indexOf(term.toLowerCase()) !== -1;
}