Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate type tests to TSTyche #1988

Merged
merged 5 commits into from Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions .github/workflows/ci.yml
Expand Up @@ -50,8 +50,8 @@ jobs:
- run: npm run type-check
- run: npm run check-git-clean

unit-test:
name: 'Build & Unit Test'
test:
name: 'Build & Unit Test & Type Test'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -65,7 +65,8 @@ jobs:
restore-keys: ${{ runner.OS }}-node-
- run: npm ci
- run: npm run build
- run: npm run unit-test
- run: npm run test:unit
- run: npm run test:types -- --target 4.5,5.0,current
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdeniau What you think? Locally test:types would test only on the installed TypeScript. The lowest supported version (4.5, resolves to 4.5.5) is checked on CI. I would definitely recommend adding 5.0 (resolves to 5.0.2), because each major brings in breaking changes. And currently installed.

latest could be added. It points to typescript@latest in NPM registry, hence it is a changing target. It might start failing with any unrelated PR, i.e. even without changes in types. That’s somewhat unpredictable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

latest (or even next) could work as a daily cron job. Just some food for though.

- run: npx size-limit
- run: npm run check-git-clean

Expand All @@ -90,7 +91,7 @@ jobs:

publish:
name: 'Publish'
needs: [lint, type-check, unit-test, website]
needs: [lint, type-check, test, website]
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
Expand Down
9 changes: 7 additions & 2 deletions .prettierignore
@@ -1,5 +1,10 @@
dist
pages/generated
pages/out
type-definitions/ts-tests/
type-definitions/flow-tests/
type-definitions/ts-tests/*.ts
type-definitions/flow-tests/

!type-definitions/ts-tests/covariance.ts
!type-definitions/ts-tests/deepCopy.ts
!type-definitions/ts-tests/empty.ts
!type-definitions/ts-tests/es6-collections.ts
31 changes: 31 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -55,7 +55,9 @@
"npm": ">=7.0.0"
},
"scripts": {
"test": "run-s format lint type-check build unit-test",
"test": "run-s format lint type-check build test:*",
"test:unit": "jest",
"test:types": "tstyche",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added test:types script to make it easy to pass extra options. For example, in CI the command could be test:types --target 4.5,5.0,current. Just a suggestion, it can be set up somehow else.

Currently type-check script is used. In early stages I saw TSTyche as a type checker (this is where the name is coming from), but it does more. It uses TypeScript to perform static analysis and file are not executed, but with .only / .skip and other features it feels more like a type test runner.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we do have this file telling that the minimum version is 4.5, and I think that we do only test 4.5 (not really sure about that, we might use the local typescript version).

It might be interesting to test multiple typescript version in CI (but not done actually, so if it can be do, it's great, but not a necessity).

Locally, I think that testing the local version of typescript is good enough.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I forgot to mention that current is currently (or locally) installed TypeScript. This is the default. I mean, tstyche is the same as tstyche --target current.

"format": "npm run lint:format -- --write",
"lint": "run-s lint:*",
"lint:format": "prettier --check \"{__tests__,src,type-definitions,website/src,perf,resources}/**/*{.js,.ts,.tsx,.flow,.css}\"",
Expand All @@ -69,7 +71,6 @@
"build:types": "cpy ./type-definitions/immutable.* dist",
"build:prepare": "./resources/prepare-dist.sh",
"build:stats": "node ./resources/dist-stats.mjs",
"unit-test": "jest",
"website:build": "cd website && next build && next-sitemap",
"website:dev": "cd website && next dev",
"check-git-clean": "./resources/check-git-clean.sh",
Expand Down Expand Up @@ -127,6 +128,7 @@
"rollup": "3.28.1",
"size-limit": "^8.2.6",
"transducers-js": "0.4.174",
"tstyche": "^1.0.0",
"typescript": "5.1"
},
"size-limit": [
Expand Down
9 changes: 9 additions & 0 deletions tstyche.config.json
@@ -0,0 +1,9 @@
{
"$schema": "https://tstyche.org/schemas/config.json",
"testFileMatch": [
"**/type-definitions/ts-tests/covariance.ts",
"**/type-definitions/ts-tests/deepCopy.ts",
"**/type-definitions/ts-tests/empty.ts",
"**/type-definitions/ts-tests/es6-collections.ts"
]
}
114 changes: 61 additions & 53 deletions type-definitions/ts-tests/covariance.ts
@@ -1,4 +1,13 @@
import { List, Map, OrderedMap, OrderedSet, Set, Stack } from 'immutable';
import { expect, test } from 'tstyche';
import {
List,
Map,
MapOf,
OrderedMap,
OrderedSet,
Set,
Stack,
} from 'immutable';

class A {
x: number;
Expand All @@ -7,6 +16,7 @@ class A {
this.x = 1;
}
}

class B extends A {
y: string;

Expand All @@ -15,6 +25,7 @@ class B extends A {
this.y = 'B';
}
}

class C {
z: string;

Expand All @@ -23,55 +34,52 @@ class C {
}
}

// List covariance
let listOfB: List<B> = List<B>();
let listOfA: List<A> = listOfB;

// $ExpectType List<B>
listOfA = List([new B()]);

// $ExpectError
let listOfC: List<C> = listOfB;

// Map covariance
declare let mapOfB: Map<string, B>;
let mapOfA: Map<string, A> = mapOfB;

// $ExpectType MapOf<{ b: B; }>
mapOfA = Map({ b: new B() });

// $ExpectError
let mapOfC: Map<string, C> = mapOfB;

// Set covariance
declare let setOfB: Set<B>;
let setOfA: Set<A> = setOfB;

// $ExpectType Set<B>
setOfA = Set([new B()]);
// $ExpectError
let setOfC: Set<C> = setOfB;

// Stack covariance
declare let stackOfB: Stack<B>;
let stackOfA: Stack<A> = stackOfB;
// $ExpectType Stack<B>
stackOfA = Stack([new B()]);
// $ExpectError
let stackOfC: Stack<C> = stackOfB;

// OrderedMap covariance
declare let orderedMapOfB: OrderedMap<string, B>;
let orderedMapOfA: OrderedMap<string, A> = orderedMapOfB;
// $ExpectType OrderedMap<string, B>
orderedMapOfA = OrderedMap({ b: new B() });
// $ExpectError
let orderedMapOfC: OrderedMap<string, C> = orderedMapOfB;

// OrderedSet covariance
declare let orderedSetOfB: OrderedSet<B>;
let orderedSetOfA: OrderedSet<A> = orderedSetOfB;
// $ExpectType OrderedSet<B>
orderedSetOfA = OrderedSet([new B()]);
// $ExpectError
let orderedSetOfC: OrderedSet<C> = orderedSetOfB;
test('List covariance', () => {
expect<List<A>>().type.toBeAssignable(List<B>());

expect(List([new B()])).type.toEqual<List<B>>();

expect<List<C>>().type.not.toBeAssignable(List<B>());
});

test('Map covariance', () => {
expect<Map<string, A>>().type.toBeAssignable<Map<string, B>>();

expect(Map({ b: new B() })).type.toEqual<MapOf<{ b: B }>>();

expect<Map<string, C>>().type.not.toBeAssignable<Map<string, B>>();
});

test('Set covariance', () => {
expect<Set<A>>().type.toBeAssignable<Set<B>>();

expect(Set([new B()])).type.toEqual<Set<B>>();

expect<Set<C>>().type.not.toBeAssignable<Set<B>>();
});

test('Stack covariance', () => {
expect<Stack<A>>().type.toBeAssignable<Stack<B>>();

expect(Stack([new B()])).type.toEqual<Stack<B>>();

expect<Stack<C>>().type.not.toBeAssignable<Stack<B>>();
});

test('OrderedMap covariance', () => {
expect<OrderedMap<string, A>>().type.toBeAssignable<OrderedMap<string, B>>();

expect(OrderedMap({ b: new B() })).type.toEqual<OrderedMap<string, B>>();

expect<OrderedMap<string, C>>().type.not.toBeAssignable<
OrderedMap<string, B>
>();
});

test('OrderedSet covariance', () => {
expect<OrderedSet<A>>().type.toBeAssignable<OrderedSet<B>>();

expect(OrderedSet([new B()])).type.toEqual<OrderedSet<B>>();

expect<OrderedSet<C>>().type.not.toBeAssignable<OrderedSet<B>>();
});