types-testing
Version:
Test TypeScript types at test runner runtime - Works seamlessly with Jest, Vitest, and Bun.
82 lines (81 loc) • 2.72 kB
JavaScript
import { createErrorKey } from "./create-error-key.js";
import { createErrorValue } from "./create-error-value.js";
import { createProgram } from "./create-program.js";
import { getTypeChecker } from "./get-type-checker.js";
import { traverseSourceFile } from "./traverse-source-file.js";
import { validateAssertion } from "./validate-assertion.js";
const compile = (options) => {
const program = createProgram(options);
const config = program.__internal;
const checker = getTypeChecker(program);
const { identifyTestCall, postIdentifyTestCall } = checker.__internal;
const setErrorValue = (...args) => {
const value = createErrorValue(...args);
const key = createErrorKey(value.filePath, value.line, value.column);
result.errors.set(key, value);
};
const result = {
files: config.files,
options: config.options,
errors: /* @__PURE__ */ new Map()
};
for (const fileName of config.files) {
const sourceFile = program.getSourceFile(fileName);
if (!sourceFile) {
continue;
}
traverseSourceFile(sourceFile, (node) => {
const testCall = identifyTestCall(node);
if (testCall === null) {
return;
}
if (testCall.type === "expect") {
if (testCall.props.receivedType === void 0) {
const { callerNode, expectCallExpressionText } = postIdentifyTestCall(
node,
true
);
setErrorValue(sourceFile, callerNode, expectCallExpressionText);
}
return;
}
if (testCall.type === "assertion") {
if (testCall.props.receivedType === void 0) {
const { callerNode, expectCallExpressionText } = postIdentifyTestCall(
node,
false
);
setErrorValue(sourceFile, callerNode, expectCallExpressionText);
return;
}
const isValidAssertion = validateAssertion(testCall.props.assertionFn, {
checker,
receivedType: testCall.props.receivedType,
expectedType: testCall.props.expectedType,
isNegated: testCall.props.isNegated
});
if (!isValidAssertion) {
const {
callerNode,
expectCallExpressionText,
assertionCallExpressionText
} = postIdentifyTestCall(node, false);
setErrorValue(
sourceFile,
callerNode,
expectCallExpressionText,
assertionCallExpressionText,
testCall.props.receivedTypeString,
testCall.props.expectedTypeString,
testCall.props.isNegated,
testCall.props.needTypeArgument
);
}
}
});
}
return result;
};
export {
compile
};