Skip to content

Commit 0e18865

Browse files
ivogabesindresorhus
authored andcommitted
Close #571 PR: Add TypeScript typings. Fixes #568
1 parent 691fdb6 commit 0e18865

File tree

4 files changed

+151
-1
lines changed

4 files changed

+151
-1
lines changed

index.d.ts

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
export interface Observable {
2+
subscribe(observer: (value: {}) => void): void;
3+
}
4+
5+
export type Test = (t: TestContext) => Promise<void> | Iterator<any> | Observable | void;
6+
export type SerialTest = (t: TestContext) => void;
7+
export type CallbackTest = (t: CallbackTestContext) => void;
8+
9+
export interface Runner {
10+
(name: string, run: Test): void;
11+
(run: Test): void;
12+
skip: Runner;
13+
cb: CallbackRunner;
14+
}
15+
export interface SerialRunner {
16+
(name: string, run: SerialTest): void;
17+
(run: SerialTest): void;
18+
skip: SerialRunner;
19+
}
20+
export interface CallbackRunner {
21+
(name: string, run: CallbackTest): void;
22+
(run: CallbackTest): void;
23+
skip: CallbackRunner;
24+
}
25+
26+
export function test(name: string, run: Test): void;
27+
export function test(run: Test): void;
28+
export namespace test {
29+
export const before: Runner;
30+
export const after: Runner;
31+
export const beforeEach: Runner;
32+
export const afterEach: Runner;
33+
34+
export const skip: typeof test;
35+
export const only: typeof test;
36+
37+
export function serial(name: string, run: SerialTest): void;
38+
export function serial(run: SerialTest): void;
39+
export function cb(name: string, run: CallbackTest): void;
40+
export function cb(run: CallbackTest): void;
41+
}
42+
export namespace test.serial {
43+
export const before: SerialRunner;
44+
export const after: SerialRunner;
45+
export const beforeEach: SerialRunner;
46+
export const afterEach: SerialRunner;
47+
48+
export const skip: typeof test.serial;
49+
export const only: typeof test.serial;
50+
}
51+
export namespace test.cb {
52+
export const before: CallbackRunner;
53+
export const after: CallbackRunner;
54+
export const beforeEach: CallbackRunner;
55+
export const afterEach: CallbackRunner;
56+
57+
export const skip: typeof test.cb;
58+
export const only: typeof test.cb;
59+
}
60+
export default test;
61+
62+
export type ErrorValidator
63+
= (new (...args: any[]) => any)
64+
| RegExp
65+
| string
66+
| ((error: any) => boolean);
67+
68+
export interface AssertContext {
69+
/**
70+
* Passing assertion.
71+
*/
72+
pass(message?: string): void;
73+
/**
74+
* Failing assertion.
75+
*/
76+
fail(message?: string): void;
77+
/**
78+
* Assert that value is truthy.
79+
*/
80+
ok(value: any, message?: string): void;
81+
/**
82+
* Assert that value is falsy.
83+
*/
84+
notOk(value: any, message?: string): void;
85+
/**
86+
* Assert that value is true.
87+
*/
88+
true(value: boolean, message?: string): void;
89+
/**
90+
* Assert that value is false.
91+
*/
92+
false(value: boolean, message?: string): void;
93+
/**
94+
* Assert that value is equal to expected.
95+
*/
96+
is<U>(value: U, expected: U, message?: string): void;
97+
/**
98+
* Assert that value is not equal to expected.
99+
*/
100+
not<U>(value: U, expected: U, message?: string): void;
101+
/**
102+
* Assert that value is deep equal to expected.
103+
*/
104+
same<U>(value: U, expected: U, message?: string): void;
105+
/**
106+
* Assert that value is not deep equal to expected.
107+
*/
108+
notSame<U>(value: U, expected: U, message?: string): void;
109+
/**
110+
* Assert that function throws an error or promise rejects.
111+
* @param error Can be a constructor, regex, error message or validation function.
112+
*/
113+
throws(value: (() => void) | Promise<{}>, error?: ErrorValidator, message?: string);
114+
/**
115+
* Assert that function doesn't throw an error or promise resolves.
116+
*/
117+
notThrows(value: (() => void) | Promise<{}>, message?: string);
118+
/**
119+
* Assert that contents matches regex.
120+
*/
121+
regex(contents: string, regex: RegExp, message?: string): void;
122+
/**
123+
* Assert that error is falsy.
124+
*/
125+
ifError(error: any, message?: string): void;
126+
}
127+
export interface TestContext extends AssertContext {
128+
/**
129+
* Plan how many assertion there are in the test.
130+
* The test will fail if the actual assertion count doesn't match planned assertions.
131+
*/
132+
plan(count: number): void;
133+
134+
skip: AssertContext;
135+
}
136+
export interface CallbackTestContext extends TestContext {
137+
/**
138+
* End the test.
139+
*/
140+
end(): void;
141+
}

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,7 @@ globals.setImmediate(function () {
8989
});
9090

9191
module.exports = runner.test;
92+
// TypeScript imports the `default` property for
93+
// an ES2015 default import (`import test from 'ava'`)
94+
// See: https://github.com/Microsoft/TypeScript/issues/2242#issuecomment-83694181
95+
module.exports.default = runner.test;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
},
3939
"files": [
4040
"lib",
41-
"*.js"
41+
"*.js",
42+
"index.d.ts"
4243
],
4344
"keywords": [
4445
"test",

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ test(t => {
363363

364364
AVA comes with builtin support for ES2015 through [Babel 6](https://babeljs.io). Just write your tests in ES2015. No extra setup needed. You can use any Babel version in your project. We use our own bundled Babel with the [`es2015`](http://babeljs.io/docs/plugins/preset-es2015/) and [`stage-2`](http://babeljs.io/docs/plugins/preset-stage-2/) presets.
365365

366+
### TypeScript support
367+
368+
AVA includes typings for TypeScript. You have to setup transpilation yourself. When you set `module` to `commonjs` in your `tsconfig.json` file, TypeScript will automatically find the type definitions for AVA. You should set `target` to `es2015` to use Promises and async functions.
369+
366370
#### Transpiling Imported Modules
367371

368372
AVA currently only transpiles the tests you ask it to run. *It will not transpile modules you ```import``` from outside of the test.* While there are valid reasons for taking this approach, it may not be what you expect!

0 commit comments

Comments
 (0)