From 5e15d96323a7d5cc7c2961453ea8d39eff1b38c6 Mon Sep 17 00:00:00 2001 From: Ifiok Jr Date: Fri, 14 Jun 2019 02:25:03 +0100 Subject: [PATCH] feat: add types for jest-dev-server (#36084) --- types/jest-dev-server/index.d.ts | 133 ++++++++++++++++++ .../jest-dev-server/jest-dev-server-tests.ts | 39 +++++ types/jest-dev-server/tsconfig.json | 16 +++ types/jest-dev-server/tslint.json | 1 + 4 files changed, 189 insertions(+) create mode 100644 types/jest-dev-server/index.d.ts create mode 100644 types/jest-dev-server/jest-dev-server-tests.ts create mode 100644 types/jest-dev-server/tsconfig.json create mode 100644 types/jest-dev-server/tslint.json diff --git a/types/jest-dev-server/index.d.ts b/types/jest-dev-server/index.d.ts new file mode 100644 index 0000000000..ede5f46f7b --- /dev/null +++ b/types/jest-dev-server/index.d.ts @@ -0,0 +1,133 @@ +// Type definitions for jest-dev-server 4.2 +// Project: https://github.com/smooth-code/jest-puppeteer/tree/master/packages/jest-dev-server +// Definitions by: Ifiok Jr. +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +/// + +import { ChildProcess } from 'child_process'; +import { WaitOnOptions } from 'wait-on'; + +export interface JestDevServerOptions { + /** + * Command to execute to start the port. Directly passed to spawnd. + * + * ```js + * module.exports = { + * command: 'npm run start', + * } + * ``` + */ + command: string; + + /** + * Log server output, useful if server is crashing at start. + * @default false + * ```js + * module.exports = { + * command: 'npm run start', + * debug: true, + * } + * ``` + */ + debug?: boolean; + + /** + * How many milliseconds to wait for the spawned server to be available before giving up. Defaults to wait-port's default. + * @default 5000 + * ```js + * module.exports = { + * command: 'npm run start', + * launchTimeout: 30000, + * } + * ``` + */ + launchTimeout?: number; + + /** + * Host to wait for activity on before considering the server running. Must be used in conjunction with port. + * @default 'localhost' + * + * ```js + * module.exports = { + * command: 'npm run start --port 3000', + * host: 'customhost.com', + * port: 3000 + * } + * ``` + */ + host?: string; + + /** + * To wait for an HTTP or TCP endpoint before considering the server running, include http or tcp as a protocol. Must be used in conjunction with port. + * @default 'tcp' + * ```js + * module.exports = { + * command: 'npm run start --port 3000', + * protocol: 'http', + * port: 3000, + * } + * ``` + */ + protocol?: 'https' | 'http' | 'tcp' | 'socket'; + + /** + * Port to wait for activity on before considering the server running. If not provided, the server is assumed to immediately be running. + * @default null + * + * ```js + * module.exports = { + * command: 'npm run start --port 3000', + * port: 3000, + * } + * ``` + */ + port?: number; + + /** + * It defines the action to take if port is already used: + * @default 'ask' + * + * - ask: a prompt is shown to decide if you want to kill the process or not + * - error: an errow is thrown + * - ignore: your test are executed, we assume that the server is already started + * - kill: the process is automatically killed without a prompt + * + * ```js + * module.exports = { + * command: 'npm run start --port 3000', + * port: 3000, + * usedPortAction: 'kill', + * } + */ + usedPortAction?: 'ask' | 'error' | 'ignore' | 'kill'; + + /** + * jest-dev-server uses the wait-on npm package to wait for resources to become available before calling callback. + * @default {} + * + * ```js + * module.exports = { + * command: 'npm run start --port 3000', + * port: 3000, + * usedPortAction: 'kill', + * waitOnScheme: { + * delay: 1000, + * }, + * } + */ + waitOnScheme?: Partial; +} + +export const ERROR_TIMEOUT: 'ERROR_TIMEOUT'; +export const ERROR_PORT_USED: 'ERROR_PORT_USED'; +export const ERROR_NO_COMMAND: 'ERROR_NO_COMMAND'; + +export function setup( + options: JestDevServerOptions | JestDevServerOptions[], +): Promise; +export function teardown(): Promise; +export function getServers(): ChildProcess[]; + +export class JestDevServerError extends Error {} diff --git a/types/jest-dev-server/jest-dev-server-tests.ts b/types/jest-dev-server/jest-dev-server-tests.ts new file mode 100644 index 0000000000..d4afe35ab5 --- /dev/null +++ b/types/jest-dev-server/jest-dev-server-tests.ts @@ -0,0 +1,39 @@ +import { setup, getServers, teardown } from 'jest-dev-server'; + +setup([ + { + command: 'yarn storybook:ci', + port: 3002, + usedPortAction: 'kill', + launchTimeout: 60000, + }, + { + command: 'yarn next:ci', + port: 3001, + usedPortAction: 'kill', + launchTimeout: 60000, + }, + { + command: 'yarn dev:docs', + port: 3000, + usedPortAction: 'kill', + launchTimeout: 60000, + }, +]).then(() => { + // have fun +}); + +setup({ + command: 'yarn storybook:ci', + port: 3002, + usedPortAction: 'kill', + launchTimeout: 60000, +}).then(() => { + // One server only +}); + +getServers(); + +teardown().then(() => { + // After teardown has completed +}); diff --git a/types/jest-dev-server/tsconfig.json b/types/jest-dev-server/tsconfig.json new file mode 100644 index 0000000000..710e505336 --- /dev/null +++ b/types/jest-dev-server/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": ["index.d.ts", "jest-dev-server-tests.ts"] +} diff --git a/types/jest-dev-server/tslint.json b/types/jest-dev-server/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/jest-dev-server/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }