types-testing
Version:
Test TypeScript types at test runner runtime - Works seamlessly with Jest, Vitest, and Bun.
143 lines (142 loc) • 5.03 kB
JavaScript
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __typeError = (msg) => {
throw TypeError(msg);
};
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
var _options, _compileResult, _assertions;
import { Assertion, Compiler } from "../../lib/index.js";
import { TypesTestingError } from "./__internal/TypesTestingError.js";
class TypesTesting {
/**
* Creates a new types-testing instance.
*
* @param options Options for types-testing instance.
*/
constructor(options) {
__privateAdd(this, _options);
__privateAdd(this, _compileResult);
__privateAdd(this, _assertions);
var _a;
this.prepare = this.prepare.bind(this);
this.reset = this.reset.bind(this);
this.expectType = this.expectType.bind(this);
__privateSet(this, _options, {
autoPrepare: (_a = options.autoPrepare) != null ? _a : true,
basePath: options.basePath,
tsConfig: options.tsConfig,
files: options.files,
compilerOptions: options.compilerOptions,
projectReferences: options.projectReferences
});
__privateSet(this, _assertions, new Proxy(Assertion, {
get: () => {
const assertionFn = () => {
if (!__privateGet(this, _compileResult)) {
return;
}
const errors = __privateGet(this, _compileResult).errors;
new TypesTestingError(errors, (key, TypesTestingError2) => {
errors.delete(key);
throw TypesTestingError2;
});
};
return assertionFn;
}
}));
}
/**
* Checks if the types-testing instance is ready (prepared).
*/
get isPrepared() {
return __privateGet(this, _compileResult) !== void 0;
}
/**
* Gets the current configuration options.
*/
get options() {
return __privateGet(this, _options);
}
/**
* Prepares the types-testing instance.
*
* @param options Optional options to override instance options.
* @throws If the `tsconfig.json` file can't be found.
*/
prepare(options) {
if (this.isPrepared) {
return this;
}
__privateSet(this, _compileResult, Compiler.compile(__spreadValues(__spreadValues({}, __privateGet(this, _options)), options)));
return this;
}
/**
* Reset the types-testing instance.
*
* Will do nothing if the types-testing instance is not prepared.
*
* @param runPrepare Optional boolean flag to specify whether to run {@link prepare} after reset.
*/
reset(runPrepare) {
if (!this.isPrepared) {
return;
}
__privateSet(this, _compileResult, void 0);
if (runPrepare) {
this.prepare();
}
}
/**
* Returns assertion functions for checking types.
*
* @template Received The received type.
* @param received The received value (only the value type will be used).
* @returns Set of assertions object.
* @throws If the tester isn't prepared (i.e., `prepare()` hasn't been called yet).
*/
expectType(received) {
const notPrepared = !this.isPrepared;
if (notPrepared) {
const { autoPrepare } = this.options;
if (autoPrepare) {
this.prepare();
} else {
throw new Error("need to run prepare first.");
}
}
const errors = __privateGet(this, _compileResult).errors;
new TypesTestingError(errors, (key, TypesTestingError2) => {
errors.delete(key);
throw TypesTestingError2;
});
return __spreadProps(__spreadValues({}, __privateGet(this, _assertions)), {
not: __spreadValues({}, __privateGet(this, _assertions))
});
}
}
_options = new WeakMap();
_compileResult = new WeakMap();
_assertions = new WeakMap();
export {
TypesTesting
};