Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit 97804e7

Browse files
danielreardenIvanGoncharov
authored andcommitted
Convert tests to TS
1 parent edc3c24 commit 97804e7

File tree

7 files changed

+250
-29
lines changed

7 files changed

+250
-29
lines changed

.eslintrc.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,7 @@ overrides:
503503
'@typescript-eslint/adjacent-overload-signatures': error
504504
'@typescript-eslint/array-type': [error, { default: generic }]
505505
'@typescript-eslint/await-thenable': error
506-
'@typescript-eslint/ban-ts-comment':
507-
[error, { 'ts-expect-error': 'allow-with-description' }]
506+
'@typescript-eslint/ban-ts-comment': [error, { 'ts-expect-error': false }]
508507
'@typescript-eslint/ban-types': error
509508
'@typescript-eslint/class-literal-property-style': off
510509
'@typescript-eslint/consistent-type-assertions':

.mocharc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ throw-deprecation: true
22
check-leaks: true
33
require:
44
- '@babel/register'
5+
- ts-node/register

package-lock.json

Lines changed: 216 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"scripts": {
3131
"test": "npm run prettier:check && npm run lint && npm run check && npm run testonly && npm run build && npm run check:integrations",
3232
"test:ci": "npm ci && npm run prettier:check && npm run lint && npm run check && npm run testonly:cover && npm run build && npm run check:integrations",
33-
"testonly": "mocha src/**/__tests__/**/*.js",
33+
"testonly": "mocha src/**/__tests__/**/*.ts",
3434
"testonly:cover": "nyc npm run testonly",
3535
"lint": "eslint src resources integrationTests",
3636
"prettier": "prettier --ignore-path .gitignore --write --list-different '**/*.{js,ts,md,json,yml}'",
@@ -53,7 +53,16 @@
5353
"@babel/plugin-transform-flow-strip-types": "7.10.1",
5454
"@babel/preset-env": "7.10.2",
5555
"@babel/register": "7.10.1",
56+
"@types/body-parser": "1.19.0",
57+
"@types/chai": "4.2.11",
58+
"@types/connect": "3.4.33",
59+
"@types/express": "4.17.6",
60+
"@types/mocha": "7.0.2",
61+
"@types/multer": "1.4.3",
5662
"@types/node": "14.0.11",
63+
"@types/restify": "8.4.2",
64+
"@types/sinon": "9.0.4",
65+
"@types/supertest": "2.0.9",
5766
"@typescript-eslint/eslint-plugin": "3.1.0",
5867
"@typescript-eslint/parser": "3.1.0",
5968
"babel-eslint": "10.1.0",
@@ -80,6 +89,7 @@
8089
"restify": "4.3.2",
8190
"sinon": "9.0.2",
8291
"supertest": "4.0.2",
92+
"ts-node": "8.10.2",
8393
"typescript": "3.9.5",
8494
"unfetch": "4.1.0"
8595
},

src/__tests__/http-test.js renamed to src/__tests__/http-test.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @flow strict
2-
31
import zlib from 'zlib';
42

53
import connect from 'connect';
@@ -28,14 +26,14 @@ import {
2826

2927
import { graphqlHTTP } from '../index';
3028

31-
// TODO Improve typings after converting to TypeScript
32-
type Server = () => {|
33-
get: (...args: Array<mixed>) => mixed,
34-
post: (...args: Array<mixed>) => mixed,
35-
put: (...args: Array<mixed>) => mixed,
36-
request: () => any,
37-
use: (...args: Array<mixed>) => any,
38-
|};
29+
type Middleware = (req: any, res: any, next: () => void) => unknown;
30+
type Server = () => {
31+
request: () => supertest.SuperTest<supertest.Test>;
32+
use: (middleware: Middleware) => unknown;
33+
get: (path: string, middleware: Middleware) => unknown;
34+
post: (path: string, middleware: Middleware) => unknown;
35+
put: (path: string, middleware: Middleware) => unknown;
36+
};
3937

4038
const QueryRootType = new GraphQLObjectType({
4139
name: 'QueryRoot',
@@ -45,7 +43,8 @@ const QueryRootType = new GraphQLObjectType({
4543
args: {
4644
who: { type: GraphQLString },
4745
},
48-
resolve: (_root, args) => 'Hello ' + (args.who ?? 'World'),
46+
resolve: (_root, args: { who?: string }) =>
47+
'Hello ' + (args.who ?? 'World'),
4948
},
5049
thrower: {
5150
type: GraphQLString,
@@ -69,14 +68,11 @@ const TestSchema = new GraphQLSchema({
6968
}),
7069
});
7170

72-
function stringifyURLParams(urlParams?: {
73-
[param: string]: string,
74-
...
75-
}): string {
71+
function stringifyURLParams(urlParams?: { [param: string]: string }): string {
7672
return new URLSearchParams(urlParams).toString();
7773
}
7874

79-
function urlString(urlParams?: { [param: string]: string, ... }): string {
75+
function urlString(urlParams?: { [param: string]: string }): string {
8076
let string = '/graphql';
8177
if (urlParams) {
8278
string += '?' + stringifyURLParams(urlParams);
@@ -1057,7 +1053,7 @@ function runTests(server: Server) {
10571053

10581054
it('supports pretty printing configured by request', async () => {
10591055
const app = server();
1060-
let pretty;
1056+
let pretty: boolean | undefined;
10611057

10621058
app.get(
10631059
urlString(),
@@ -1567,7 +1563,7 @@ function runTests(server: Server) {
15671563
const response = await app.request().put(urlString({ query: '{test}' }));
15681564

15691565
expect(response.status).to.equal(405);
1570-
expect(response.headers.allow).to.equal('GET, POST');
1566+
expect(response.get('allow')).to.equal('GET, POST');
15711567
expect(JSON.parse(response.text)).to.deep.equal({
15721568
errors: [{ message: 'GraphQL only supports GET and POST requests.' }],
15731569
});

0 commit comments

Comments
 (0)