Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New] add types #603

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"no-underscore-dangle": "warn",
"object-curly-newline": "off",
"sort-keys": "off",

"no-extra-parens": "off",
},
"ignorePatterns": ["syntax-error.*"],
"overrides": [
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/node-aught.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ jobs:
range: '< 10'
type: minors
command: npm run tests-only
skip-ls-check: true
1 change: 1 addition & 0 deletions bin/import-or-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { extname: extnamePath } = require('path');
const { pathToFileURL } = require('url');
const getPackageType = require('get-package-type');

/** @type {(file: string) => undefined | Promise<unknown>} */
ljharb marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line consistent-return
module.exports = function importOrRequire(file) {
const ext = extnamePath(file);
Expand Down
2 changes: 2 additions & 0 deletions bin/tape
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ var hasImport = require('has-dynamic-import');

var tape = require('../');

/** @type {(hasSupport: boolean) => Promise<void> | void} */
function importFiles(hasSupport) {
tape.wait();

/** @type {null | undefined | Promise<unknown>} */
var filesPromise;
if (hasSupport) {
var importOrRequire = require('./import-or-require');
Expand Down
5 changes: 3 additions & 2 deletions example/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test('array', function (t) {
var src = '(' + function () {
var xs = [1, 2, [3, 4]];
var ys = [5, 6];
// @ts-expect-error
g([xs, ys]);
} + ')()';

Expand All @@ -26,11 +27,11 @@ test('array', function (t) {
];

Function('fn', 'g', String(output))(
function (xs) {
/** @param {(number | number[])[]} xs */ function (xs) {
t.same(arrays.shift(), xs);
return xs;
},
function (xs) {
/** @param {(number | number[])[]} xs */ function (xs) {
t.same(xs, [[1, 2, [3, 4]], [5, 6]]);
}
);
Expand Down
5 changes: 3 additions & 2 deletions example/fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test('array', function (t) {
var src = '(' + function () {
var xs = [1, 2, [3, 4]];
var ys = [5, 6];
// @ts-expect-error
g([xs, ys]);
} + ')()';

Expand All @@ -26,11 +27,11 @@ test('array', function (t) {
];

Function('fn', 'g', String(output))(
function (xs) {
/** @param {(number | number[])[]} xs */ function (xs) {
t.same(arrays.shift(), xs);
return xs;
},
function (xs) {
/** @param {(number | number[])[]} xs */ function (xs) {
t.same(xs, [[1, 2, [3, 4444]], [5, 6]]);
}
);
Expand Down
5 changes: 3 additions & 2 deletions example/nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test('nested array test', function (t) {
var src = '(' + function () {
var xs = [1, 2, [3, 4]];
var ys = [5, 6];
// @ts-expect-error
g([xs, ys]);
} + ')()';

Expand All @@ -35,11 +36,11 @@ test('nested array test', function (t) {
];

Function('fn', 'g', String(output))(
function (xs) {
/** @param {(number | number[])[]} xs */ function (xs) {
t.same(arrays.shift(), xs);
return xs;
},
function (xs) {
/** @param {(number | number[])[]} xs */ function (xs) {
t.same(xs, [[1, 2, [3, 4]], [5, 6]]);
}
);
Expand Down
5 changes: 3 additions & 2 deletions example/nested_fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test('nested array test', function (t) {
var src = '(' + function () {
var xs = [1, 2, [3, 4]];
var ys = [5, 6];
// @ts-expect-error
g([xs, ys]);
} + ')()';

Expand All @@ -35,11 +36,11 @@ test('nested array test', function (t) {
];

Function('fn', 'g', String(output))(
function (xs) {
/** @param {(number | number[])[]} xs */ function (xs) {
t.same(arrays.shift(), xs);
return xs;
},
function (xs) {
/** @param {(number | number[])[]} xs */ function (xs) {
t.same(xs, [[1, 2, [3, 4]], [5, 6]]);
}
);
Expand Down
5 changes: 3 additions & 2 deletions example/not_enough_fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test('array', function (t) {
var src = '(' + function () {
var xs = [1, 2, [3, 4]];
var ys = [5, 6];
// @ts-expect-error
g([xs, ys]);
} + ')()';

Expand All @@ -26,11 +27,11 @@ test('array', function (t) {
];

Function('fn', 'g', String(output))(
function (xs) {
/** @param {(number | number[])[]} xs */ function (xs) {
t.same(arrays.shift(), xs);
return xs;
},
function (xs) {
/** @param {(number | number[])[]} xs */ function (xs) {
t.same(xs, [[1, 2, [3, 4]], [5, 6]]);
}
);
Expand Down
5 changes: 3 additions & 2 deletions example/too_many_fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test('array', function (t) {
var src = '(' + function () {
var xs = [1, 2, [3, 4]];
var ys = [5, 6];
// @ts-expect-error
g([xs, ys]);
} + ')()';

Expand All @@ -26,11 +27,11 @@ test('array', function (t) {
];

Function('fn', 'g', String(output))(
function (xs) {
/** @param {(number | number[])[]} xs */ function (xs) {
t.same(arrays.shift(), xs);
return xs;
},
function (xs) {
/** @param {(number | number[])[]} xs */ function (xs) {
t.same(xs, [[1, 2, [3, 4]], [5, 6]]);
}
);
Expand Down
92 changes: 92 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import type { ThroughStream } from '@ljharb/through';

import type Test from './lib/test';
import type Results from './lib/results';

declare function harnessFunction(this: Test, name: string, opts: tape.TestOptions, cb: Test.TestCase): Test;
declare function harnessFunction(this: Test, name: string, opts: tape.TestOptions): Test;
declare function harnessFunction(this: Test, name: string, cb: Test.TestCase): Test;
declare function harnessFunction(this: Test, name: string): Test;
declare function harnessFunction(this: Test, opts: tape.TestOptions, cb: Test.TestCase): Test;
declare function harnessFunction(this: Test, opts: tape.TestOptions): Test;
declare function harnessFunction(this: Test, cb: Test.TestCase): Test;

declare function harnessFunction(this: void, name: string, opts: tape.TestOptions, cb: Test.TestCase): Test;
declare function harnessFunction(this: void, name: string, opts: tape.TestOptions): Test;
declare function harnessFunction(this: void, name: string, cb: Test.TestCase): Test;
declare function harnessFunction(this: void, name: string): Test;
declare function harnessFunction(this: void, opts: tape.TestOptions, cb: Test.TestCase): Test;
declare function harnessFunction(this: void, opts: tape.TestOptions): Test;
declare function harnessFunction(this: void, cb: Test.TestCase): Test;

declare namespace tape {
export type TestOptions = {
objectPrintDepth?: number | undefined;
skip?: boolean | string | undefined;
timeout?: number | undefined;
todo?: boolean | string | undefined;
};

export interface AssertOptions {
skip?: boolean | string | undefined;
todo?: boolean | string | undefined;
message?: string | undefined;
actual?: unknown;
expected?: unknown;
exiting?: boolean;
}

export interface StreamOptions {
objectMode?: boolean | undefined;
}

function createStream(opts?: StreamOptions): ThroughStream;

export type CreateStream = typeof createStream;

export type HarnessEventHandler = (cb: Test.SyncCallback, ...rest: unknown[]) => void;

function only(name: string, cb: Test.TestCase): void;
function only(name: string, opts: tape.TestOptions, cb: Test.TestCase): void;
function only(cb: Test.TestCase): void;
function only(opts: tape.TestOptions, cb?: Test.TestCase): void;

export type Harness = typeof harnessFunction & {
run?: () => void;
only: typeof only;
_exitCode: number;
_results: Results;
_tests: Test[];
close: () => void;
createStream: CreateStream;
onFailure: HarnessEventHandler;
onFinish: HarnessEventHandler;
}

export type HarnessConfig = {
autoclose?: boolean;
noOnly?: boolean;
stream?: NodeJS.WritableStream | ThroughStream;
exit?: boolean;
} & StreamOptions;

function createHarness(conf_?: HarnessConfig): Harness;
const Test: Test;
const test: Harness;
const skip: Test['skip'];

function getHarness(opts?: HarnessConfig): Harness;
function run(): void;
function onFailure(cb: Test.SyncCallback, ...rest: unknown[]): void;
function onFinish(cb: Test.SyncCallback, ...rest: unknown[]): void
function wait(): void;
}

declare function tape(this: void | tape.Harness, name: string, opts: tape.TestOptions, cb: Test.TestCase): Test;
declare function tape(this: void | tape.Harness, name: string, cb: Test.TestCase): Test;
declare function tape(this: void | tape.Harness, opts?: tape.TestOptions): Test;
declare function tape(this: void | tape.Harness, opts: tape.TestOptions, cb: Test.TestCase): Test;
declare function tape(this: void | tape.Harness, cb: Test.TestCase): Test;
declare function tape(this: void | tape.Harness, name: string): Test;

export = tape;
Loading
Loading