Skip to content

Commit

Permalink
✅ Add tests for typings based on tsd (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed Nov 6, 2019
1 parent 1e67c8e commit 98a1526
Show file tree
Hide file tree
Showing 4 changed files with 825 additions and 14 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ jobs:
- yarn format:check
- yarn lint:check
- stage: test
env: STEP=EXAMPLES_ROLLUP_LEGACY
env: STEP=EXAMPLES_ROLLUP_LEGACY_TYPES
script:
- yarn prebuild
- yarn build:publish-types
- yarn build:publish-cjs
- yarn build:publish-esm
- yarn build:publish-types
- yarn webbuild
- yarn link
- cd example ;
Expand All @@ -54,6 +54,7 @@ jobs:
cd ..
- yarn test:rollup-esm
- yarn test:rollup-iife
- yarn test:type
- nvm install 0.12 ;
node test/legacy/main.js
- stage: test
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"e2e:watch": "jest --config jest.e2e.config.js --watch",
"test:rollup-esm": "rollup --config test/rollup/esm/rollup.config.js && node test/rollup/esm/dist/main.js",
"test:rollup-iife": "rollup --config test/rollup/iife/rollup.config.js && node test/rollup/iife/dist/main.js",
"test:type": "tsd",
"coverage": "cat ./coverage/lcov.info | coveralls",
"docs": "mkdir -p docs/2-API && docsify-init --editDir documentation && cp -R documentation/* docs/ && api-extractor run --local && generate-ts-docs markdown -i docs/2-API -o docs/2-API && cat README.md | sed 's/https:\\/\\/github.com\\/dubzzz\\/fast-check\\/blob\\/master\\/documentation//g' > docs/README.md && markdownbars -i docs/_sidebar.md -o docs/_sidebar.md",
"format": "prettier --write \"**/*.{js,ts}\"",
Expand Down Expand Up @@ -75,6 +76,7 @@
"source-map-support": "^0.5.13",
"ts-jest": "^24.1.0",
"ts-node": "^8.4.1",
"tsd": "^0.10.0",
"tslint-config-prettier": "^1.18.0",
"typescript": "^3.6.4",
"uglifyjs": "^2.4.11"
Expand All @@ -87,5 +89,8 @@
"quickcheck",
"jscheck",
"jsverify"
]
],
"tsd": {
"directory": "test/type"
}
}
90 changes: 90 additions & 0 deletions test/type/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { expectType, expectError } from 'tsd';
import * as fc from '../../src/fast-check';

// assert
expectType<void>(fc.assert(fc.property(fc.nat(), () => {})));
expectType<Promise<void>>(fc.assert(fc.asyncProperty(fc.nat(), async () => {})));

// property
expectType(fc.property(fc.nat(), a => {}) as fc.IProperty<[number]>);
expectType(fc.property(fc.nat(), fc.string(), (a, b) => {}) as fc.IProperty<[number, string]>);
expectError(fc.property(fc.nat(), fc.string(), (a: number, b: number) => {}));

// asyncProperty
expectType(fc.asyncProperty(fc.nat(), async a => {}) as fc.IAsyncProperty<[number]>);
expectType(fc.asyncProperty(fc.nat(), fc.string(), async (a, b) => {}) as fc.IAsyncProperty<[number, string]>);
expectError(fc.asyncProperty(fc.nat(), fc.string(), async (a: number, b: number) => {}));

// record arbitrary
expectType<fc.Arbitrary<{ a: number; b: string }>>(fc.record({ a: fc.nat(), b: fc.string() }));
expectType<fc.Arbitrary<{ a?: number; b?: string }>>(
fc.record({ a: fc.nat(), b: fc.string() }, { withDeletedKeys: true })
);
expectError(fc.record({ a: 1 }));

// dictionary arbitrary
expectType<fc.Arbitrary<Record<string, number>>>(fc.dictionary(fc.string(), fc.nat()));
expectError(fc.dictionary(fc.nat(), fc.nat()));

// tuple arbitrary
expectType<fc.Arbitrary<[number]>>(fc.tuple(fc.nat()));
expectType<fc.Arbitrary<[number, string]>>(fc.tuple(fc.nat(), fc.string()));
expectError(fc.tuple(fc.nat(), ''));

// oneof arbitrary
expectType<fc.Arbitrary<string>>(fc.oneof(fc.string(), fc.fullUnicodeString()));
expectType<fc.Arbitrary<number | string>>(fc.oneof(fc.string() as fc.Arbitrary<number | string>, fc.nat()));
expectError(fc.oneof(fc.string(), fc.nat())); // TODO Typings should be improved
expectError(fc.oneof(fc.string(), '1'));

// frequency arbitrary
expectType<fc.Arbitrary<string>>(
fc.frequency({ arbitrary: fc.string(), weight: 1 }, { arbitrary: fc.fullUnicodeString(), weight: 1 })
);
expectType<fc.Arbitrary<number | string>>(
fc.frequency(
{ arbitrary: fc.string() as fc.Arbitrary<number | string>, weight: 1 },
{ arbitrary: fc.nat(), weight: 1 }
)
);
expectError(fc.frequency({ arbitrary: fc.string(), weight: 1 }, { arbitrary: fc.nat(), weight: 1 })); // TODO Typings should be improved
expectError(fc.frequency({ arbitrary: fc.string(), weight: 1 }, { arbitrary: '1', weight: 1 }));

// option arbitrary
expectType<fc.Arbitrary<number | null>>(fc.option(fc.nat()));
expectType<fc.Arbitrary<number | null>>(fc.option(fc.nat(), { nil: null }));
expectType<fc.Arbitrary<number | 'custom_default'>>(fc.option(fc.nat(), { nil: 'custom_default' as const }));
expectError(fc.option(1));

// tie arbitrary
expectType<{}>(fc.letrec(tie => ({})));
expectType<{ a: fc.Arbitrary<number>; b: fc.Arbitrary<string> }>(
fc.letrec(tie => ({
a: fc.nat(),
b: fc.string()
}))
);
expectType<{ a: fc.Arbitrary<number>; b: fc.Arbitrary<unknown> }>(
fc.letrec(tie => ({
a: fc.nat(),
b: tie('a')
}))
); // TODO Typings should be improved: b type might be infered from a
expectType<{ a: fc.Arbitrary<number>; b: fc.Arbitrary<unknown> }>(
fc.letrec(tie => ({
a: fc.nat(),
b: tie('c')
}))
); // TODO Typings should be improved: referencing an undefined key should failed

// dedup arbitrary
expectType<fc.Arbitrary<[]>>(fc.dedup(fc.nat(), 0));
expectType<fc.Arbitrary<[number]>>(fc.dedup(fc.nat(), 1));
expectType<fc.Arbitrary<[number, number]>>(fc.dedup(fc.nat(), 2));
expectType<fc.Arbitrary<[number, number, number]>>(fc.dedup(fc.nat(), 3));
expectType<fc.Arbitrary<[number, number, number, number]>>(fc.dedup(fc.nat(), 4));
expectType<fc.Arbitrary<number[]>>(fc.dedup(fc.nat(), 5)); // TODO Typings should be improved: handle any number of values

// func arbitrary
expectType<fc.Arbitrary<() => number>>(fc.func(fc.nat()));
expectError(fc.func(1));
Loading

0 comments on commit 98a1526

Please sign in to comment.