Skip to content

Commit

Permalink
chore: migrate type test of jest-worker to TSTyche (#14949)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas authored Mar 11, 2024
1 parent 15ae0bc commit 9113f44
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 58 deletions.
1 change: 1 addition & 0 deletions jest.config.ts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default {
'!**/packages/jest-mock/__typetests__/*.test.ts',
'!**/packages/jest-snapshot/__typetests__/*.test.ts',
'!**/packages/jest-types/__typetests__/config.test.ts',
'!**/packages/jest-worker/__typetests__/*.test.ts',
],
},
],
Expand Down
118 changes: 60 additions & 58 deletions packages/jest-worker/__typetests__/jest-worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,96 +5,98 @@
* LICENSE file in the root directory of this source tree.
*/

import {expectError, expectNotAssignable, expectType} from 'tsd-lite';
import {expect, test} from 'tstyche';
import type {JestWorkerFarm} from 'jest-worker';
import type * as testWorker from './testWorker';

type TestWorker = {
runTest: (a: string, b: number) => void;
doSomething: () => void;
isResult: boolean;
end: () => void; // reserved keys should be excluded from returned type

// the reserved keys should not be included in the resulting type
end: () => void;
getStderr: () => string;
getStdout: () => string;
setup: () => void;
teardown: () => void;
};

// unknown JestWorkerFarm

declare const unknownWorkerFarm: JestWorkerFarm<Record<string, unknown>>;

expectError(unknownWorkerFarm.runTest());
expectError(unknownWorkerFarm.runTestAsync());

expectError(unknownWorkerFarm.getResult());
expectError(unknownWorkerFarm.isResult);
test('unknown JestWorkerFarm', () => {
expect(unknownWorkerFarm).type.not.toHaveProperty('runTest');
expect(unknownWorkerFarm).type.not.toHaveProperty('runTestAsync');

expectError(unknownWorkerFarm.setup());
expectError(unknownWorkerFarm.teardown());
expect(unknownWorkerFarm).type.not.toHaveProperty('getResult');
expect(unknownWorkerFarm).type.not.toHaveProperty('isResult');

expectType<Promise<{forceExited: boolean}>>(unknownWorkerFarm.end());
expectType<NodeJS.ReadableStream>(unknownWorkerFarm.getStderr());
expectType<NodeJS.ReadableStream>(unknownWorkerFarm.getStdout());
expect(unknownWorkerFarm).type.not.toHaveProperty('setup');
expect(unknownWorkerFarm).type.not.toHaveProperty('teardown');

// detected JestWorkerFarm
expect(unknownWorkerFarm.start()).type.toEqual<Promise<void>>();
expect(unknownWorkerFarm.end()).type.toEqual<
Promise<{forceExited: boolean}>
>();

declare const detectedWorkerFarm: JestWorkerFarm<typeof testWorker>;
expect(unknownWorkerFarm.getStderr()).type.toEqual<NodeJS.ReadableStream>();
expect(unknownWorkerFarm.getStdout()).type.toEqual<NodeJS.ReadableStream>();
});

expectType<Promise<void>>(detectedWorkerFarm.runTest('abc', true));
expectType<Promise<void>>(detectedWorkerFarm.runTestAsync(123, 456));
declare const inferredWorkerFarm: JestWorkerFarm<typeof testWorker>;

