Skip to content

Commit

Permalink
Adds TypeScript usage validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Feb 13, 2021
1 parent 3184f07 commit dbd5bef
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 42 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
"start": "cross-env NODE_ENV=development rollup -c rollup.config.ts -w",
"clean": "rimraf lib node/**/*.js",
"lint": "eslint \"{cli,config,src,test}/**/*.ts\"",
"build": "yarn clean && cross-env NODE_ENV=production rollup -c rollup.config.ts",
"build": "yarn clean && yarn lint && cross-env NODE_ENV=production rollup -c rollup.config.ts && yarn test:ts",
"test": "yarn test:unit && yarn test:integration",
"test:unit": "cross-env BABEL_ENV=test jest --runInBand",
"test:integration": "node --max_old_space_size=8000 node_modules/jest/bin/jest.js --config=test/jest.config.js --runInBand",
"test:smoke": "config/scripts/smoke.sh",
"test:ts": "tsc -p test/typings/tsconfig.json",
"test:focused": "node node_modules/ts-node/dist/bin.js --project=test/tsconfig.json test/focusedTest.ts",
"prepublishOnly": "yarn lint && yarn test:unit && yarn build && yarn test:integration"
},
Expand Down
2 changes: 1 addition & 1 deletion src/context/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ export const errors = <
const prevBody = jsonParse(res.body) || {}
const nextBody = mergeRight(prevBody, { errors: errorsList })

return json(nextBody)(res)
return json(nextBody)(res as any) as any
}
}
4 changes: 2 additions & 2 deletions src/context/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { ResponseTransformer } from '../response'
*/
export const json = <BodyTypeJSON>(
body: BodyTypeJSON,
): ResponseTransformer<string> => {
): ResponseTransformer<BodyTypeJSON> => {
return (res) => {
res.headers.set('Content-Type', 'application/json')
res.body = JSON.stringify(body)
res.body = JSON.stringify(body) as any

return res
}
Expand Down
31 changes: 0 additions & 31 deletions src/graphql.test-d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function createScopedGraphQLHandler(
url: Mask,
) {
return <
QueryType,
QueryType extends Record<string, any>,
VariablesType extends GraphQLVariablesType = GraphQLVariablesType
>(
operationName: GraphQLHandlerNameSelector,
Expand All @@ -29,7 +29,7 @@ function createScopedGraphQLHandler(

function createGraphQLOperationHandler(url: Mask) {
return <
QueryType,
QueryType extends Record<string, any>,
VariablesType extends GraphQLVariablesType = GraphQLVariablesType
>(
resolver: ResponseResolver<
Expand Down
5 changes: 3 additions & 2 deletions src/handlers/RestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ export type RequestQuery = {
}

export interface RestRequestType<
BodyType extends DefaultRequestBodyType = DefaultRequestBodyType,
ParamsType extends RequestParams = Record<string, any>
> extends MockedRequest {
> extends MockedRequest<BodyType> {
params: ParamsType
}

Expand Down Expand Up @@ -146,7 +147,7 @@ ${queryParams
protected getPublicRequest(
request: RequestType,
parsedResult: ParsedResult,
): RestRequestType<RequestParams> {
): RestRequestType<any, RequestParams> {
return {
...request,
params: parsedResult.params,
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export { graphql } from './graphql'
export {
GraphQLHandler,
GraphQLContext,
GraphQLVariablesType,
GraphQLRequestType,
GraphQLRequestBodyType,
GraphQLJsonRequestBody,
graphqlContext,
} from './handlers/GraphQLHandler'

Expand Down
6 changes: 3 additions & 3 deletions src/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ function createRestHandler(method: RESTMethods) {
/**
* @fixme Propagate request body type generic to the handler.
*/
RequestBodyType = DefaultRequestBodyType,
ResponseBodyType = any,
RequestBodyType extends DefaultRequestBodyType = DefaultRequestBodyType,
ResponseBodyType extends DefaultRequestBodyType = any,
RequestParamsType extends RequestParams = RequestParams
>(
mask: Mask,
resolver: ResponseResolver<
RestRequestType<RequestParamsType>,
RestRequestType<RequestBodyType, RequestParamsType>,
RestContext,
ResponseBodyType
>,
Expand Down
52 changes: 52 additions & 0 deletions test/typings/graphql.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { graphql } from 'msw'

graphql.query<{ key: string }>('', (req, res, ctx) => {
return res(
ctx.data(
// @ts-expect-error Response data doesn't match the query type.
{},
),
)
})

graphql.query<
{ key: string },
// @ts-expect-error `null` is not a valid variables type.
null
>('', (req, res, ctx) => {
return res(ctx.data({ key: 'pass' }))
})

graphql.mutation<{ key: string }>('', (req, res, ctx) =>
res(
ctx.data(
// @ts-expect-error Response data doesn't match the query type.
{},
),
),
)

graphql.mutation<
{ key: string },
// @ts-expect-error `null` is not a valid variables type.
null
>('', (req, res, ctx) => {
return res(ctx.data({ key: 'pass' }))
})

graphql.operation<{ key: string }>((req, res, ctx) => {
return res(
ctx.data(
// @ts-expect-error Response data doesn't match the query type.
{},
),
)
})

graphql.operation<
{ key: string },
// @ts-expect-error `null` is not a valid variables type.
null
>((req, res, ctx) => {
return res(ctx.data({ key: 'pass' }))
})
39 changes: 39 additions & 0 deletions test/typings/rest.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { rest } from 'msw'

rest.get<{ userId: string }, { postCount: number }>(
'/user',
(req, res, ctx) => {
req.body.userId

// @ts-expect-error `session` property is not defined on the request body type.
req.body.session

res(
// @ts-expect-error JSON doesn't match given response body generic type.
ctx.json({ unknown: true }),
)

res(
// @ts-expect-error value types do not match
ctx.json({ postCount: 'this is not a number' }),
)

return res(ctx.json({ postCount: 2 }))
},
)

rest.get<any, any, { userId: string }>('/user/:userId', (req, res, ctx) => {
req.params.userId

// @ts-expect-error `unknown` is not defined in the request params type.
req.params.unknown
})

rest.post<// @ts-expect-error `null` is not a valid request body type.
null>('/submit', () => null)

rest.get<
any,
// @ts-expect-error `null` is not a valid response body type.
null
>('/user', () => null)
16 changes: 16 additions & 0 deletions test/typings/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"moduleResolution": "Node",
"lib": ["dom"],
"baseUrl": ".",
"paths": {
"msw": ["../../"]
}
},
"include": ["../../global.d.ts", "**/*.test-d.ts"],
"exclude": ["node_modules"]
}

0 comments on commit dbd5bef

Please sign in to comment.