Skip to content

Commit

Permalink
Migrate type tests to TSTyche (#1988)
Browse files Browse the repository at this point in the history
* chore: migrate type tests to TSTyche

* fix CI

* tweak CI

* add `empty.ts`

* add `es6-collections.ts`
  • Loading branch information
mrazauskas committed Feb 21, 2024
1 parent ac05955 commit 5c32c5d
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 235 deletions.
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
- 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",
"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>>();
});

0 comments on commit 5c32c5d

Please sign in to comment.