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

chore: migrate type tests to TSTyche #9394

Merged
merged 15 commits into from
Nov 15, 2023
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ jobs:
- name: 🔎 Lint
run: yarn lint

- name: 🌡 Test Types
run: yarn test:types --target '4.6,5.0,latest' # commas has to be escaped in PowerShell

- name: Get number of CPU cores
if: always()
id: cpu-cores
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"smoke-test": "node ./tasks/smoke-tests/smoke-tests.mjs",
"test": "lerna run test --concurrency 2 -- --colors --maxWorkers=4",
"test-ci": "lerna run test --concurrency 2 -- --colors --maxWorkers",
"test:k6": "tsx ./tasks/k6-test/run-k6-tests.mts"
"test:k6": "tsx ./tasks/k6-test/run-k6-tests.mts",
"test:types": "tstyche"
},
"resolutions": {
"vscode-languageserver": "6.1.1",
Expand Down Expand Up @@ -65,7 +66,6 @@
"@testing-library/jest-dom": "5.16.5",
"@testing-library/react": "14.0.0",
"@testing-library/user-event": "14.4.3",
"@tsd/typescript": "5.1.6",
"@types/babel__generator": "7.6.5",
"@types/fs-extra": "11.0.1",
"@types/jest": "29.5.4",
Expand All @@ -85,7 +85,6 @@
"fast-glob": "3.3.1",
"fs-extra": "11.1.1",
"jest": "29.7.0",
"jest-runner-tsd": "5.0.0",
"jscodeshift": "0.15.0",
"lerna": "7.4.2",
"listr2": "6.6.1",
Expand All @@ -100,6 +99,7 @@
"ora": "6.3.1",
"prompts": "2.4.2",
"rimraf": "5.0.5",
"tstyche": "1.0.0-beta.2",
"tsx": "3.12.7",
"typescript": "5.2.2",
"yargs": "17.7.2",
Expand Down
8 changes: 0 additions & 8 deletions packages/router/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,5 @@ module.exports = {
testEnvironment: 'jest-environment-jsdom',
testMatch: ['**/*.test.+(ts|tsx|js|jsx)', '!**/__typetests__/*.ts'],
},
{
displayName: {
color: 'blue',
name: 'types',
},
runner: 'jest-runner-tsd',
testMatch: ['**/__typetests__/*.test.ts'],
},
],
}
2 changes: 2 additions & 0 deletions packages/router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"build:watch": "nodemon --watch src --ext \"js,jsx,ts,tsx\" --ignore dist --exec \"yarn build\"",
"prepublishOnly": "NODE_ENV=production yarn build",
"test": "jest",
"test:types": "tstyche",
"test:watch": "yarn test --watch"
},
"dependencies": {
Expand All @@ -35,6 +36,7 @@
"jest": "29.7.0",
"react": "0.0.0-experimental-e5205658f-20230913",
"react-dom": "0.0.0-experimental-e5205658f-20230913",
"tstyche": "1.0.0-beta.2",
"typescript": "5.2.2"
},
"peerDependencies": {
Expand Down
56 changes: 28 additions & 28 deletions packages/router/src/__typetests__/routeParamsTypes.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { expectAssignable, expectType } from 'tsd-lite'
import { context, describe, expect, test } from 'tstyche'

import type { RouteParams, ParamType } from '../routeParamsTypes'

/**
* FAQ:
* - why aren't you using expectAssignable in all tests?
* - why aren't you using .toBeAssignable() in all tests?
* because {b: string} is assignable to Record, and then test isn't accurate enough
*
* - why aren't you just checking the entire type?
Expand All @@ -21,7 +21,7 @@ describe('RouteParams<>', () => {
id: 2,
}

expectType<number>(simple.id)
expect(simple.id).type.toBeNumber()
})

test('Starts with parameter', () => {
Expand All @@ -30,8 +30,8 @@ describe('RouteParams<>', () => {
driver: 44,
}

expectType<number>(startParam.driver)
expectType<number>(startParam.position)
expect(startParam.driver).type.toBeNumber()
expect(startParam.position).type.toBeNumber()
})

test('Route string with no types defaults to string', () => {
Expand All @@ -42,26 +42,26 @@ describe('RouteParams<>', () => {
slug: 'hello-world',
}

expectType<string>(untypedParams.year)
expectType<string>(untypedParams.month)
expectType<string>(untypedParams.day)
expectType<string>(untypedParams.slug)
expect(untypedParams.year).type.toBeString()
expect(untypedParams.month).type.toBeString()
expect(untypedParams.day).type.toBeString()
expect(untypedParams.slug).type.toBeString()
})

test('Custom param types', () => {
const customParams: RouteParams<'/post/{name:slug}'> = {
name: 'hello-world-slug',
}

expectType<string>(customParams.name)
expect(customParams.name).type.toBeString()
})

test('Parameter inside string', () => {
const stringConcat: RouteParams<'/signedUp/e{status:Boolean}y'> = {
status: true,
}

expectType<boolean>(stringConcat.status)
expect(stringConcat.status).type.toBeBoolean()
})

test('Multiple Glob route params', () => {
Expand All @@ -70,33 +70,33 @@ describe('RouteParams<>', () => {
toDate: '2021/11/17',
}

expectType<string>(globRoutes.fromDate)
expectType<string>(globRoutes.toDate)
expect(globRoutes.fromDate).type.toBeString()
expect(globRoutes.toDate).type.toBeString()
})

test('Single Glob route params', () => {
const globRoutes: RouteParams<'/from/{fromDate...}'> = {
fromDate: '2021/11/03',
}

expectType<string>(globRoutes.fromDate)
expect(globRoutes.fromDate).type.toBeString()
})

test('Starts with Glob route params', () => {
const globRoutes: RouteParams<'/{description...}-little/kittens'> = {
description: 'cute',
}

expectType<string>(globRoutes.description)
expect(globRoutes.description).type.toBeString()
})

test('Glob params in the middle', () => {
context('Glob params in the middle', () => {
test('Multiple Glob route params', () => {
const middleGlob: RouteParams<'/repo/{folders...}/edit'> = {
folders: 'src/lib/auth.js',
}

expectType<string>(middleGlob.folders)
expect(middleGlob.folders).type.toBeString()
})
})

Expand All @@ -111,11 +111,11 @@ describe('RouteParams<>', () => {
c: 'stringy-string',
}

expectType<string>(untypedFirst.b)
expectType<boolean>(untypedFirst.c)
expect(untypedFirst.b).type.toBeString()
expect(untypedFirst.c).type.toBeBoolean()

expectType<number>(typedFirst.b)
expectType<string>(typedFirst.c)
expect(typedFirst.b).type.toBeNumber()
expect(typedFirst.c).type.toBeString()
})

test('Params in the middle', () => {
Expand All @@ -125,26 +125,26 @@ describe('RouteParams<>', () => {
id: 10,
}

expectType<string>(paramsInTheMiddle.authorId)
expectType<number>(paramsInTheMiddle.id)
expect(paramsInTheMiddle.authorId).type.toBeString()
expect(paramsInTheMiddle.id).type.toBeNumber()
})
})

describe('ParamType<>', () => {
test('Float', () => {
expectAssignable<ParamType<'Float'>>(1.02)
expect<ParamType<'Float'>>().type.toBeAssignable(1.02)
})

test('Boolean', () => {
expectAssignable<ParamType<'Boolean'>>(true)
expectAssignable<ParamType<'Boolean'>>(false)
expect<ParamType<'Boolean'>>().type.toBeAssignable(true)
expect<ParamType<'Boolean'>>().type.toBeAssignable(false)
})

test('Int', () => {
expectAssignable<ParamType<'Int'>>(51)
expect<ParamType<'Int'>>().type.toBeAssignable(51)
})

test('String', () => {
expectAssignable<ParamType<'String'>>('bazinga')
expect<ParamType<'String'>>().type.toBeAssignable('bazinga')
})
})
8 changes: 5 additions & 3 deletions packages/router/src/__typetests__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"composite": false,
"incremental": true,
"declaration": false,
"declarationMap": false,
"emitDeclarationOnly": false
"emitDeclarationOnly": false,
"types": []
},
"include": ["./"],
"exclude": [],
"references": [{"path": "../../"}]
Comment on lines -4 to +11
Copy link
Contributor Author

Choose a reason for hiding this comment

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

composite was a problem before, but it works as expected with TSTyche. include, exclude, references must be here too, because root TSConfig excludes **/*.test.*.

}
5 changes: 0 additions & 5 deletions packages/router/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
}
},
"include": ["src", "./ambient.d.ts"],
"exclude": [
"dist",
"node_modules",
"**/__mocks__",
],
"references": [
{ "path": "../auth" },
]
Expand Down
8 changes: 0 additions & 8 deletions packages/web/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,5 @@ module.exports = {
RWJS_ENV: {},
},
},
{
displayName: {
color: 'blue',
name: 'types',
},
runner: 'jest-runner-tsd',
testMatch: ['**/__typetests__/*.test.+(ts|tsx|js|jsx)'],
},
],
}
3 changes: 2 additions & 1 deletion packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"build:watch": "nodemon --watch src --ext \"js,jsx,ts,tsx\" --ignore dist --exec \"yarn build\"",
"prepublishOnly": "NODE_ENV=production yarn build",
"test": "jest",
"test:types": "tstyche",
"test:watch": "yarn test --watch"
},
"dependencies": {
Expand All @@ -57,10 +58,10 @@
"@types/react-dom": "18.2.6",
"@types/testing-library__jest-dom": "5.14.8",
"jest": "29.7.0",
"jest-runner-tsd": "5.0.0",
"nodemon": "2.0.22",
"react": "0.0.0-experimental-e5205658f-20230913",
"react-dom": "0.0.0-experimental-e5205658f-20230913",
"tstyche": "1.0.0-beta.2",
"typescript": "5.2.2"
},
"peerDependencies": {
Expand Down
14 changes: 7 additions & 7 deletions packages/web/src/__typetests__/cellProps.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import React from 'react'

import gql from 'graphql-tag'
import { expectAssignable } from 'tsd-lite'
import { describe, expect, test } from 'tstyche'

import type { CellProps, CellSuccessProps } from '@redwoodjs/web'

Expand Down Expand Up @@ -68,7 +68,7 @@ describe('CellProps mapper type', () => {
ExampleQueryVariables
>

expectAssignable<CellInputs>({
expect<CellInputs>().type.toBeAssignable({
customProp: 55,
category: 'Dinner',
saved: true,
Expand All @@ -83,7 +83,7 @@ describe('CellProps mapper type', () => {
EmptyVariables
>

expectAssignable<CellWithoutVariablesInputs>({
expect<CellWithoutVariablesInputs>().type.toBeAssignable({
customProp: 55,
})
})
Expand Down Expand Up @@ -111,7 +111,7 @@ describe('CellProps mapper type', () => {
>

// Note that the gql variables are no longer required here
expectAssignable<CellWithBeforeQueryInputs>({
expect<CellWithBeforeQueryInputs>().type.toBeAssignable({
word: 'abracadabra',
customProp: 99,
})
Expand All @@ -134,7 +134,7 @@ describe('CellProps mapper type', () => {
EmptyVariables
>

expectAssignable<CellWithBeforeQueryInputs>({
expect<CellWithBeforeQueryInputs>().type.toBeAssignable({
fetchPolicy: 'cache-only',
customProp: 55,
})
Expand Down Expand Up @@ -163,7 +163,7 @@ describe('CellProps mapper type', () => {
>

// Note that the gql variables are no longer required here
expectAssignable<CellWithBeforeQueryInputs>({
expect<CellWithBeforeQueryInputs>().type.toBeAssignable({
customProp: 99,
})
})
Expand All @@ -185,7 +185,7 @@ describe('CellProps mapper type', () => {
EmptyVariables
>

expectAssignable<CellWithBeforeQueryInputs>({
expect<CellWithBeforeQueryInputs>().type.toBeAssignable({
customProp: 55,
})
})
Expand Down
13 changes: 8 additions & 5 deletions packages/web/src/__typetests__/cellSuccessData.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expectType } from 'tsd-lite'
import { describe, expect, it } from 'tstyche'

import type { CellSuccessData } from '@redwoodjs/web'

Expand All @@ -9,13 +9,13 @@ describe('CellSuccessData', () => {
foo: '',
}

expectType<{ foo: string }>(value)
expect<{ foo: string }>().type.toBeAssignable(value)
})

it('removes null and undefined from properties', () => {
const value: CellSuccessData<{ foo?: string | null }> = { foo: '' }

expectType<{ foo: string }>(value)
expect<{ foo: string }>().type.toBeAssignable(value)
})
})

Expand All @@ -30,7 +30,7 @@ describe('CellSuccessData', () => {
bar: '',
}

expectType<{ foo: string; bar: string }>(value)
expect<{ foo: string; bar: string }>().type.toBeAssignable(value)
})

it('does not remove null or undefined from properties', () => {
Expand All @@ -42,7 +42,10 @@ describe('CellSuccessData', () => {
bar: '',
}

expectType<{ foo?: string | null; bar?: string | null }>(value)
expect<{
foo?: string | null
bar?: string | null
}>().type.toBeAssignable(value)
})
})
})
Loading