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

Convert tests to TS #640

Merged
merged 1 commit into from
Jun 28, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,7 @@ overrides:
'@typescript-eslint/adjacent-overload-signatures': error
'@typescript-eslint/array-type': [error, { default: generic }]
'@typescript-eslint/await-thenable': error
'@typescript-eslint/ban-ts-comment':
[error, { 'ts-expect-error': 'allow-with-description' }]
'@typescript-eslint/ban-ts-comment': [error, { 'ts-expect-error': false }]
'@typescript-eslint/ban-types': error
'@typescript-eslint/class-literal-property-style': off
'@typescript-eslint/consistent-type-assertions':
Expand Down
1 change: 1 addition & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ throw-deprecation: true
check-leaks: true
require:
- '@babel/register'
- ts-node/register
216 changes: 216 additions & 0 deletions package-lock.json

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

12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"scripts": {
"test": "npm run prettier:check && npm run lint && npm run check && npm run testonly && npm run build && npm run check:integrations",
"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",
"testonly": "mocha src/**/__tests__/**/*.js",
"testonly": "mocha src/**/__tests__/**/*.ts",
"testonly:cover": "nyc npm run testonly",
"lint": "eslint src resources integrationTests",
"prettier": "prettier --ignore-path .gitignore --write --list-different '**/*.{js,ts,md,json,yml}'",
Expand All @@ -53,7 +53,16 @@
"@babel/plugin-transform-flow-strip-types": "7.10.1",
"@babel/preset-env": "7.10.2",
"@babel/register": "7.10.1",
"@types/body-parser": "1.19.0",
"@types/chai": "4.2.11",
"@types/connect": "3.4.33",
"@types/express": "4.17.6",
"@types/mocha": "7.0.2",
"@types/multer": "1.4.3",
"@types/node": "14.0.11",
"@types/restify": "8.4.2",
"@types/sinon": "9.0.4",
"@types/supertest": "2.0.9",
"@typescript-eslint/eslint-plugin": "3.1.0",
"@typescript-eslint/parser": "3.1.0",
"babel-eslint": "10.1.0",
Expand All @@ -80,6 +89,7 @@
"restify": "4.3.2",
"sinon": "9.0.2",
"supertest": "4.0.2",
"ts-node": "8.10.2",
"typescript": "3.9.5",
"unfetch": "4.1.0"
},
Expand Down
32 changes: 14 additions & 18 deletions src/__tests__/http-test.js → src/__tests__/http-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @flow strict

import zlib from 'zlib';

import connect from 'connect';
Expand Down Expand Up @@ -28,14 +26,14 @@ import {

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

// TODO Improve typings after converting to TypeScript
type Server = () => {|
get: (...args: Array<mixed>) => mixed,
post: (...args: Array<mixed>) => mixed,
put: (...args: Array<mixed>) => mixed,
request: () => any,
use: (...args: Array<mixed>) => any,
|};
type Middleware = (req: any, res: any, next: () => void) => unknown;
type Server = () => {
request: () => supertest.SuperTest<supertest.Test>;
use: (middleware: Middleware) => unknown;
get: (path: string, middleware: Middleware) => unknown;
post: (path: string, middleware: Middleware) => unknown;
put: (path: string, middleware: Middleware) => unknown;
};

const QueryRootType = new GraphQLObjectType({
name: 'QueryRoot',
Expand All @@ -45,7 +43,8 @@ const QueryRootType = new GraphQLObjectType({
args: {
who: { type: GraphQLString },
},
resolve: (_root, args) => 'Hello ' + (args.who ?? 'World'),
resolve: (_root, args: { who?: string }) =>
'Hello ' + (args.who ?? 'World'),
},
thrower: {
type: GraphQLString,
Expand All @@ -69,14 +68,11 @@ const TestSchema = new GraphQLSchema({
}),
});

function stringifyURLParams(urlParams?: {
[param: string]: string,
...
}): string {
function stringifyURLParams(urlParams?: { [param: string]: string }): string {
return new URLSearchParams(urlParams).toString();
}

function urlString(urlParams?: { [param: string]: string, ... }): string {
function urlString(urlParams?: { [param: string]: string }): string {
let string = '/graphql';
if (urlParams) {
string += '?' + stringifyURLParams(urlParams);
Expand Down Expand Up @@ -1057,7 +1053,7 @@ function runTests(server: Server) {

it('supports pretty printing configured by request', async () => {
const app = server();
let pretty;
let pretty: boolean | undefined;

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

expect(response.status).to.equal(405);
expect(response.headers.allow).to.equal('GET, POST');
expect(response.get('allow')).to.equal('GET, POST');
IvanGoncharov marked this conversation as resolved.
Show resolved Hide resolved
expect(JSON.parse(response.text)).to.deep.equal({
errors: [{ message: 'GraphQL only supports GET and POST requests.' }],
});
Expand Down
Loading