Add types for lab v11 (#12468)

This commit is contained in:
Prashant Tiwari
2016-11-06 20:53:30 +05:30
committed by Andy
parent b469fc9234
commit 3c0d432527
3 changed files with 375 additions and 0 deletions

187
lab/index.d.ts vendored Normal file
View File

@@ -0,0 +1,187 @@
// Type definitions for lab 11.1.0
// Project: https://github.com/hapijs/lab
// Definitions by: Prashant Tiwari <https://github.com/prashaantt>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/** The test script. */
export function script(options?: ScriptOptions): Lab & ExperimentAlt & TestAlt;
/** Access the configured assertion library. */
export const assertions: any;
interface Lab {
/** Organise tests into an experiment */
experiment(desc: string, cb: EmptyCallback): void;
/** Organise tests into an experiment with options */
experiment(desc: string, options: ExperimentOptions, cb: EmptyCallback): void;
/** Create a test suite */
describe(desc: string, cb: EmptyCallback): void;
/** Create a test suite with options */
describe(desc: string, options: ExperimentOptions, cb: EmptyCallback): void;
/** Create a test suite */
suite(desc: string, cb: EmptyCallback): void;
/** Create a test suite with options */
suite(desc: string, options: ExperimentOptions, cb: EmptyCallback): void;
/** The test spec */
test(desc: string, cb: TestCallback): void;
/** The test spec using a promise */
test(desc: string, promise: TestPromise): void;
/** The test spec with options */
test(desc: string, options: TestOptions, cb: TestCallback): void;
/** The test spec using a promise with options */
test(desc: string, options: TestOptions, promise: TestPromise): void;
/** The test spec */
it(desc: string, cb: TestCallback): void;
/** The test spec using a promise */
it(desc: string, promise: TestPromise): void;
/** The test spec with options */
it(desc: string, options: TestOptions, cb: TestCallback): void;
/** The test spec using a promise with options */
it(desc: string, options: TestOptions, promise: TestPromise): void;
/** Perform async actions before the test suite */
before(cb: AsyncCallback): void;
/** Perform async actions before the test suite using a promise */
before(promise: AsyncPromise): void;
/** Perform async actions before the test suite with options */
before(options: AsyncOptions, cb: AsyncCallback): void;
/** Perform async actions before the test suite with otions, using a promise */
before(options: AsyncOptions, promise: AsyncPromise): void;
/** Perform async actions before each test */
beforeEach(cb: AsyncCallback): void;
/** Perform async actions before each test using a promise */
beforeEach(promise: AsyncPromise): void;
/** Perform async actions before each test with options */
beforeEach(options: AsyncOptions, cb: AsyncCallback): void;
/** Perform async actions before each test with options, using a promise */
beforeEach(options: AsyncOptions, promise: AsyncPromise): void;
/** Perform async actions after the test suite */
after(cb: AsyncCallback): void;
/** Perform async actions after the test suite using a promise */
after(promise: AsyncPromise): void;
/** Perform async actions after the test suite with options */
after(options: AsyncOptions, cb: AsyncCallback): void;
/** Perform async actions after the test suite with options, using a promise */
after(options: AsyncOptions, promise: AsyncPromise): void;
/** Perform async actions after each test */
afterEach(cb: AsyncCallback): void;
/** Perform async actions after each test using a promise */
afterEach(promise: AsyncPromise): void;
/** Perform async actions after each test with options */
afterEach(options: AsyncOptions, cb: AsyncCallback): void;
/** Perform async actions after each test with options, using a promise */
afterEach(options: AsyncOptions, promise: AsyncPromise): void;
}
interface ExperimentAlt {
experiment: SkipOnlyExperiment;
suite: SkipOnlyExperiment;
describe: SkipOnlyExperiment;
}
interface TestAlt {
test: SkipOnlyTest;
it: SkipOnlyTest;
}
interface SkipOnlyExperiment {
/** Skip this test suite */
skip: ExperimentArgs & ExperimentWithOptionsArgs;
/** Only execute this test suite */
only: ExperimentArgs & ExperimentWithOptionsArgs;
}
interface SkipOnlyTest {
/** Skip this test */
skip: TestArgs & TestWithOptionsArgs;
/** Only execute this test */
only: TestArgs & TestWithOptionsArgs;
}
interface ScriptOptions {
/** Enable auto-execution of the script? (true) */
schedule?: boolean;
/** Pass Lab CLI options */
cli?: any;
}
interface ExperimentOptions {
/** Set a specific timeout in milliseconds (2000) */
timeout?: number;
/** Execute tests in parallel? (false) */
parallel?: boolean;
/** Skip execution? (false) */
skip?: boolean;
/** Execute only this test/experiment? (false) */
only?: boolean;
}
interface TestOptions extends ExperimentOptions {
/** The expected number of assertions to execute */
plan?: number;
}
interface AsyncOptions {
/** Set a specific timeout in milliseconds (disabled) */
timeout?: number;
}
interface DoneNote {
/** Attach a note to the test case */
note: (text: string) => void;
}
type EmptyCallback = () => void;
type DoneFunction = (err?: Error) => void;
type CleanupFunction = (func: (next: Function) => void) => void;
type TestCallback = (done: DoneFunction & DoneNote, onCleanup?: CleanupFunction) => void;
type TestPromise = () => Promise<any>;
type AsyncCallback = (done: DoneFunction) => void;
type AsyncPromise = () => Promise<any>;
type ExperimentArgs = (desc: string, cb: EmptyCallback) => {};
type ExperimentWithOptionsArgs = (desc: string, options: ExperimentOptions, cb: EmptyCallback) => {};
type TestArgs = (desc: string, cb: TestCallback) => {};
type TestWithOptionsArgs = (desc: string, options: TestOptions, cb: TestCallback) => {}

169
lab/lab-tests.ts Normal file
View File

@@ -0,0 +1,169 @@
import { script, assertions } from "lab";
const { experiment, describe, suite, test, it, before, beforeEach, after, afterEach } = script();
const expect = assertions.expect;
const fail = assertions.fail;
experiment('math', () => {
before((done) => {
setTimeout(() => {
done();
}, 1000);
});
beforeEach((done) => {
done();
});
test('returns true when 1 + 1 equals 2', (done) => {
expect(1 + 1).to.equal(2);
done();
});
});
experiment('math', () => {
before(() => {
return Promise.resolve();
});
test('returns true when 1 + 1 equals 2', () => {
return Promise.resolve()
.then((aValue) => {
const expectedValue = aValue;
expect(aValue).to.equal(expectedValue);
});
});
});
experiment.only('with only experiment', () => {
test('this test will run', (done) => {
expect(1 + 1).to.equal(2);
done();
});
test('another test that will run', (done) => {
expect(true).to.equal(true);
done();
});
});
experiment('with only test', () => {
test.only('only this test will run', (done) => {
expect(1 + 1).to.equal(2);
done();
});
test('another test that will not be executed', (done) => {
done();
});
});
test('attaches notes', (done) => {
expect(1 + 1).to.equal(2);
done.note(`The current time is ${Date.now()}`);
done();
});
test('cleanups after test', (done, onCleanup) => {
if (onCleanup) {
onCleanup((next) => {
return next();
});
}
expect(1 + 1).to.equal(2);
done();
});
experiment('my plan', () => {
test('only a single assertion executes', { plan: 1 }, (done) => {
expect(1 + 1).to.equal(2);
done();
});
});
experiment('math', { timeout: 1000 }, () => {
before({ timeout: 500 }, (done) => {
done();
});
test('returns true when 1 + 1 equals 2', { parallel: true }, (done) => {
expect(1 + 1).to.equal(2);
done();
});
});
describe('math', () => {
before((done) => {
done();
});
after((done) => {
done();
});
afterEach((done) => {
done();
});
it('returns true when 1 + 1 equals 2', (done) => {
expect(1 + 1).to.equal(2);
done();
});
});
suite('math', () => {
test('returns true when 1 + 1 equals 2', (done) => {
expect(1 + 1).to.equal(2);
done();
});
});
describe('expectation', () => {
it('should be able to expect', (done) => {
expect(true).to.be.true();
done();
});
it('should be able to fail (This test should fail)', (done) => {
fail('Should fail');
done();
});
});

19
lab/tsconfig.json Normal file
View File

@@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"lab-tests.ts"
]
}