Skip to content

Commit

Permalink
chore: added an assortment of unit tests (#205)
Browse files Browse the repository at this point in the history
## PR Checklist

- [x] Addresses an existing open issue: starts on #7, #8, #9, #10, #11,
#12, #13, #14
- [x] That issue was marked as [`status: accepting
prs`](https://github.com/JoshuaKGoldberg/ts-api-utils/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22)
- [x] Steps in
[CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/ts-api-utils/blob/main/.github/CONTRIBUTING.md)
were taken

## Overview

I had some time on a road trip today and was motivated to backfill some
tests 馃槃. Doesn't add in 100% coverage on any file or really finish any
of the linked issues. Just raises test coverage across the board in
general.

Co-authored-by: Rebecca Stevens <rebecca.stevens@outlook.co.nz>
  • Loading branch information
JoshuaKGoldberg and RebeccaStevens committed Jun 15, 2023
1 parent 4ac4813 commit e11775e
Show file tree
Hide file tree
Showing 9 changed files with 822 additions and 32 deletions.
52 changes: 52 additions & 0 deletions src/comments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import ts from "typescript";
import { describe, expect, it, vitest } from "vitest";

import { forEachComment } from "./comments";
import { createNodeAndSourceFile } from "./test/utils";
import { isTsVersionAtLeast } from "./utils";

describe("forEachComment", () => {
it("does not call the callback when the source has no comments", () => {
const { node, sourceFile } = createNodeAndSourceFile("let value;");
const callback = vitest.fn();

forEachComment(node, callback, sourceFile);

expect(callback).not.toHaveBeenCalled();
});

if (isTsVersionAtLeast(4, 3)) {
it("calls the callback when the source has a leading comment", () => {
const { node, sourceFile } = createNodeAndSourceFile(`
// hello world
let value;
`);
const callback = vitest.fn();

forEachComment(node, callback, sourceFile);

expect(callback).toHaveBeenCalledWith(
sourceFile.getFullText(),
expect.objectContaining({
kind: ts.SyntaxKind.SingleLineCommentTrivia,
})
);
});

it("calls the callback when the source has a trailing comment", () => {
const { node, sourceFile } = createNodeAndSourceFile(`
let value; // hello world
`);
const callback = vitest.fn();

forEachComment(node, callback, sourceFile);

expect(callback).toHaveBeenCalledWith(
sourceFile.getFullText(),
expect.objectContaining({
kind: ts.SyntaxKind.SingleLineCommentTrivia,
})
);
});
}
});
93 changes: 78 additions & 15 deletions src/nodes/typeGuards/compound.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,14 @@ import { describe, expect, it } from "vitest";
import { createNode } from "../../test/utils";
import {
isConstAssertionExpression,
isIterationStatement,
isNamedDeclarationWithName,
isNamespaceDeclaration,
isNumericOrStringLikeLiteral,
isSuperElementAccessExpression,
isSuperPropertyAccessExpression,
} from "./compound";

describe("isNumericOrStringLikeLiteral", () => {
it.each([
[false, "an identifier", ts.factory.createIdentifier("abc")],
[
true,
"a no substitution template literal",
ts.factory.createNoSubstitutionTemplateLiteral("abc"),
],
[true, "a numeric literal", ts.factory.createNumericLiteral("123")],
[true, "a string literal", ts.factory.createStringLiteral("abc")],
])("returns %j when given %s", (expected, _, node) => {
expect(isNumericOrStringLikeLiteral(node)).toBe(expected);
});
});

describe("isConstAssertionExpression", () => {
it.each([
[
Expand Down Expand Up @@ -65,3 +55,76 @@ describe("isConstAssertionExpression", () => {
expect(isConstAssertionExpression(createNode(node))).toBe(expected);
});
});

describe("isIterationStatement", () => {
it.each([
[true, "do {} while (0)"],
[true, "for (;;) {}"],
[true, "for (const _ in {}) {}"],
[true, "for (const _ of []) {}"],
[true, "while (0) {}"],
[false, "let _"],
])("returns %j when given %s", (expected, code) => {
expect(isIterationStatement(createNode(code))).toBe(expected);
});
});

describe("isNamedDeclarationWithName", () => {
it.each([
[true, "class HasName {}"],
[false, "const _ = class {}"],
[false, "let _"],
])("returns %j when given %s", (expected, code) => {
expect(isNamedDeclarationWithName(createNode<ts.Declaration>(code))).toBe(
expected
);
});
});

describe("isNamespaceDeclaration", () => {
it.each([
[true, "namespace A {}"],
[true, "namespace A.B {}"],
[false, "let _"],
])("returns %j when given %s", (expected, code) => {
expect(isNamespaceDeclaration(createNode(code))).toBe(expected);
});
});

describe("isNumericOrStringLikeLiteral", () => {
it.each([
[false, "an identifier", ts.factory.createIdentifier("abc")],
[
true,
"a no substitution template literal",
ts.factory.createNoSubstitutionTemplateLiteral("abc"),
],
[true, "a numeric literal", ts.factory.createNumericLiteral("123")],
[true, "a string literal", ts.factory.createStringLiteral("abc")],
])("returns %j when given %s", (expected, _, node) => {
expect(isNumericOrStringLikeLiteral(node)).toBe(expected);
});
});

describe("isSuperElementAccessExpression", () => {
it.each([
[true, "super['property']"],
[false, "super.property"],
[false, "super.method()"],
[false, "super()"],
[false, "other()"],
])("returns %j when given %s", (expected, code) => {
expect(isSuperElementAccessExpression(createNode(code))).toBe(expected);
});
});

describe("isSuperPropertyAccessExpression", () => {
it.each([
[true, "super.property"],
[false, "super.method()"],
[false, "super()"],
[false, "other()"],
])("returns %j when given %s", (expected, code) => {
expect(isSuperPropertyAccessExpression(createNode(code))).toBe(expected);
});
});
6 changes: 3 additions & 3 deletions src/nodes/typeGuards/compound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ export function isIterationStatement(
node: ts.Node
): node is ts.IterationStatement {
switch (node.kind) {
case ts.SyntaxKind.ForStatement:
case ts.SyntaxKind.ForOfStatement:
case ts.SyntaxKind.DoStatement:
case ts.SyntaxKind.ForInStatement:
case ts.SyntaxKind.ForOfStatement:
case ts.SyntaxKind.ForStatement:
case ts.SyntaxKind.WhileStatement:
case ts.SyntaxKind.DoStatement:
return true;
default:
return false;
Expand Down

0 comments on commit e11775e

Please sign in to comment.