Fix TypeScript type definitions for test suite

- Add explicit Describe and It interfaces with todo support
- Fix mock type compatibility in jest.ts
- Update import assertions to import attributes (assert -> with)
- Add dynamic.d.mts declaration file
- Fix spec files to use explicit type declarations
This commit is contained in:
2026-01-17 21:02:25 +03:00
parent cb6801a3cc
commit b65f265e87
9 changed files with 2987 additions and 2672 deletions
+2 -2
View File
@@ -58,11 +58,11 @@ function extendMock <F extends AnyFn> (mocked: WithMock<F>) {
export function jest () {
return {
jest: enhanceObj(enhanceObj({
fn: <F extends AnyFn> (fn?: F) => extendMock<F>(mock.fn(fn)),
fn: <F extends AnyFn> (fn?: F) => extendMock<F>(mock.fn(fn) as unknown as WithMock<F>),
restoreAllMocks: () => {
mock.reset();
},
spyOn: <F extends AnyFn> (obj: object, key: string) => extendMock<F>(mock.method(obj, key as keyof typeof obj))
spyOn: <F extends AnyFn> (obj: object, key: string) => extendMock<F>(mock.method(obj, key as keyof typeof obj) as unknown as WithMock<F>)
}, jestWarn), jestStub)
};
}
+6
View File
@@ -1,6 +1,12 @@
// Copyright 2017-2026 @pezkuwi/dev-test authors & contributors
// SPDX-License-Identifier: Apache-2.0
import '../globals.d.ts';
declare const describe: import('./suite.js').Describe;
declare const it: import('./suite.js').It;
declare const expect: (value: unknown) => { toBe: (expected: unknown) => void; toBeDefined: () => void };
describe('describe()', () => {
// eslint-disable-next-line jest/no-focused-tests
describe.only('.only', () => {
+19 -5
View File
@@ -13,6 +13,20 @@ interface WrapOpts {
type WrapFn = (name: string, options: { only?: boolean; skip?: boolean; timeout?: number; todo?: boolean; }, fn: () => void | Promise<void>) => void | Promise<void>;
type TestFn = (name: string, exec: () => void | Promise<void>, timeout?: number) => void;
export interface Describe extends TestFn {
only: TestFn;
skip: TestFn;
todo: TestFn;
}
export interface It extends TestFn {
only: TestFn;
skip: TestFn;
todo: TestFn;
}
const MINUTE = 60 * 1000;
/**
@@ -23,7 +37,7 @@ const MINUTE = 60 * 1000;
*
* @param {} fn
*/
function createWrapper <T extends WrapFn> (fn: T, defaultTimeout: number) {
function createWrapper <T extends WrapFn> (fn: T, defaultTimeout: number): Describe | It {
const wrap = (opts: WrapOpts) => (name: string, exec: () => void | Promise<void>, timeout?: number) => fn(name, { ...opts, timeout: (timeout || defaultTimeout) }, exec) as unknown as void;
// Ensure that we have consistent helpers on the function. These are not consistently
@@ -33,16 +47,16 @@ function createWrapper <T extends WrapFn> (fn: T, defaultTimeout: number) {
only: wrap({ only: true }),
skip: wrap({ skip: true }),
todo: wrap({ todo: true })
});
}) as Describe | It;
}
/**
* This ensures that the describe and it functions match our actual usages.
* This includes .only, .skip and .todo helpers (.each is not applied)
**/
export function suite () {
export function suite (): { describe: Describe; it: It } {
return {
describe: createWrapper(describe, 60 * MINUTE),
it: createWrapper(it, 2 * MINUTE)
describe: createWrapper(describe, 60 * MINUTE) as Describe,
it: createWrapper(it, 2 * MINUTE) as It
};
}
+3 -5
View File
@@ -6,7 +6,7 @@
import type { expect } from './env/expect.js';
import type { jest } from './env/jest.js';
import type { lifecycle } from './env/lifecycle.js';
import type { suite } from './env/suite.js';
import type { Describe, It } from './env/suite.js';
type Expect = ReturnType<typeof expect>;
@@ -14,8 +14,6 @@ type Jest = ReturnType<typeof jest>;
type Lifecycle = ReturnType<typeof lifecycle>;
type Suite = ReturnType<typeof suite>;
declare global {
var after: Lifecycle['after'];
var afterAll: Lifecycle['afterAll'];
@@ -23,9 +21,9 @@ declare global {
var before: Lifecycle['before'];
var beforeAll: Lifecycle['beforeAll'];
var beforeEach: Lifecycle['beforeEach'];
var describe: Suite['describe'];
var describe: Describe;
var expect: Expect['expect'];
var it: Suite['it'];
var it: It;
var jest: Jest['jest'];
}
+2 -1
View File
@@ -8,7 +8,8 @@
"noEmit": true
},
"include": [
"src/**/*.spec.ts"
"src/**/*.spec.ts",
"src/globals.d.ts"
],
"references": [
{ "path": "../dev-test/tsconfig.build.json" }
+5 -1
View File
@@ -1,7 +1,11 @@
// Copyright 2017-2026 @pezkuwi/dev authors & contributors
// SPDX-License-Identifier: Apache-2.0
/// <reference types="@pezkuwi/dev-test/globals.d.ts" />
import type { Describe, It } from '@pezkuwi/dev-test/env/suite.js';
declare const describe: Describe;
declare const it: It;
declare const expect: (value: unknown) => { toBe: (expected: boolean) => void; toEqual: (expected: unknown) => void };
import fs from 'node:fs';
import path from 'node:path';
+4
View File
@@ -0,0 +1,4 @@
// Copyright 2017-2026 @pezkuwi/dev authors & contributors
// SPDX-License-Identifier: Apache-2.0
export function sum(a: number, b: number): number;
+1 -1
View File
@@ -5,7 +5,7 @@
import './augmented.js';
/** This import should appear as-in in the ouput (cjs without asserts) */
import testJson from '@pezkuwi/dev/rootJs/testJson.json' assert { type: 'json' };
import testJson from '@pezkuwi/dev/rootJs/testJson.json' with { type: 'json' };
/** Double double work, i.e. re-exports */
export { Clazz } from './Clazz.js';
+2945 -2657
View File
File diff suppressed because it is too large Load Diff