From a8f4f8923e89aad7871ebcebd22e9c052197a2cb Mon Sep 17 00:00:00 2001 From: Junyoung Clare Jang Date: Sat, 16 Jun 2018 18:35:49 -0400 Subject: [PATCH] Add typings for jest-emotion (#723) --- packages/jest-emotion/package.json | 4 ++ packages/jest-emotion/types/index.d.ts | 22 ++++++++++ packages/jest-emotion/types/test.ts | 52 +++++++++++++++++++++++ packages/jest-emotion/types/tsconfig.json | 27 ++++++++++++ packages/jest-emotion/types/tslint.json | 6 +++ yarn.lock | 4 ++ 6 files changed, 115 insertions(+) create mode 100644 packages/jest-emotion/types/index.d.ts create mode 100644 packages/jest-emotion/types/test.ts create mode 100644 packages/jest-emotion/types/tsconfig.json create mode 100644 packages/jest-emotion/types/tslint.json diff --git a/packages/jest-emotion/package.json b/packages/jest-emotion/package.json index 4c6576c49..f3fc84d60 100644 --- a/packages/jest-emotion/package.json +++ b/packages/jest-emotion/package.json @@ -3,6 +3,7 @@ "version": "9.2.2", "description": "Jest utilities for emotion", "main": "lib/index.js", + "types": "types/index.d.ts", "files": [ "src", "lib" @@ -10,15 +11,18 @@ "scripts": { "build": "npm-run-all clean babel", "babel": "babel src -d lib", + "test:typescript": "dtslint types", "watch": "babel src -d lib --watch", "clean": "rimraf lib" }, "dependencies": { + "@types/jest": "^23.0.2", "chalk": "^2.4.1", "css": "^2.2.1" }, "devDependencies": { "babel-cli": "^6.24.1", + "dtslint": "^0.3.0", "npm-run-all": "^4.0.2", "pretty-format": "^22.4.3", "rimraf": "^2.6.1" diff --git a/packages/jest-emotion/types/index.d.ts b/packages/jest-emotion/types/index.d.ts new file mode 100644 index 000000000..22d8657cf --- /dev/null +++ b/packages/jest-emotion/types/index.d.ts @@ -0,0 +1,22 @@ +// Definitions by: Junyoung Clare Jang +// TypeScript Version: 2.4 + +/// +import { Emotion } from 'create-emotion'; + +export interface CreateSerializerOptions { + classNameReplacer?: (className: string, index: number) => string; + DOMElements?: boolean; +} + +export function getStyles(emotion: Emotion): string; +export function createSerializer(emotion: Emotion, options?: CreateSerializerOptions): jest.SnapshotSerializerPlugin; +export function createMatchers(emotion: Emotion): jest.ExpectExtendMap; + +declare global { + namespace jest { + interface Matchers { + toHaveStyleRule(property: string, value: any): R; + } + } +} diff --git a/packages/jest-emotion/types/test.ts b/packages/jest-emotion/types/test.ts new file mode 100644 index 000000000..3870a481c --- /dev/null +++ b/packages/jest-emotion/types/test.ts @@ -0,0 +1,52 @@ +/// +import * as emotion from 'emotion'; +import { + CreateSerializerOptions, + createSerializer, + createMatchers, + getStyles, +} from '../'; + +createSerializer(emotion); +createSerializer(emotion, {}); +createSerializer(emotion, { + DOMElements: true, +}); +createSerializer(emotion, { + classNameReplacer() { + return 'abc'; + }, +}); +createSerializer(emotion, { + classNameReplacer(className) { + return className; + }, +}); +createSerializer(emotion, { + classNameReplacer(className, index) { + return `${className}-${index}`; + }, +}); +createSerializer(emotion, 213 as any as CreateSerializerOptions); +// $ExpectError +createSerializer(); +// $ExpectError +createSerializer(emotion, 1); +// $ExpectError +createSerializer(emotion, true); +// $ExpectError +createSerializer(emotion, {}, undefined as any); + +// $ExpectError +createMatchers(); +// $ExpectError +createMatchers(emotion, undefined as any); + +expect.addSnapshotSerializer(createSerializer(emotion)); +expect.extend(createMatchers(emotion)); + +expect({}).toHaveStyleRule('width', 'black'); +expect({}).toHaveStyleRule('height', /red/); +expect({}).toHaveStyleRule('color', expect.stringContaining('20')); +// $ExpectError +expect({}).toHaveStyleRule(5, 'abc'); diff --git a/packages/jest-emotion/types/tsconfig.json b/packages/jest-emotion/types/tsconfig.json new file mode 100644 index 000000000..ea6734342 --- /dev/null +++ b/packages/jest-emotion/types/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + "baseUrl": "../", + "forceConsistentCasingInFileNames": true, + "jsx": "react", + "lib": [ + "es6", + "dom" + ], + "module": "commonjs", + "noEmit": true, + "noImplicitAny": true, + "noImplicitThis": true, + "strict": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "target": "es5", + "typeRoots": [ + "../" + ], + "types": [] + }, + "include": [ + "./*.ts", + "./*.tsx" + ] +} diff --git a/packages/jest-emotion/types/tslint.json b/packages/jest-emotion/types/tslint.json new file mode 100644 index 000000000..b48934d7b --- /dev/null +++ b/packages/jest-emotion/types/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "no-relative-import-in-test": false + } +} diff --git a/yarn.lock b/yarn.lock index 9db8ba178..c35be25e3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -928,6 +928,10 @@ version "4.6.2" resolved "https://registry.yarnpkg.com/@types/history/-/history-4.6.2.tgz#12cfaba693ba20f114ed5765467ff25fdf67ddb0" +"@types/jest@^23.0.2": + version "23.0.2" + resolved "https://registry.yarnpkg.com/@types/jest/-/jest-23.0.2.tgz#f03f9e6dd2206cc2a2e8fd033161b0b7cf905db6" + "@types/minimatch@*": version "3.0.3" resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"