Skip to content

Commit

Permalink
chore: Switch to vitest.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shakeskeyboarde committed Nov 16, 2023
1 parent 493d3ea commit fbc1d6c
Show file tree
Hide file tree
Showing 35 changed files with 9,998 additions and 6,968 deletions.
26 changes: 0 additions & 26 deletions .eslintrc.cjs

This file was deleted.

6 changes: 3 additions & 3 deletions debounced/debounced.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { createDebounced } from './debounced.js';

test('debounced', () => {
jest.useFakeTimers();
const callback = jest.fn();
vi.useFakeTimers();
const callback = vi.fn();
const debounced = createDebounced(250, callback);
debounced(1);
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenLastCalledWith(1);
debounced(2);
expect(callback).toHaveBeenCalledTimes(1);
jest.runAllTimers();
vi.runAllTimers();
debounced(3);
expect(callback).toHaveBeenCalledTimes(2);
expect(callback).toHaveBeenLastCalledWith(3);
Expand Down
8 changes: 4 additions & 4 deletions deferred/deferred.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { createDeferred } from './deferred.js';

test('deferred', () => {
jest.useFakeTimers();
const callback = jest.fn();
vi.useFakeTimers();
const callback = vi.fn();
const deferred = createDeferred(1000, callback);
deferred(1);
expect(callback).not.toHaveBeenCalled();
deferred(2);
expect(callback).not.toHaveBeenCalled();
jest.runAllTimers();
vi.runAllTimers();
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenLastCalledWith(2);
deferred(3);
Expand All @@ -17,6 +17,6 @@ test('deferred', () => {
expect(callback).toHaveBeenLastCalledWith(3);
deferred(4);
deferred.cancel();
jest.runAllTimers();
vi.runAllTimers();
expect(callback).toHaveBeenCalledTimes(2);
});
5 changes: 5 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import rational from 'eslint-config-rational';

export default rational({
ignores: ['**/node_modules/', '**/lib/', '**/out/', '**/.*/'],
});
6 changes: 3 additions & 3 deletions events/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ describe('Events', () => {

test('emit calls listeners', () => {
const events = new Events();
const a = jest.fn();
const b = jest.fn();
const a = vi.fn();
const b = vi.fn();
const off = events.on('event', a);
events.on('event', b);
events.emit('event', 1, 2);
Expand All @@ -29,7 +29,7 @@ describe('Events', () => {

test('emit only calls listeners for the emitted event', () => {
const events = new Events();
const a = jest.fn();
const a = vi.fn();
events.on('event', a);
events.emit('other');
expect(a).not.toHaveBeenCalled();
Expand Down
34 changes: 17 additions & 17 deletions fetch-sdk/fetch-sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ describe('createFetchSdk', () => {

beforeEach(() => {
res.status = 200;
(global as any).fetch = jest.fn().mockResolvedValue(res);
(global as any).fetch = vi.fn().mockResolvedValue(res);
(global as any).Request = class {
constructor(url: string, init: Record<string, string>) {
Object.assign(this, { url: String(url), ...init, headers: new Headers(init.headers as any) });
}
};
res.text = jest.fn().mockResolvedValue('');
res.json = jest.fn().mockResolvedValue({});
res.text = vi.fn().mockResolvedValue('');
res.json = vi.fn().mockResolvedValue({});
});

afterEach(() => {
Expand All @@ -41,7 +41,7 @@ describe('createFetchSdk', () => {
});

test('constructor with arguments', () => {
const definitions = jest.fn().mockReturnValue({});
const definitions = vi.fn().mockReturnValue({});
const Sdk = createFetchSdk(definitions);
new Sdk('foo', 123, true);

Expand Down Expand Up @@ -106,9 +106,9 @@ describe('createFetchSdk', () => {
body: 'foo',
}),
);
expect(Object.fromEntries((jest.mocked(fetch).mock.lastCall?.[0] as any).headers.entries())).toEqual({
'x-foo': 'foo',
});
const args = vi.mocked(fetch).mock.lastCall!;
const request = args[0] as unknown as Request;
expect(request.headers.get('x-foo')).toBe('foo');

await sdk.bar();
expect(fetch).toHaveBeenLastCalledWith(
Expand Down Expand Up @@ -146,9 +146,9 @@ describe('createFetchSdk', () => {
const Sdk = createFetchSdk({ foo: { url: 'https://example.com' } });
const sdk = new Sdk();
const reason = new Error('fetch');
jest.mocked(fetch).mockRejectedValue(reason);
vi.mocked(fetch).mockRejectedValue(reason);
await expect(sdk.foo()).rejects.toMatchObject({
message: expect.stringMatching(/^failed fetching /),
message: expect.stringMatching(/^failed fetching /u),
code: 'network_error',
reason,
});
Expand All @@ -158,7 +158,7 @@ describe('createFetchSdk', () => {
const Sdk = createFetchSdk({ foo: { url: 'https://example.com', accept: () => false } });
const sdk = new Sdk();
await expect(sdk.foo()).rejects.toMatchObject({
message: expect.stringMatching(/^failed fetching /),
message: expect.stringMatching(/^failed fetching /u),
code: 'unacceptable',
});
});
Expand All @@ -175,7 +175,7 @@ describe('createFetchSdk', () => {
});
const sdk = new Sdk();
await expect(sdk.foo()).rejects.toMatchObject({
message: expect.stringMatching(/^failed fetching /),
message: expect.stringMatching(/^failed fetching /u),
code: 'unacceptable',
reason,
});
Expand All @@ -186,7 +186,7 @@ describe('createFetchSdk', () => {
const Sdk = createFetchSdk({ foo: { url: 'https://example.com' } });
const sdk = new Sdk();
await expect(sdk.foo()).rejects.toMatchObject({
message: expect.stringMatching(/^failed fetching /),
message: expect.stringMatching(/^failed fetching /u),
code: 'unacceptable',
});
});
Expand All @@ -204,15 +204,15 @@ describe('createFetchSdk', () => {
);
const sdk = new Sdk();
await expect(sdk.foo()).rejects.toMatchObject({
message: expect.stringMatching(/^failed fetching /),
message: expect.stringMatching(/^failed fetching /u),
code: 'foo',
});
await expect(sdk.bar()).rejects.toMatchObject({
message: expect.stringMatching(/^failed fetching /),
message: expect.stringMatching(/^failed fetching /u),
code: 'bar',
});
await expect(sdk.baz()).rejects.toMatchObject({
message: expect.stringMatching(/^failed fetching /),
message: expect.stringMatching(/^failed fetching /u),
code: 1,
});
});
Expand All @@ -227,7 +227,7 @@ describe('createFetchSdk', () => {
});
const sdk = new Sdk();
await expect(sdk.foo()).rejects.toMatchObject({
message: expect.stringMatching(/^failed fetching /),
message: expect.stringMatching(/^failed fetching /u),
code: 'unacceptable',
});
});
Expand All @@ -242,7 +242,7 @@ describe('createFetchSdk', () => {
});
const sdk = new Sdk();
await expect(sdk.foo()).rejects.toMatchObject({
message: expect.stringMatching(/^failed fetching /),
message: expect.stringMatching(/^failed fetching /u),
code: 'parse_error',
reason,
});
Expand Down
20 changes: 10 additions & 10 deletions fetch-sdk/fetch-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ type FetchSdkInstance<TDefinitions extends FetchSdkRequests> = {
[P in keyof TDefinitions]: TDefinitions[P] extends string
? FetchSdkFunction<Response, []>
: TDefinitions[P] extends FetchSdkRequestFactory<any, infer TArgs>
? ReturnType<TDefinitions[P]> extends { parse(...args: any[]): Promise<infer TResult> }
? FetchSdkFunction<TResult, TArgs>
: FetchSdkFunction<Response, TArgs>
: TDefinitions[P] extends { parse(...args: any[]): Promise<infer TResult> }
? FetchSdkFunction<TResult, []>
: FetchSdkFunction<Response, []>;
? ReturnType<TDefinitions[P]> extends { parse(...args: any[]): Promise<infer TResult> }
? FetchSdkFunction<TResult, TArgs>
: FetchSdkFunction<Response, TArgs>
: TDefinitions[P] extends { parse(...args: any[]): Promise<infer TResult> }
? FetchSdkFunction<TResult, []>
: FetchSdkFunction<Response, []>;
};

type FetchSdkConstructor<TDefinitions extends FetchSdkRequests, TArgs extends any[]> = new (
Expand Down Expand Up @@ -172,10 +172,10 @@ class FetchSdkError extends Error {
* ```
*/
const createFetchSdk: {
<TDefinitions extends FetchSdkRequests>(definitions: TDefinitions, defaults?: FetchSdkDefaults): FetchSdkConstructor<
TDefinitions,
[]
>;
<TDefinitions extends FetchSdkRequests>(
definitions: TDefinitions,
defaults?: FetchSdkDefaults,
): FetchSdkConstructor<TDefinitions, []>;
<TDefinitions extends FetchSdkRequests, TArgs extends any[] = []>(
definitions: (...args: TArgs) => TDefinitions,
defaults?: FetchSdkDefaults,
Expand Down
16 changes: 8 additions & 8 deletions fsm/fsm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type FsmTransition<
> = TStates[TToState] extends undefined | void
? () => FsmState<TStates, TEdges, TToState>
: undefined extends TStates[TToState]
? (value?: TStates[TToState]) => FsmState<TStates, TEdges, TToState>
: (value: TStates[TToState]) => FsmState<TStates, TEdges, TToState>;
? (value?: TStates[TToState]) => FsmState<TStates, TEdges, TToState>
: (value: TStates[TToState]) => FsmState<TStates, TEdges, TToState>;

type FsmState<
TStates extends FsmStatesDefinition = Readonly<Record<FsmStateKey, any>>,
Expand Down Expand Up @@ -43,8 +43,8 @@ type Fsm<TStates extends FsmStatesDefinition = FsmStatesDefinition, TEdges exten
...args: TStates[TState] extends undefined | void
? readonly []
: undefined extends TStates[TState]
? readonly [value?: TStates[TState]]
: readonly [value: TStates[TState]]
? readonly [value?: TStates[TState]]
: readonly [value: TStates[TState]]
): FsmState<TStates, TEdges, TState>;
transition<
TAction extends string,
Expand All @@ -70,17 +70,17 @@ type Fsm<TStates extends FsmStatesDefinition = FsmStatesDefinition, TEdges exten
type InferFsmStateKeys<TFsm> = TFsm extends Fsm<infer TStates>
? keyof TStates
: TFsm extends FsmState<infer TStates>
? keyof TStates
: never;
? keyof TStates
: never;

type InferFsmStates<TFsm, TStateSubset extends InferFsmStateKeys<TFsm> = InferFsmStateKeys<TFsm>> = TFsm extends Fsm<
infer TStates,
infer TEdges
>
? FsmStatesUnion<TStates, TEdges, TStateSubset>
: TFsm extends FsmState<infer TStates, infer TEdges>
? FsmStatesUnion<TStates, TEdges, TStateSubset>
: never;
? FsmStatesUnion<TStates, TEdges, TStateSubset>
: never;

const createFsmState = (
transitions: Readonly<Record<FsmStateKey, Readonly<Record<string, FsmTransition>>>>,
Expand Down
2 changes: 1 addition & 1 deletion hello-world/hello-world.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { hello } from './hello-world.js';
describe('hello-world', () => {
['world', 'jane', 'john'].forEach((who) => {
test(`who: ${who}`, () => {
jest.spyOn(console, 'log').mockImplementation();
vi.spyOn(console, 'log').mockImplementation(() => undefined);
hello('world');
expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenLastCalledWith('Hello, world!');
Expand Down
31 changes: 0 additions & 31 deletions jest.config.cjs

This file was deleted.

12 changes: 0 additions & 12 deletions jest.setup.cjs

This file was deleted.

4 changes: 2 additions & 2 deletions limiter/limiter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { createLimiter } from './limiter.js';

const tasks: { reject(): void; resolve(): void }[] = [];
const nextTick = (): Promise<void> => new Promise(process.nextTick);
let task: jest.Mock;
let task: any;

beforeEach(() => {
tasks.length = 0;
task = jest
task = vi
.fn()
.mockImplementation(
(arg) =>
Expand Down
2 changes: 1 addition & 1 deletion limiter/limiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const createLimiter = (concurrency: number, options: LimiterOptions = {}): Limit
const update = (): void => {
while (!isPaused && active < safeConcurrency && queue.length) {
active += 1;
queue
void queue
.shift()
?.start()
.finally(() => {
Expand Down
Loading

0 comments on commit fbc1d6c

Please sign in to comment.