tstyche 4.0.0-beta.1 → 4.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -18,14 +18,15 @@ If you are used to test JavaScript, a simple type test file should look familiar
18
18
  ```ts
19
19
  import { expect, test } from "tstyche";
20
20
 
21
- function firstItem<T>(target: Array<T>): T | undefined {
22
- return target[0];
21
+ function isSameLength<T extends { length: number }>(a: T, b: T) {
22
+ return a.length === b.length;
23
23
  }
24
24
 
25
- test("firstItem", () => {
26
- expect(firstItem(["a", "b", "c"])).type.toBe<string | undefined>();
25
+ test("isSameLength", () => {
26
+ expect(isSameLength([1, 2], [1, 2, 3])).type.toBe<boolean>();
27
+ expect(isSameLength("one", "two")).type.toBe<boolean>();
27
28
 
28
- expect(firstItem()).type.toRaiseError("Expected 1 argument");
29
+ expect(isSameLength).type.not.toBeCallableWith(1, 2);
29
30
  });
30
31
  ```
31
32
 
@@ -59,6 +60,8 @@ Here is the list of all matchers:
59
60
  - `.toBe()`, `.toBeAssignableTo()`, `.toBeAssignableWith()` compare types or types of expression,
60
61
  - `.toAcceptProps()` checks the type of JSX component props,
61
62
  - `.toBeApplicable` ensures that the decorator function can be applied,
63
+ - `.toBeCallableWith()` checks whether a function is callable with the given arguments,
64
+ - `.toBeConstructableWith()` checks whether a class is constructable with the given arguments,
62
65
  - `.toHaveProperty()` looks up keys on an object type,
63
66
  - `.toRaiseError()` captures the message or code of a type error.
64
67
 
package/build/index.cjs CHANGED
@@ -17,3 +17,4 @@ exports.it = noopChain;
17
17
  exports.omit = doNothing;
18
18
  exports.pick = doNothing;
19
19
  exports.test = noopChain;
20
+ exports.when = noopChain;
package/build/index.d.cts CHANGED
@@ -85,15 +85,15 @@ interface Matchers {
85
85
  (target: unknown): void;
86
86
  };
87
87
  /**
88
- * Checks if the source type is identical to the target type.
88
+ * Checks if the source type is the same as the target type.
89
89
  */
90
90
  toBe: {
91
91
  /**
92
- * Checks if the source type is identical to the target type.
92
+ * Checks if the source type is the same as the target type.
93
93
  */
94
94
  <Target>(): void;
95
95
  /**
96
- * Checks if the source type is identical to type of the target expression.
96
+ * Checks if the source type is the same as type of the target expression.
97
97
  */
98
98
  (target: unknown): void;
99
99
  };
@@ -127,6 +127,14 @@ interface Matchers {
127
127
  */
128
128
  (target: unknown): void;
129
129
  };
130
+ /**
131
+ * Checks if the source type is callable with the given arguments.
132
+ */
133
+ toBeCallableWith: (...target: Array<unknown>) => void;
134
+ /**
135
+ * Checks if the source type is constructable with the given arguments.
136
+ */
137
+ toBeConstructableWith: (...target: Array<unknown>) => void;
130
138
  /**
131
139
  * Checks if a property key exists on the source type.
132
140
  */
@@ -261,5 +269,29 @@ declare const test: Test;
261
269
  * Defines a single test.
262
270
  */
263
271
  declare const it: Test;
272
+ interface Actions {
273
+ /**
274
+ * Calls the given function with the provided arguments.
275
+ */
276
+ isCalledWith: (...args: Array<unknown>) => void;
277
+ }
278
+ interface When {
279
+ /**
280
+ * Creates a test plan.
281
+ *
282
+ * @template Target - The type upon which an action is performed.
283
+ */
284
+ <Target>(): Actions;
285
+ /**
286
+ * Creates a test plan.
287
+ *
288
+ * @param target - The expression upon which an action is performed.
289
+ */
290
+ (target: unknown): Actions;
291
+ }
292
+ /**
293
+ * Creates a test plan.
294
+ */
295
+ declare const when: When;
264
296
 
