Skip to content

Commit

Permalink
feat(node): extend test API shorthands
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed May 17, 2023
1 parent da89ad3 commit 757e4a5
Show file tree
Hide file tree
Showing 6 changed files with 374 additions and 20 deletions.
58 changes: 57 additions & 1 deletion types/node/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ declare module 'node:test' {
function test(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
function test(options?: TestOptions, fn?: TestFn): Promise<void>;
function test(fn?: TestFn): Promise<void>;
namespace test {
export {
after,
afterEach,
before,
beforeEach,
describe,
it,
run,
mock,
test,
skip,
todo,
only
};
}
/**
* The `describe()` function imported from the `node:test` module. Each
* invocation of this function results in the creation of a Subtest.
Expand Down Expand Up @@ -164,6 +180,14 @@ declare module 'node:test' {
function todo(name?: string, fn?: SuiteFn): void;
function todo(options?: TestOptions, fn?: SuiteFn): void;
function todo(fn?: SuiteFn): void;
/**
* Shorthand for marking a suite as `only`, same as `describe([name], { only: true }[, fn])`.
* @since v18.15.0
*/
function only(name?: string, options?: TestOptions, fn?: SuiteFn): void;
function only(name?: string, fn?: SuiteFn): void;
function only(options?: TestOptions, fn?: SuiteFn): void;
function only(fn?: SuiteFn): void;
}
/**
* Shorthand for `test()`.
Expand All @@ -186,7 +210,39 @@ declare module 'node:test' {
function todo(name?: string, fn?: TestFn): void;
function todo(options?: TestOptions, fn?: TestFn): void;
function todo(fn?: TestFn): void;
/**
* Shorthand for marking a suite as `only`, same as `describe([name], { only: true }[, fn])`.
* @since v18.15.0
*/
function only(name?: string, options?: TestOptions, fn?: TestFn): void;
function only(name?: string, fn?: TestFn): void;
function only(options?: TestOptions, fn?: TestFn): void;
function only(fn?: TestFn): void;
}
/**
* Shorthand for skipping a test, same as `test([name], { skip: true }[, fn])`.
* @since v20.2.0
*/
function skip(name?: string, options?: TestOptions, fn?: TestFn): void;
function skip(name?: string, fn?: TestFn): void;
function skip(options?: TestOptions, fn?: TestFn): void;
function skip(fn?: TestFn): void;
/**
* Shorthand for marking a test as `TODO`, same as `test([name], { todo: true }[, fn])`.
* @since v20.2.0
*/
function todo(name?: string, options?: TestOptions, fn?: TestFn): void;
function todo(name?: string, fn?: TestFn): void;
function todo(options?: TestOptions, fn?: TestFn): void;
function todo(fn?: TestFn): void;
/**
* Shorthand for marking a test as `only`, same as `test([name], { only: true }[, fn])`.
* @since v20.2.0
*/
function only(name?: string, options?: TestOptions, fn?: TestFn): void;
function only(name?: string, fn?: TestFn): void;
function only(options?: TestOptions, fn?: TestFn): void;
function only(fn?: TestFn): void;
/**
* The type of a function under test. The first argument to this function is a
* {@link TestContext} object. If the test uses callbacks, the callback function is passed as
Expand Down Expand Up @@ -988,5 +1044,5 @@ declare module 'node:test' {
*/
restore(): void;
}
export { test as default, run, test, describe, it, before, after, beforeEach, afterEach, mock };
export { test as default, run, test, describe, it, before, after, beforeEach, afterEach, mock, skip, only, todo };
}
103 changes: 98 additions & 5 deletions types/node/test/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, run, test, before, beforeEach, after, afterEach } from 'node:test';
import { describe, it, run, test, before, beforeEach, after, afterEach, skip, todo, only } from 'node:test';

