Files
languageservices/languageservice/src/hover.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

203 lines
6.2 KiB
TypeScript

import {isString} from "@actions/workflow-parser";
import {DescriptionProvider, hover, HoverConfig} from "./hover.js";
import {getPositionFromCursor} from "./test-utils/cursor-position.js";
import {testFileProvider} from "./test-utils/test-file-provider.js";
import {clearCache} from "./utils/workflow-cache.js";
export function testHoverConfig(tokenValue: string, tokenKey: string, description?: string) {
return {
descriptionProvider: {
getDescription: (_, token) => {
if (!isString(token)) {
throw new Error("Test provider only supports string tokens");
}
expect(token.value).toEqual(tokenValue);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(token.definition!.key).toEqual(tokenKey);
return Promise.resolve(description);
}
} satisfies DescriptionProvider,
fileProvider: testFileProvider
} satisfies HoverConfig;
}
beforeEach(() => {
clearCache();
});
describe("hover", () => {
it("on a key", async () => {
const input = `o|n: push
jobs:
build:
runs-on: [self-hosted]`;
const result = await hover(...getPositionFromCursor(input));
expect(result).not.toBeUndefined();
expect(result?.contents).toContain("The GitHub event that triggers the workflow.");
});
it("on a value", async () => {
const input = `on: pu|sh
jobs:
build:
runs-on: [self-hosted]`;
const result = await hover(...getPositionFromCursor(input));
expect(result).not.toBeUndefined();
expect(result?.contents).toEqual("Runs your workflow when you push a commit or tag.");
});
it("on a parameter with a description", async () => {
const input = `on: push
jobs:
build:
co|ntinue-on-error: false`;
const result = await hover(...getPositionFromCursor(input));
expect(result).not.toBeUndefined();
expect(result?.contents).toEqual(
"Prevents a workflow run from failing when a job fails. Set to true to allow a workflow run to pass when this job fails.\n\n" +
"Available expression contexts: `github`, `inputs`, `vars`, `needs`, `strategy`, `matrix`"
);
});
it("on a parameter with its own type", async () => {
const input = `on: push
jobs:
build:
pe|rmissions: read-all`;
const result = await hover(...getPositionFromCursor(input));
expect(result).not.toBeUndefined();
expect(result?.contents).toContain(
"You can use `permissions` to modify the default permissions granted to the `GITHUB_TOKEN`"
);
});
it("property values are not overwritten", async () => {
const input1 = `on: push
jobs:
build:
ti|meout-minutes: 10
cancel-timeout-minutes: 10`;
const result1 = await hover(...getPositionFromCursor(input1));
expect(result1).not.toBeUndefined();
const input2 = `on: push
jobs:
build:
timeout-minutes: 10
ca|ncel-timeout-minutes: 10`;
const result2 = await hover(...getPositionFromCursor(input2));
expect(result2).not.toBeUndefined();
expect(result1?.contents).not.toEqual(result2?.contents);
});
it("on a value in a sequence", async () => {
const input = `on: [pull_request,
pu|sh]
jobs:
build:
runs-on: [self-hosted]`;
const result = await hover(...getPositionFromCursor(input));
expect(result).not.toBeUndefined();
expect(result?.contents).toEqual("Runs your workflow when you push a commit or tag.");
});
it("on a cron schedule", async () => {
const input = `on:
schedule:
- cron: '0,30 0|,12 * * *'
`;
const result = await hover(...getPositionFromCursor(input));
expect(result).not.toBeUndefined();
// Cron description is now shown via diagnostics, not hover
expect(result?.contents).toEqual("");
});
it("on a cron mapping key", async () => {
const input = `on:
schedule:
- c|ron: '0 0 * * *'
`;
const result = await hover(...getPositionFromCursor(input));
expect(result).not.toBeUndefined();
expect(result?.contents).toEqual("");
});
it("on an invalid cron schedule", async () => {
const input = `on:
schedule:
- cron: '0 0 |* * * * *'
`;
const result = await hover(...getPositionFromCursor(input));
expect(result).not.toBeUndefined();
expect(result?.contents).toEqual("");
});
it("shows context inherited from parent nodes", async () => {
const input = `
on: push
jobs:
build:
runs-on: [self-hosted]
steps:
- uses: actions/checkout@v2
with:
ref|: main
`;
const result = await hover(...getPositionFromCursor(input));
expect(result).not.toBeUndefined();
// The `ref` is a `string` definition and inherits the context from `step-with`
const expected =
"Available expression contexts: `github`, `inputs`, `vars`, `needs`, `strategy`, `matrix`, `secrets`, `steps`, `job`, `runner`, `env`\n\n" +
"Available expression functions: `hashFiles`";
expect(result?.contents).toEqual(expected);
});
});
describe("hover with description provider", () => {
it("uses the description provider", async () => {
const input = `
on: push
jobs:
build:
runs-on: [self-hosted]
steps:
- uses: actions/checkout@v2
with:
ref|: main
`;
const result = await hover(
...getPositionFromCursor(input),
testHoverConfig("ref", "string", "The branch, tag or SHA to checkout.")
);
expect(result).not.toBeUndefined();
const expected =
"The branch, tag or SHA to checkout.\n\n" +
"Available expression contexts: `github`, `inputs`, `vars`, `needs`, `strategy`, `matrix`, `secrets`, `steps`, `job`, `runner`, `env`\n\n" +
"Available expression functions: `hashFiles`";
expect(result?.contents).toEqual(expected);
});
it("falls back to the token description", async () => {
const input = `
on: push
jobs:
build:
runs-on: [self-hosted]
steps:
- uses|: actions/checkout@v2
`;
const result = await hover(...getPositionFromCursor(input), testHoverConfig("uses", "step-uses", undefined));
expect(result).not.toBeUndefined();
expect(result?.contents).toEqual(
"Selects an action to run as part of a step in your job. An action is a reusable unit of code. You can use an action defined in the same repository as the workflow, a public repository, or in a published Docker container image."
);
});
});