Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typings for jest-emotion #723

Merged
merged 2 commits into from
Jun 16, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions packages/jest-emotion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@
"version": "9.2.2",
"description": "Jest utilities for emotion",
"main": "lib/index.js",
"types": "types/index.d.ts",
"files": [
"src",
"lib"
],
"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"
Expand Down
22 changes: 22 additions & 0 deletions packages/jest-emotion/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Definitions by: Junyoung Clare Jang <https://github.com/Ailrun>
// TypeScript Version: 2.4

/// <reference types="jest" />
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<R> {
toHaveStyleRule(property: string, value: any): R;
}
}
}
52 changes: 52 additions & 0 deletions packages/jest-emotion/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/// <reference types="jest" />
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');
27 changes: 27 additions & 0 deletions packages/jest-emotion/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
6 changes: 6 additions & 0 deletions packages/jest-emotion/types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"no-relative-import-in-test": false
}
}
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down