265
- export { describe, expect, it, omit, pick, test };
297
+ export { describe, expect, it, omit, pick, test, when };
package/build/index.d.ts CHANGED
@@ -85,15 +85,15 @@ interface Matchers {
85
85
  (target: unknown): void;
86
86
  };
87
87
  /**
88
- * Checks if the source type is identical to the target type.
88
+ * Checks if the source type is the same as the target type.
89
89
  */
90
90
  toBe: {
91
91
  /**
92
- * Checks if the source type is identical to the target type.
92
+ * Checks if the source type is the same as the target type.
93
93
  */
94
94
  <Target>(): void;
95
95
  /**
96
- * Checks if the source type is identical to type of the target expression.
96
+ * Checks if the source type is the same as type of the target expression.
97
97
  */
98
98
  (target: unknown): void;
99
99
  };
@@ -127,6 +127,14 @@ interface Matchers {
127
127
  */
128
128
  (target: unknown): void;
129
129
  };
130
+ /**
131
+ * Checks if the source type is callable with the given arguments.
132
+ */
133
+ toBeCallableWith: (...target: Array<unknown>) => void;
134
+ /**
135
+ * Checks if the source type is constructable with the given arguments.
136
+ */
137
+ toBeConstructableWith: (...target: Array<unknown>) => void;
130
138
  /**
131
139
  * Checks if a property key exists on the source type.
132
140
  */
@@ -261,5 +269,29 @@ declare const test: Test;
261
269
  * Defines a single test.
262
270
  */
263
271
  declare const it: Test;
272
+ interface Actions {
273
+ /**
274
+ * Calls the given function with the provided arguments.
275
+ */
276
+ isCalledWith: (...args: Array<unknown>) => void;
277
+ }
278
+ interface When {
279
+ /**
280
+ * Creates a test plan.
281
+ *
282
+ * @template Target - The type upon which an action is performed.
283
+ */
284
+ <Target>(): Actions;
285
+ /**
286
+ * Creates a test plan.
287
+ *
288
+ * @param target - The expression upon which an action is performed.
289
+ */
290
+ (target: unknown): Actions;
291
+ }
292
+ /**
293
+ * Creates a test plan.
294
+ */
295
+ declare const when: When;
264
296
 
265
- export { describe, expect, it, omit, pick, test };
297
+ export { describe, expect, it, omit, pick, test, when };
package/build/index.js CHANGED
@@ -9,4 +9,4 @@ const noopChain = new Proxy(doNothing, {
9
9
  },
10
10
  });
11
11
 
12
- export { noopChain as describe, noopChain as expect, noopChain as it, doNothing as omit, doNothing as pick, noopChain as test };
12
+ export { noopChain as describe, noopChain as expect, noopChain as it, doNothing as omit, doNothing as pick, noopChain as test, noopChain as when };
@@ -20,57 +20,11 @@ declare class Cli {
20
20
  run(commandLine: Array<string>, cancellationToken?: CancellationToken): Promise<void>;
21
21
  }
22
22
 
