Skip to content

Commit

Permalink
feat(ng-dev): add staticTest()
Browse files Browse the repository at this point in the history
  • Loading branch information
ersimont committed Dec 18, 2021
1 parent f85fc6f commit e3ca8a2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions projects/integration/src/app/api-tests/ng-dev.spec.ts
Expand Up @@ -9,6 +9,7 @@ import {
logTimers,
marbleTest,
SlTestRequest,
staticTest,
TestCall,
} from '@s-libs/ng-dev';

Expand Down Expand Up @@ -56,4 +57,8 @@ describe('ng-dev', () => {
it('has marbleTest', () => {
expect(marbleTest).toBeDefined();
});

it('has staticTest', () => {
expect(staticTest).toBeDefined();
});
});
35 changes: 35 additions & 0 deletions projects/ng-dev/src/lib/static-test.spec.ts
@@ -0,0 +1,35 @@
import { noop } from '@s-libs/micro-dash';
import { staticTest } from './static-test';

let curSpec!: jasmine.SpecResult;
jasmine.getEnv().addReporter({
specStarted(result) {
curSpec = result;
},
});

describe('testTyping()', () => {
it('satisfies jasmine that the test expects something', () => {
staticTest(noop);
expect(curSpec.passedExpectations.length).toBe(1);
});

it('does not execute the code', () => {
staticTest(() => {
fail('this should not run');
});
});

describe('example from the docs', () => {
function reject<T>(array: T[], predicate: (value: T) => boolean): T[] {
return array.filter((value) => !predicate(value));
}

it('requires the predicate type to match the array type', () => {
staticTest(() => {
// @ts-expect-error -- mismatch of number array w/ string function
reject([1, 2, 3], (value: string) => value === '2');
});
});
});
});
18 changes: 18 additions & 0 deletions projects/ng-dev/src/lib/static-test.ts
@@ -0,0 +1,18 @@
/**
* Use this when you want to write test code that doesn't actually run, instead relying only on your static tools like Typescript or a linter to raise errors.
*
* ```ts
* function reject<T>(array: T[], predicate: (value: T) => boolean): T[] {
* return array.filter((value) => !predicate(value));
* }
*
* it('requires the predicate type to match the array type', () => {
* staticTest(() => {
* // @ts-expect-error -- mismatch of number array w/ string function
* reject([1, 2, 3], (value: string) => value === '2');
* });
* }); * ```
*/
export function staticTest(_test: VoidFunction): void {
expect().nothing();
}
1 change: 1 addition & 0 deletions projects/ng-dev/src/public-api.ts
Expand Up @@ -8,3 +8,4 @@ export * from './lib/component-context/index';
export * from './lib/test-requests/index';
export { logTimers } from './lib/log-timers';
export { marbleTest } from './lib/marble-test';
export { staticTest } from './lib/static-test';

0 comments on commit e3ca8a2

Please sign in to comment.