expectType<Promise<void>>(detectedWorkerFarm.doSomething());
expectType<Promise<void>>(detectedWorkerFarm.doSomething());
expectType<Promise<void>>(detectedWorkerFarm.doSomethingAsync());
expectType<Promise<void>>(detectedWorkerFarm.doSomethingAsync());
test('inferred JestWorkerFarm', () => {
expect(inferredWorkerFarm.runTest('abc', true)).type.toEqual<Promise<void>>();
expect(inferredWorkerFarm.runTestAsync(123, 456)).type.toEqual<
Promise<void>
>();

expectError(detectedWorkerFarm.runTest());
expectError(detectedWorkerFarm.runTest('abc'));
expectError(detectedWorkerFarm.runTestAsync());
expectError(detectedWorkerFarm.runTestAsync(123));
expectError(detectedWorkerFarm.doSomething(123));
expectError(detectedWorkerFarm.doSomethingAsync('abc'));
expect(inferredWorkerFarm.doSomething()).type.toEqual<Promise<void>>();
expect(inferredWorkerFarm.doSomething()).type.toEqual<Promise<void>>();
expect(inferredWorkerFarm.doSomethingAsync()).type.toEqual<Promise<void>>();
expect(inferredWorkerFarm.doSomethingAsync()).type.toEqual<Promise<void>>();

expectError(detectedWorkerFarm.getResult());
expectError(detectedWorkerFarm.isResult);
expect(inferredWorkerFarm.runTest()).type.toRaiseError();
expect(inferredWorkerFarm.runTest('abc')).type.toRaiseError();
expect(inferredWorkerFarm.runTestAsync()).type.toRaiseError();
expect(inferredWorkerFarm.runTestAsync(123)).type.toRaiseError();
expect(inferredWorkerFarm.doSomething(123)).type.toRaiseError();
expect(inferredWorkerFarm.doSomethingAsync('abc')).type.toRaiseError();

expectError(detectedWorkerFarm.setup());
expectError(detectedWorkerFarm.teardown());
expect(inferredWorkerFarm).type.not.toHaveProperty('getResult');
expect(inferredWorkerFarm).type.not.toHaveProperty('isResult');

expectNotAssignable<Promise<void>>(detectedWorkerFarm.end());
expectType<Promise<{forceExited: boolean}>>(detectedWorkerFarm.end());
expect(inferredWorkerFarm).type.not.toHaveProperty('setup');
expect(inferredWorkerFarm).type.not.toHaveProperty('teardown');

expectNotAssignable<Promise<string>>(detectedWorkerFarm.getStderr());
expectType<NodeJS.ReadableStream>(detectedWorkerFarm.getStderr());
expect(inferredWorkerFarm.start()).type.toEqual<Promise<void>>();
expect(inferredWorkerFarm.end()).type.toEqual<
Promise<{forceExited: boolean}>
>();

expectNotAssignable<Promise<string>>(detectedWorkerFarm.getStdout());
expectType<NodeJS.ReadableStream>(detectedWorkerFarm.getStdout());

// typed JestWorkerFarm
expect(inferredWorkerFarm.getStderr()).type.toEqual<NodeJS.ReadableStream>();
expect(inferredWorkerFarm.getStdout()).type.toEqual<NodeJS.ReadableStream>();
});

declare const typedWorkerFarm: JestWorkerFarm<TestWorker>;

expectType<Promise<void>>(typedWorkerFarm.runTest('abc', 123));
expectType<Promise<void>>(typedWorkerFarm.doSomething());

expectError(typedWorkerFarm.runTest());
expectError(typedWorkerFarm.runTest('abc'));
expectError(typedWorkerFarm.doSomething('abc'));

expectError(typedWorkerFarm.isResult);
expectError(typedWorkerFarm.runTestAsync());
test('typed JestWorkerFarm', () => {
expect(typedWorkerFarm.runTest('abc', 123)).type.toEqual<Promise<void>>();
expect(typedWorkerFarm.doSomething()).type.toEqual<Promise<void>>();

expectError(typedWorkerFarm.setup());
expectError(typedWorkerFarm.teardown());
expect(typedWorkerFarm.runTest()).type.toRaiseError();
expect(typedWorkerFarm.runTest('abc')).type.toRaiseError();
expect(typedWorkerFarm.doSomething('abc')).type.toRaiseError();

expectType<Promise<void>>(typedWorkerFarm.start());
expect(typedWorkerFarm).type.not.toHaveProperty('isResult');
expect(typedWorkerFarm).type.not.toHaveProperty('runTestAsync');

expectNotAssignable<Promise<void>>(typedWorkerFarm.end());
expectType<Promise<{forceExited: boolean}>>(typedWorkerFarm.end());
expect(typedWorkerFarm).type.not.toHaveProperty('setup');
expect(typedWorkerFarm).type.not.toHaveProperty('teardown');

expectNotAssignable<Promise<string>>(typedWorkerFarm.getStderr());
expectType<NodeJS.ReadableStream>(typedWorkerFarm.getStderr());
expect(typedWorkerFarm.start()).type.toEqual<Promise<void>>();
expect(typedWorkerFarm.end()).type.toEqual<Promise<{forceExited: boolean}>>();

expectNotAssignable<Promise<string>>(typedWorkerFarm.getStdout());
expectType<NodeJS.ReadableStream>(typedWorkerFarm.getStdout());
expect(typedWorkerFarm.getStderr()).type.toEqual<NodeJS.ReadableStream>();
expect(typedWorkerFarm.getStdout()).type.toEqual<NodeJS.ReadableStream>();
});
1 change: 1 addition & 0 deletions tstyche.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"**/packages/jest-expect/__typetests__/*.test.ts",
"**/packages/jest-mock/__typetests__/*.test.ts",
"**/packages/jest-snapshot/__typetests__/*.test.ts",
"**/packages/jest-worker/__typetests__/*.test.ts"
"**/packages/jest-types/__typetests__/config.test.ts"
]
}

0 comments on commit 9113f44

Please sign in to comment.