23
- declare enum DiagnosticCategory {
24
- Error = "error",
25
- Warning = "warning"
26
- }
27
-
28
- declare class SourceFile {
29
- #private;
30
- fileName: string;
31
- text: string;
32
- constructor(fileName: string, text: string);
33
- getLineStarts(): Array<number>;
34
- getLineAndCharacterOfPosition(position: number): {
35
- line: number;
36
- character: number;
37
- };
38
- }
39
-
40
- declare class DiagnosticOrigin {
41
- assertion: AssertionNode | undefined;
42
- end: number;
43
- sourceFile: SourceFile | ts.SourceFile;
44
- start: number;
45
- constructor(start: number, end: number, sourceFile: SourceFile | ts.SourceFile, assertion?: AssertionNode);
46
- static fromAssertion(assertion: AssertionNode): DiagnosticOrigin;
47
- static fromNode(node: ts.Node, assertion?: AssertionNode): DiagnosticOrigin;
48
- }
49
-
50
- declare class Diagnostic {
51
- category: DiagnosticCategory;
52
- code: string | undefined;
53
- origin: DiagnosticOrigin | undefined;
54
- related: Array<Diagnostic> | undefined;
55
- text: string | Array<string>;
56
- constructor(text: string | Array<string>, category: DiagnosticCategory, origin?: DiagnosticOrigin);
57
- add(options: {
58
- code?: string | undefined;
59
- related?: Array<Diagnostic> | undefined;
60
- }): this;
61
- static error(text: string | Array<string>, origin?: DiagnosticOrigin): Diagnostic;
62
- extendWith(text: string | Array<string>, origin?: DiagnosticOrigin): Diagnostic;
63
- static fromDiagnostics(diagnostics: Array<ts.Diagnostic>): Array<Diagnostic>;
64
- static toMessageText(chain: ts.DiagnosticMessageChain): Array<string>;
65
- static warning(text: string | Array<string>, origin?: DiagnosticOrigin): Diagnostic;
66
- }
67
-
68
- type DiagnosticsHandler<T extends Diagnostic | Array<Diagnostic> = Diagnostic> = (this: void, diagnostics: T) => void;
69
-
70
23
  declare enum TestTreeNodeBrand {
71
24
  Describe = "describe",
72
25
  Test = "test",
73
- Expect = "expect"
26
+ Expect = "expect",
27
+ When = "when"
74
28
  }
75
29
 