// run without options
// $ExpectType TestsStream
Expand Down Expand Up @@ -53,6 +53,8 @@ test('options with booleans', {

// Test callback mode
test((t, cb) => {
// $ExpectedType TestContext
t;
// $ExpectType (result?: any) => void
cb;
// $ExpectType void
Expand Down Expand Up @@ -98,6 +100,24 @@ test(t => {
// @ts-expect-error
test(1, () => {});

test.after(() => {});
test.afterEach(() => {});
test.before(() => {});
test.beforeEach(() => {});
test.describe('describe', () => {});
test.it('it', () => {});
// $ExpectType MockTracker
test.mock;
// $ExpectType typeof test
test.test;
test.test.test('chained self ref', (t) => {
// $ExpectType typeof test
t.test;
});
test.skip('skip', () => {});
test.todo('todo', () => {});
test.only('only', () => {});

describe('foo', () => {
it('it', () => {});
});
Expand Down Expand Up @@ -132,26 +152,99 @@ it('options with booleans', {
todo: false,
});

skip('skip shorthand', {
concurrency: 1,
skip: true,
signal: new AbortController().signal,
timeout: Infinity,
});
skip((t, cb) => {
// $ExpectType TestContext
t;
// $ExpectType (result?: any) => void
cb;
// $ExpectType void
cb({ x: 'anything' });
});
test.skip('skip shorthand', {
concurrency: 1,
skip: true,
signal: new AbortController().signal,
timeout: Infinity,
});
describe.skip('skip shorthand', {
concurrency: 1,
skip: true,
signal: new AbortController().signal,
timeout: Infinity,
});
it.skip('skip shorthand', {
concurrency: 1,
skip: true,
signal: new AbortController().signal,
timeout: Infinity,
});

todo('todo shorthand', {
concurrency: 1,
todo: true,
signal: new AbortController().signal,
timeout: Infinity,
});
todo((t, cb) => {
// $ExpectType TestContext
t;
// $ExpectType (result?: any) => void
cb;
// $ExpectType void
cb({ x: 'anything' });
});
test.todo('todo shorthand', {
concurrency: 1,
todo: true,
signal: new AbortController().signal,
timeout: Infinity,
});
describe.todo('todo shorthand', {
concurrency: 1,
todo: true,
signal: new AbortController().signal,
timeout: Infinity,
});
it.todo('todo shorthand', {
concurrency: 1,
todo: true,
signal: new AbortController().signal,
timeout: Infinity,
});

only('todo shorthand', {
concurrency: 1,
only: true,
signal: new AbortController().signal,
timeout: Infinity,
});
it.skip('todo shorthand', {
only((t, cb) => {
// $ExpectType TestContext
t;
// $ExpectType (result?: any) => void
cb;
// $ExpectType void
cb({ x: 'anything' });
});
test.only('only shorthand', {
concurrency: 1,
only: true,
signal: new AbortController().signal,
timeout: Infinity,
});

describe.todo('skip shorthand', {
describe.only('only shorthand', {
concurrency: 1,
only: true,
signal: new AbortController().signal,
timeout: Infinity,
});
it.todo('todo shorthand', {
it.only('only shorthand', {
concurrency: 1,
only: true,
signal: new AbortController().signal,
Expand Down
33 changes: 31 additions & 2 deletions types/node/v18/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ declare module 'node:test' {
* @returns A {@link TestsStream} that emits events about the test execution.
*/
function run(options?: RunOptions): TestsStream;

/**
* The `test()` function is the value imported from the test module. Each invocation of this
* function results in reporting the test to the {@link TestsStream}.
Expand Down Expand Up @@ -49,7 +48,19 @@ declare module 'node:test' {
function test(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
function test(options?: TestOptions, fn?: TestFn): Promise<void>;
function test(fn?: TestFn): Promise<void>;

namespace test {
export {
after,
afterEach,
before,
beforeEach,
describe,
it,
run,
mock,
test
};
}
/**
* @since v18.6.0
* @param name The name of the suite, which is displayed when reporting suite results.
Expand All @@ -73,6 +84,15 @@ declare module 'node:test' {
function todo(name?: string, fn?: SuiteFn): void;
function todo(options?: TestOptions, fn?: SuiteFn): void;
function todo(fn?: SuiteFn): void;

/**
* Shorthand for marking a suite as `only`, same as `describe([name], { only: true }[, fn])`.
* @since v18.15.0
*/
function only(name?: string, options?: TestOptions, fn?: SuiteFn): void;
function only(name?: string, fn?: SuiteFn): void;
function only(options?: TestOptions, fn?: SuiteFn): void;
function only(fn?: SuiteFn): void;
}

/**
Expand All @@ -99,6 +119,15 @@ declare module 'node:test' {
function todo(name?: string, fn?: TestFn): void;
function todo(options?: TestOptions, fn?: TestFn): void;
function todo(fn?: TestFn): void;

/**
* Shorthand for marking a suite as `only`, same as `describe([name], { only: true }[, fn])`.
* @since v18.15.0
*/
function only(name?: string, options?: TestOptions, fn?: TestFn): void;
function only(name?: string, fn?: TestFn): void;
function only(options?: TestOptions, fn?: TestFn): void;
function only(fn?: TestFn): void;
}

/**
Expand Down
38 changes: 33 additions & 5 deletions types/node/v18/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ test(t => {
// @ts-expect-error
test(1, () => {});

test.after(() => {});
test.afterEach(() => {});
test.before(() => {});
test.beforeEach(() => {});
test.describe('describe', () => {});
test.it('it', () => {});
// $ExpectType MockTracker
test.mock;
// $ExpectType typeof test
test.test;
test.test.test('chained self ref', (t) => {
// $ExpectType typeof test
t.test;
});

describe('foo', () => {
it('it', () => {});
});
Expand Down Expand Up @@ -131,24 +146,37 @@ it('options with booleans', {

describe.skip('skip shorthand', {
concurrency: 1,
only: true,
skip: true,
signal: new AbortController().signal,
timeout: Infinity,
});
it.skip('todo shorthand', {
it.skip('skip shorthand', {
concurrency: 1,
only: true,
skip: true,
signal: new AbortController().signal,
timeout: Infinity,
});

describe.todo('skip shorthand', {
describe.todo('todo shorthand', {
concurrency: 1,
only: true,
todo: true,
signal: new AbortController().signal,
timeout: Infinity,
});
it.todo('todo shorthand', {
concurrency: 1,
todo: true,
signal: new AbortController().signal,
timeout: Infinity,
});

describe.only('only shorthand', {
concurrency: 1,
only: true,
signal: new AbortController().signal,
timeout: Infinity,
});
it.only('only shorthand', {
concurrency: 1,
only: true,
signal: new AbortController().signal,
Expand Down
Loading

0 comments on commit 757e4a5

Please sign in to comment.