Skip to content

Commit

Permalink
Convert analyzer template test to node:test (#4537)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinfagnani committed Feb 7, 2024
1 parent d128391 commit 59d5ba7
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 66 deletions.
2 changes: 2 additions & 0 deletions .changeset/rare-pianos-draw.md
@@ -0,0 +1,2 @@
---
---
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Expand Up @@ -19,7 +19,7 @@ jobs:

- uses: actions/setup-node@v3
with:
node-version: 20
node-version: 21
cache: 'npm'
cache-dependency-path: package-lock.json

Expand All @@ -43,7 +43,7 @@ jobs:

- uses: actions/setup-node@v3
with:
node-version: 20
node-version: 21
cache: 'npm'
cache-dependency-path: package-lock.json

Expand Down Expand Up @@ -74,7 +74,7 @@ jobs:

- uses: actions/setup-node@v3
with:
node-version: 18
node-version: 21
cache: 'npm'
cache-dependency-path: package-lock.json

Expand Down
26 changes: 23 additions & 3 deletions packages/labs/analyzer/package.json
Expand Up @@ -19,11 +19,16 @@
"build": "wireit",
"test": "wireit",
"test:server": "wireit",
"test:server:uvu": "wireit",
"test:server:node": "wireit",
"test:browser": "wireit"
},
"wireit": {
"build": {
"command": "tsc --build --pretty",
"dependencies": [
"../../lit:build"
],
"files": [
"src/**/*.ts",
"tsconfig.json"
Expand Down Expand Up @@ -54,14 +59,29 @@
]
},
"test:server": {
"dependencies": [
"test:server:node",
"test:server:uvu"
]
},
"test:server:uvu": {
"#comment": "The quotes around the file regex must be double quotes on windows!",
"command": "uvu test/server \"_test\\.js$\"",
"command": "uvu test/server \"_test\\.js$\" -i \"lit-html/\"",
"env": {
"NODE_OPTIONS": "--enable-source-maps"
},
"dependencies": [
"build",
"../../lit:build"
"build"
],
"files": [
"test-files/"
],
"output": []
},
"test:server:node": {
"command": "node --enable-source-maps --test-reporter=spec --test test/server/lit-html/*_test.js",
"dependencies": [
"build"
],
"files": [
"test-files/"
Expand Down
76 changes: 35 additions & 41 deletions packages/labs/analyzer/src/test/server/lit-html/template_test.ts
Expand Up @@ -4,53 +4,47 @@
* SPDX-License-Identifier: BSD-3-Clause
*/

import {suite} from 'uvu';
// eslint-disable-next-line import/extensions
import * as assert from 'uvu/assert';
import {test, describe as suite} from 'node:test';
import * as assert from 'node:assert';
import type ts from 'typescript';

import {
AnalyzerTestContext,
languages,
setupAnalyzerForTest,
} from '../utils.js';
import {languages, setupAnalyzerForNodeTest} from '../utils.js';
import {ClassDeclaration} from '../../../lib/model.js';
import {isLitTaggedTemplateExpression} from '../../../lib/lit-html/template.js';

for (const lang of languages) {
const test = suite<AnalyzerTestContext>(`lit-html tests (${lang})`);

test.before((ctx) => {
setupAnalyzerForTest(ctx, lang, 'basic-elements');
});

test('isLitHtmlTemplateTag', ({getModule, analyzer, typescript}) => {
const elementAModule = getModule('element-a')!;
const decl = elementAModule.declarations[0];

// get to the lit-html template tag
const renderMethod = (decl as ClassDeclaration).getMethod('render')!;
const statement = renderMethod.node.body!.statements[0];

assert.is(typescript.isReturnStatement(statement), true);
const returnStatement = statement as ts.ReturnStatement;
assert.ok(returnStatement.expression);
assert.is(
typescript.isTaggedTemplateExpression(returnStatement.expression),
true
);
const expression =
returnStatement.expression as ts.TaggedTemplateExpression;
assert.is(typescript.isIdentifier(expression.tag), true);
assert.is(
isLitTaggedTemplateExpression(
expression,
analyzer.typescript,
analyzer.program.getTypeChecker()
),
true
suite(`lit-html template utility tests (${lang})`, () => {
const {getModule, analyzer, typescript} = setupAnalyzerForNodeTest(
lang,
'basic-elements'
);
});

test.run();
test('isLitHtmlTemplateTag', () => {
const elementAModule = getModule('element-a')!;
const decl = elementAModule.declarations[0];

// get to the lit-html template tag
const renderMethod = (decl as ClassDeclaration).getMethod('render')!;
const statement = renderMethod.node.body!.statements[0];

assert.equal(typescript.isReturnStatement(statement), true);
const returnStatement = statement as ts.ReturnStatement;
assert.ok(returnStatement.expression);
assert.equal(
typescript.isTaggedTemplateExpression(returnStatement.expression),
true
);
const expression =
returnStatement.expression as ts.TaggedTemplateExpression;
assert.equal(typescript.isIdentifier(expression.tag), true);
assert.equal(
isLitTaggedTemplateExpression(
expression,
analyzer.typescript,
analyzer.program.getTypeChecker()
),
true
);
});
});
}
44 changes: 25 additions & 19 deletions packages/labs/analyzer/src/test/server/utils.ts
Expand Up @@ -217,25 +217,7 @@ export const setupAnalyzerForTest = (
pkg: string
) => {
try {
const packagePath = fileURLToPath(
new URL(`../../test-files/${lang}/${pkg}`, import.meta.url).href
) as AbsolutePath;
const analyzer = createPackageAnalyzer(packagePath);
const diagnostics = [...analyzer.getDiagnostics()];
if (diagnostics.length > 0) {
throw makeDiagnosticError(diagnostics);
}
const getModule = (name: string) =>
analyzer.getModule(
getSourceFilename(
analyzer.path.join(packagePath, name),
lang
) as AbsolutePath
);
ctx.packagePath = packagePath;
ctx.analyzer = analyzer;
ctx.typescript = analyzer.typescript;
ctx.getModule = getModule;
Object.assign(ctx, setupAnalyzerForNodeTest(lang, pkg));
} catch (error) {
// Uvu has a bug where it silently ignores failures in before and after,
// see https://github.com/lukeed/uvu/issues/191.
Expand All @@ -244,6 +226,30 @@ export const setupAnalyzerForTest = (
}
};

export const setupAnalyzerForNodeTest = (lang: Language, pkg: string) => {
const packagePath = fileURLToPath(
new URL(`../../test-files/${lang}/${pkg}`, import.meta.url).href
) as AbsolutePath;
const analyzer = createPackageAnalyzer(packagePath);
const diagnostics = [...analyzer.getDiagnostics()];
if (diagnostics.length > 0) {
throw makeDiagnosticError(diagnostics);
}
const getModule = (name: string) =>
analyzer.getModule(
getSourceFilename(
analyzer.path.join(packagePath, name),
lang
) as AbsolutePath
);
return {
packagePath,
analyzer,
typescript: analyzer.typescript,
getModule,
};
};

export interface AnalyzerModuleTestContext extends AnalyzerTestContext {
module: Module;
}
Expand Down

0 comments on commit 59d5ba7

Please sign in to comment.