Skip to content

Commit

Permalink
test: test properties both directly and indirectly on a type
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed Feb 21, 2023
1 parent 97bc0d0 commit 7c35092
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/types/getters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import * as ts from "typescript";
import { describe, expect, it } from "vitest";

import { createSourceFileAndTypeChecker } from "../test/utils.js";
import { isTsVersionAtLeast } from "../utils.js";
import { getWellKnownSymbolPropertyOfType } from "./getters.js";

describe("getWellKnownSymbolPropertyOfType", () => {
// /~https://github.com/JoshuaKGoldberg/ts-api-tools/issues/15
it("gets the property when it does not have a value declaration", () => {
it("finds the symbol for an `asyncIterator` property", () => {
const { sourceFile, typeChecker } = createSourceFileAndTypeChecker(`
declare const x: {
[Symbol.asyncIterator](): AsyncIterator<any>;
}
`);
declare const x: {
[Symbol.asyncIterator](): AsyncIterator<any>;
}>
`);

const node = (sourceFile.statements[0] as ts.VariableStatement)
const node = (sourceFile.statements[0] as ts.VariaybleStatement)
.declarationList.declarations[0].name;

const type = typeChecker.getTypeAtLocation(node);
Expand All @@ -24,4 +24,26 @@ describe("getWellKnownSymbolPropertyOfType", () => {
name: /^__@asyncIterator/,
});
});

if (isTsVersionAtLeast(4, 3)) {
// /~https://github.com/JoshuaKGoldberg/ts-api-tools/issues/15
it("finds the symbol for an `asyncIterator` property when that property isn't directly on the type", () => {
const { sourceFile, typeChecker } = createSourceFileAndTypeChecker(`
declare const x: Omit<{
[Symbol.asyncIterator](): AsyncIterator<any>;
}, 'z'>
`);

const node = (sourceFile.statements[0] as ts.VariaybleStatement)
.declarationList.declarations[0].name;

const type = typeChecker.getTypeAtLocation(node);

expect(
getWellKnownSymbolPropertyOfType(type, "asyncIterator", typeChecker)
).toMatchObject({
name: /^__@asyncIterator/,
});
});
}
});

0 comments on commit 7c35092

Please sign in to comment.