76
30
  declare enum TestTreeNodeFlags {
@@ -81,21 +35,27 @@ declare enum TestTreeNodeFlags {
81
35
  Todo = 8
82
36
  }
83
37
 
38
+ declare class WhenNode extends TestTreeNode {
39
+ actionNode: ts.CallExpression;
40
+ actionNameNode: ts.PropertyAccessExpression;
41
+ abilityDiagnostics: Set<ts.Diagnostic> | undefined;
42
+ target: ts.NodeArray<ts.Expression> | ts.NodeArray<ts.TypeNode>;
43
+ constructor(compiler: typeof ts, brand: TestTreeNodeBrand, node: ts.CallExpression, parent: TestTree | TestTreeNode, flags: TestTreeNodeFlags, actionNode: ts.CallExpression, actionNameNode: ts.PropertyAccessExpression);
44
+ }
45
+
84
46
  declare class TestTreeNode {
85
- #private;
86
47
  brand: TestTreeNodeBrand;
87
- children: Array<TestTreeNode | AssertionNode>;
48
+ children: Array<TestTreeNode | AssertionNode | WhenNode>;
88
49
  diagnostics: Set<ts.Diagnostic>;
89
50
  flags: TestTreeNodeFlags;
90
51
  name: string;
91
52
  node: ts.CallExpression;
92
53
  parent: TestTree | TestTreeNode;
93
54
  constructor(compiler: typeof ts, brand: TestTreeNodeBrand, node: ts.CallExpression, parent: TestTree | TestTreeNode, flags: TestTreeNodeFlags);
94
- validate(): Array<Diagnostic>;
95
55
  }
96
56
 
97
57
  declare class TestTree {
98
- children: Array<TestTreeNode | AssertionNode>;
58
+ children: Array<TestTreeNode | AssertionNode | WhenNode>;
99
59
  diagnostics: Set<ts.Diagnostic>;
100
60
  hasOnly: boolean;
101
61
  sourceFile: ts.SourceFile;
@@ -289,6 +249,58 @@ declare class Config {
289
249
  static resolveConfigFilePath(filePath?: string): string;
290
250
  }
291
251
 
252
+ declare enum DiagnosticCategory {
253
+ Error = "error",
254
+ Warning = "warning"
255
+ }
256
+
257
+ declare class SourceFile {
258
+ #private;
259
+ fileName: string;
260
+ text: string;
261
+ constructor(fileName: string, text: string);
262
+ getLineStarts(): Array<number>;
263
+ getLineAndCharacterOfPosition(position: number): {
264
+ line: number;
265
+ character: number;
266
+ };
267
+ }
268
+
269
+ declare class DiagnosticOrigin {
270
+ assertion: AssertionNode | undefined;
271
+ end: number;
272
+ sourceFile: SourceFile | ts.SourceFile;
273
+ start: number;
274
+ constructor(start: number, end: number, sourceFile: SourceFile | ts.SourceFile, assertion?: AssertionNode);
275
+ static fromAssertion(assertion: AssertionNode): DiagnosticOrigin;
276
+ static fromNode(node: ts.Node, assertion?: AssertionNode): DiagnosticOrigin;
277
+ static fromNodes(nodes: ts.NodeArray<ts.Node>, assertion?: AssertionNode): DiagnosticOrigin;
278
+ }
279
+
280
+ declare class Diagnostic {
281
+ category: DiagnosticCategory;
282
+ code: string | undefined;
283
+ origin: DiagnosticOrigin | undefined;
284
+ related: Array<Diagnostic> | undefined;
285
+ text: string | Array<string>;
286
+ constructor(text: string | Array<string>, category: DiagnosticCategory, origin?: DiagnosticOrigin);
287
+ add(options: {
288
+ code?: string | undefined;
289
+ related?: Array<Diagnostic> | undefined;
290
+ }): this;
291
+ static error(text: string | Array<string>, origin?: DiagnosticOrigin): Diagnostic;
292
+ extendWith(text: string | Array<string>, origin?: DiagnosticOrigin): Diagnostic;
293
+ static fromDiagnostics(diagnostics: Array<ts.Diagnostic>): Array<Diagnostic>;
294
+ static warning(text: string | Array<string>, origin?: DiagnosticOrigin): Diagnostic;
295
+ }
296
+
297
+ declare function diagnosticBelongsToNode(diagnostic: ts.Diagnostic, node: ts.NodeArray<ts.Node> | ts.Node): boolean;
298
+ declare function getDiagnosticMessageText(diagnostic: ts.Diagnostic): string | Array<string>;
299
+ declare function getTextSpanEnd(span: ts.TextSpan): number;
300
+ declare function isDiagnosticWithLocation(diagnostic: ts.Diagnostic): diagnostic is ts.DiagnosticWithLocation;
301
+
302
+ type DiagnosticsHandler<T extends Diagnostic | Array<Diagnostic> = Diagnostic> = (this: void, diagnostics: T) => void;
303
+
292
304
  declare enum OptionGroup {
293
305
  CommandLine = 2,
294
306
  ConfigFile = 4,
@@ -324,7 +336,7 @@ declare const defaultOptions: Required<ConfigFileOptions>;
324
336
 
325
337
  declare class ProjectService {
326
338
  #private;
327
- constructor(resolvedConfig: ResolvedConfig, compiler: typeof ts);
339
+ constructor(compiler: typeof ts, resolvedConfig: ResolvedConfig);
328
340
  closeFile(filePath: string): void;
329
341
  getDefaultProject(filePath: string): ts.server.Project | undefined;
330
342
  getLanguageService(filePath: string): ts.LanguageService | undefined;
@@ -337,6 +349,12 @@ declare class CollectService {
337
349
  createTestTree(sourceFile: ts.SourceFile, semanticDiagnostics?: Array<ts.Diagnostic>): TestTree;
338
350
  }
339
351
 
352
+ declare function nodeBelongsToArgumentList(compiler: typeof ts, node: ts.Node): boolean;
353
+
354
+ declare function argumentIsProvided<T>(argumentNameText: string, node: T, enclosingNode: ts.Node, onDiagnostics: DiagnosticsHandler<Array<Diagnostic>>): node is NonNullable<T>;
355
+
356
+ declare function argumentOrTypeArgumentIsProvided<T>(argumentNameText: string, typeArgumentNameText: string, node: T, enclosingNode: ts.Node, onDiagnostics: DiagnosticsHandler<Array<Diagnostic>>): node is NonNullable<T>;
357
+
340
358
  interface EnvironmentOptions {
341
359
  /**
342
360
  * Is `true` if the process is running in continuous integration environment.
@@ -488,13 +506,12 @@ declare class TargetResult {
488
506
  declare class Result {
489
507
  expectCount: ResultCount;
490
508
  fileCount: ResultCount;
491
- resolvedConfig: ResolvedConfig;
492
509
  results: Array<TargetResult>;
493
510
  targetCount: ResultCount;
494
511
  tasks: Array<Task>;
495
512
  testCount: ResultCount;
496
513
  timing: ResultTiming;
497
- constructor(resolvedConfig: ResolvedConfig, tasks: Array<Task>);
514
+ constructor(tasks: Array<Task>);
498
515
  }
499
516
 
500
517
  interface EventHandler {
@@ -530,11 +547,13 @@ type Event = ["config:error", {
530
547
  }] | ["task:end", {
531
548
  result: TaskResult;
532
549
  }] | ["collect:start", {
533
- testTree: TestTree;
550
+ tree: TestTree;
551
+ }] | ["collect:error", {
552
+ diagnostics: Array<Diagnostic>;
534
553
  }] | ["collect:node", {
535
- testNode: TestTreeNode | AssertionNode;
554
+ node: TestTreeNode | AssertionNode | WhenNode;
536
555
  }] | ["collect:end", {
537
- testTree: TestTree;
556
+ tree: TestTree;
538
557
  }] | ["describe:start", {
539
558
  result: DescribeResult;
540
559
  }] | ["describe:end", {
@@ -581,18 +600,19 @@ declare class EventEmitter {
581
600
  removeReporters(): void;
582
601
  }
583
602
 
603
+ declare class Reject {
604
+ #private;
605
+ constructor(compiler: typeof ts, typeChecker: ts.TypeChecker, resolvedConfig: ResolvedConfig);
606
+ argumentType(target: Array<[name: string, node: ts.Expression | ts.TypeNode | undefined]>, onDiagnostics: DiagnosticsHandler<Array<Diagnostic>>): boolean;
607
+ }
608
+
584
609
  interface MatchResult {
585
610
  explain: () => Array<Diagnostic>;
586
611
  isMatch: boolean;
587
612
  }
588
- type Relation = Map<string, unknown>;
589
613
  interface TypeChecker extends ts.TypeChecker {
590
614
  isApplicableIndexType: (source: ts.Type, target: ts.Type) => boolean;
591
- isTypeRelatedTo: (source: ts.Type, target: ts.Type, relation: Relation) => boolean;
592
- relation: {
593
- assignable: Relation;
594
- identity: Relation;
595
- };
615
+ isTypeIdenticalTo: (source: ts.Type, target: ts.Type) => boolean;
596
616
  }
597
617
 
598
618
  declare class ExpectService {
@@ -602,9 +622,11 @@ declare class ExpectService {
602
622
  private toBeApplicable;
603
623
  private toBeAssignableTo;
604
624
  private toBeAssignableWith;
625
+ private toBeCallableWith;
626
+ private toBeConstructableWith;
605
627
  private toHaveProperty;
606
628
  private toRaiseError;
607
- constructor(compiler: typeof ts, typeChecker: TypeChecker, resolvedConfig: ResolvedConfig);
629
+ constructor(compiler: typeof ts, typeChecker: TypeChecker, reject: Reject);
608
630
  match(assertion: AssertionNode, onDiagnostics: DiagnosticsHandler<Diagnostic | Array<Diagnostic>>): MatchResult | undefined;
609
631
  }
610
632
 
@@ -625,11 +647,6 @@ declare class ResultHandler implements EventHandler {
625
647
  on([event, payload]: Event): void;
626
648
  }
627
649
 
628
- declare class TestTreeHandler implements EventHandler {
629
- testTree: TestTree | undefined;
630
- on([event, payload]: Event): void;
631
- }
632
-
633
650
  type InputHandler = (chunk: string) => void;
634
651
  declare class InputService {
635
652
  #private;
@@ -720,13 +737,10 @@ declare class OutputService {
720
737
  static writeWarning(element: ScribblerJsx.Element | Array<ScribblerJsx.Element>): void;
721
738
  }
722
739
 
723
- declare function summaryText({ duration, expectCount, fileCount, onlyMatch, pathMatch, skipMatch, targetCount, testCount, }: {
740
+ declare function summaryText({ duration, expectCount, fileCount, targetCount, testCount, }: {
724
741
  duration: number;
725
742
  expectCount: ResultCount;
726
743
  fileCount: ResultCount;
727
- onlyMatch: string | undefined;
728
- pathMatch: Array<string>;
729
- skipMatch: string | undefined;
730
744
  targetCount: ResultCount;
731
745
  testCount: ResultCount;
732
746
  }): ScribblerJsx.Element;
@@ -866,5 +880,11 @@ declare class WatchService {
866
880
  watch(cancellationToken: CancellationToken): AsyncIterable<Array<Task>>;
867
881
  }
868
882
 
869
- export { AssertionNode, BaseReporter, CancellationHandler, CancellationReason, CancellationToken, Cli, CollectService, Color, Config, ConfigDiagnosticText, DescribeResult, Diagnostic, DiagnosticCategory, DiagnosticOrigin, EventEmitter, ExitCodeHandler, ExpectResult, ExpectService, FileWatcher, InputService, Line, ListReporter, OptionBrand, OptionGroup, Options, OutputService, Path, PluginService, ProjectResult, ProjectService, Result, ResultCount, ResultHandler, ResultStatus, ResultTiming, Runner, Scribbler, ScribblerJsx, Select, SelectDiagnosticText, SetupReporter, SourceFile, Store, SummaryReporter, TargetResult, Task, TaskResult, TestResult, TestTree, TestTreeHandler, TestTreeNode, TestTreeNodeBrand, TestTreeNodeFlags, Text, Version, WatchReporter, WatchService, Watcher, addsPackageText, defaultOptions, describeNameText, diagnosticText, environmentOptions, fileViewText, formattedText, helpText, summaryText, taskStatusText, testNameText, usesCompilerText, waitingForFileChangesText, watchUsageText };
883
+ declare class WhenService {
884
+ #private;
885
+ constructor(reject: Reject, onDiagnostics: DiagnosticsHandler<Array<Diagnostic>>);
886
+ action(when: WhenNode): void;
887
+ }
888
+
889
+ export { AssertionNode, BaseReporter, CancellationHandler, CancellationReason, CancellationToken, Cli, CollectService, Color, Config, ConfigDiagnosticText, DescribeResult, Diagnostic, DiagnosticCategory, DiagnosticOrigin, EventEmitter, ExitCodeHandler, ExpectResult, ExpectService, FileWatcher, InputService, Line, ListReporter, OptionBrand, OptionGroup, Options, OutputService, Path, PluginService, ProjectResult, ProjectService, Reject, Result, ResultCount, ResultHandler, ResultStatus, ResultTiming, Runner, Scribbler, ScribblerJsx, Select, SelectDiagnosticText, SetupReporter, SourceFile, Store, SummaryReporter, TargetResult, Task, TaskResult, TestResult, TestTree, TestTreeNode, TestTreeNodeBrand, TestTreeNodeFlags, Text, Version, WatchReporter, WatchService, Watcher, WhenNode, WhenService, addsPackageText, argumentIsProvided, argumentOrTypeArgumentIsProvided, defaultOptions, describeNameText, diagnosticBelongsToNode, diagnosticText, environmentOptions, fileViewText, formattedText, getDiagnosticMessageText, getTextSpanEnd, helpText, isDiagnosticWithLocation, nodeBelongsToArgumentList, summaryText, taskStatusText, testNameText, usesCompilerText, waitingForFileChangesText, watchUsageText };
870
890
  export type { CodeFrameOptions, CommandLineOptions, ConfigFileOptions, DiagnosticsHandler, EnvironmentOptions, Event, EventHandler, FileWatchHandler, InputHandler, ItemDefinition, MatchResult, OptionDefinition, Plugin, Reporter, ReporterEvent, ResolvedConfig, ScribblerOptions, SelectHookContext, TargetResultStatus, TaskResultStatus, TypeChecker, WatchHandler, WatcherOptions };