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 18, 2023
1 parent 4891709 commit 2ce3d41
Show file tree
Hide file tree
Showing 8 changed files with 405 additions and 100 deletions.
67 changes: 43 additions & 24 deletions types/node/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,27 +136,20 @@ declare module 'node:test' {
function test(options?: TestOptions, fn?: TestFn): Promise<void>;
function test(fn?: TestFn): Promise<void>;
namespace test {
/**
* Shorthand for skipping a suite, same as `test([name], { skip: true }[, fn])`.
*/
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 suite as `TODO`, same as `test([name], { todo: true }[, fn])`.
*/
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 suite as `TODO`, same as `test([name], { only: true }[, fn])`.
*/
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;
export {
after,
afterEach,
before,
beforeEach,
describe,
it,
run,
mock,
test,
skip,
todo,
only
};
}
/**
* The `describe()` function imported from the `node:test` module. Each
Expand Down Expand Up @@ -188,7 +181,8 @@ declare module 'node:test' {
function todo(options?: TestOptions, fn?: SuiteFn): void;
function todo(fn?: SuiteFn): void;
/**
* Shorthand for marking a suite as `TODO`, same as `describe([name], { only: true }[, fn])`.
* 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;
Expand Down Expand Up @@ -217,13 +211,38 @@ declare module 'node:test' {
function todo(options?: TestOptions, fn?: TestFn): void;
function todo(fn?: TestFn): void;
/**
* Shorthand for marking a suite as `TODO`, same as `it([name], { only: true }[, fn])`.
* Shorthand for marking a test as `only`, same as `it([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 @@ -1025,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 };
}
87 changes: 75 additions & 12 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,56 +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,
only: true,
skip: true,
signal: new AbortController().signal,
timeout: Infinity,
});
it.skip('skip shorthand', {
concurrency: 1,
only: true,
skip: true,
signal: new AbortController().signal,
timeout: Infinity,
});
test.skip('skip shorthand', {

todo('todo shorthand', {
concurrency: 1,
only: true,
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,
only: true,
todo: true,
signal: new AbortController().signal,
timeout: Infinity,
});
it.todo('todo shorthand', {
concurrency: 1,
only: true,
todo: true,
signal: new AbortController().signal,
timeout: Infinity,
});
test.todo('todo shorthand', {

only('todo shorthand', {
concurrency: 1,
only: true,
signal: new AbortController().signal,
timeout: Infinity,
});
describe.only('only 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,
});
it.only('only shorthand', {
describe.only('only shorthand', {
concurrency: 1,
only: true,
signal: new AbortController().signal,
timeout: Infinity,
});
test.only('only shorthand', {
it.only('only shorthand', {
concurrency: 1,
only: true,
signal: new AbortController().signal,
Expand Down
105 changes: 60 additions & 45 deletions types/node/ts4.8/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,27 +136,20 @@ declare module 'node:test' {
function test(options?: TestOptions, fn?: TestFn): Promise<void>;
function test(fn?: TestFn): Promise<void>;
namespace test {
/**
* Shorthand for skipping a suite, same as `test([name], { skip: true }[, fn])`.
*/
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 suite as `TODO`, same as `test([name], { todo: true }[, fn])`.
*/
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 suite as `TODO`, same as `test([name], { only: true }[, fn])`.
*/
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;
export {
after,
afterEach,
before,
beforeEach,
describe,
it,
run,
mock,
test,
skip,
todo,
only
};
}
/**
* The `describe()` function imported from the `node:test` module. Each
Expand Down Expand Up @@ -187,8 +180,10 @@ 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 `TODO`, same as `describe([name], { only: true }[, fn])`.
* 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;
Expand All @@ -201,29 +196,54 @@ declare module 'node:test' {
* The `it()` function is imported from the `node:test` module.
* @since v18.6.0, v16.17.0
*/
function it(name?: string, options?: TestOptions, fn?: ItFn): void;
function it(name?: string, fn?: ItFn): void;
function it(options?: TestOptions, fn?: ItFn): void;
function it(fn?: ItFn): void;
function it(name?: string, options?: TestOptions, fn?: TestFn): void;
function it(name?: string, fn?: TestFn): void;
function it(options?: TestOptions, fn?: TestFn): void;
function it(fn?: TestFn): void;
namespace it {
// Shorthand for skipping a test, same as `it([name], { skip: true }[, fn])`.
function skip(name?: string, options?: TestOptions, fn?: ItFn): void;
function skip(name?: string, fn?: ItFn): void;
function skip(options?: TestOptions, fn?: ItFn): void;
function skip(fn?: ItFn): void;
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 `it([name], { todo: true }[, fn])`.
function todo(name?: string, options?: TestOptions, fn?: ItFn): void;
function todo(name?: string, fn?: ItFn): void;
function todo(options?: TestOptions, fn?: ItFn): void;
function todo(fn?: ItFn): void;
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 suite as `TODO`, same as `it([name], { only: true }[, fn])`.
* Shorthand for marking a test as `only`, same as `it([name], { only: true }[, fn])`.
* @since v18.15.0
*/
function only(name?: string, options?: TestOptions, fn?: ItFn): void;
function only(name?: string, fn?: ItFn): void;
function only(options?: TestOptions, fn?: ItFn): void;
function only(fn?: ItFn): void;
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 All @@ -235,11 +255,6 @@ declare module 'node:test' {
* If the test uses callbacks, the callback function is passed as an argument
*/
type SuiteFn = (done: (result?: any) => void) => void;
/**
* The type of a function under test.
* If the test uses callbacks, the callback function is passed as an argument
*/
type ItFn = (done: (result?: any) => void) => any;
interface RunOptions {
/**
* If a number is provided, then that many files would run in parallel.
Expand Down Expand Up @@ -1030,5 +1045,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 };
}
Loading

0 comments on commit 2ce3d41

Please sign in to comment.