Jest update

This commit is contained in:
Alexey Svetliakov
2017-01-09 06:06:27 +01:00
parent e93460bfaa
commit b4fc6f27dc
2 changed files with 140 additions and 7 deletions
+62 -6
View File
@@ -1,7 +1,8 @@
// Type definitions for Jest 16.0.0
// Type definitions for Jest 18.0.0
// Project: http://facebook.github.io/jest/
// Definitions by: Asana <https://asana.com>, Ivo Stratev <https://github.com/NoHomey>, jwbay <https://github.com/jwbay>
// Definitions by: Asana <https://asana.com>, Ivo Stratev <https://github.com/NoHomey>, jwbay <https://github.com/jwbay>, Alexey Svetliakov <https://github.com/asvetliakov>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
declare var beforeAll: jest.Lifecycle;
declare var beforeEach: jest.Lifecycle;
@@ -16,7 +17,7 @@ declare var xit: jest.It;
declare var test: jest.It;
declare var xtest: jest.It;
declare function expect(actual: any): jest.Matchers;
declare const expect: jest.Expect;
interface NodeRequire {
/** Returns the actual module instead of a mock, bypassing all checks on whether the module should receive a mock implementation or not. */
@@ -31,8 +32,10 @@ declare namespace jest {
function autoMockOff(): typeof jest;
/** Enables automatic mocking in the module loader. */
function autoMockOn(): typeof jest;
/** Clears the mock.calls and mock.instances properties of all mocks. Equivalent to calling .mockClear() on every mocked function. */
/** @deprecated */
function clearAllMocks(): typeof jest;
/** Clears the mock.calls and mock.instances properties of all mocks. Equivalent to calling .mockClear() on every mocked function. */
function resetAllMocks(): typeof jest;
/** Removes any pending timers from the timer system. If any timers have been scheduled, they will be cleared and will never have the opportunity to execute in the future. */
function clearAllTimers(): typeof jest;
/** Indicates that the module system should never return a mocked version of the specified module, including all of the specificied module's dependencies. */
@@ -111,6 +114,41 @@ declare namespace jest {
skip: Describe;
}
interface MatcherUtils {
readonly isNot: boolean;
utils: {
readonly EXPECTED_COLOR: string;
readonly RECEIVED_COLOR: string;
ensureActualIsNumber(actual: any, matcherName?: string): void;
ensureExpectedIsNumber(actual: any, matcherName?: string): void;
ensureNoExpected(actual: any, matcherName?: string): void;
ensureNumbers(actual: any, expected: any, matcherName?: string): void;
/** get the type of a value with handling the edge cases like `typeof []` and `typeof null` */
getType(value: any): string;
matcherHint(matcherName: string, received?: string, expected?: string, options?: { secondArgument?: string, isDirectExpectCall?: boolean }): string;
pluralize(word: string, count: number): string;
printExpected(value: any): string;
printReceived(value: any): string;
printWithType(name: string, received: any, print: (value: any) => string): string;
stringify(object: {}, maxDepth?: number): string;
}
}
interface ExpectExtendMap {
[key: string]: (this: MatcherUtils, received: any, actual: any) => { message: () => string, pass: boolean };
}
interface Expect {
(actual: any): Matchers;
anything(): void;
any(cs: any): void;
arrayContaining(arr: any[]): void;
assertions(num: number): void;
extend(obj: ExpectExtendMap): void;
objectContaining(obj: {}): void;
stringMatching(str: string | RegExp): void;
}
interface Matchers {
not: Matchers;
lastCalledWith(...args: any[]): void;
@@ -135,9 +173,11 @@ declare namespace jest {
toHaveBeenCalledTimes(expected: number): boolean;
toHaveBeenCalledWith(...params: any[]): boolean;
toHaveBeenLastCalledWith(...params: any[]): boolean;
toHaveLength(expected: number): void;
toHaveProperty(propertyPath: string, value?: any): void;
toMatch(expected: string | RegExp): void;
toMatchObject(expected: {}): void;
toMatchSnapshot(): void;
toMatchSnapshot(snapshotName?: string): void;
toThrow(): void;
toThrowError(error?: string | Constructable | RegExp): void;
toThrowErrorMatchingSnapshot(): void;
@@ -147,9 +187,25 @@ declare namespace jest {
new (...args: any[]): any
}
interface Mock<T> extends Function {
interface Mock<T> extends Function, MockInstance<T> {
new (): T;
(...args: any[]): any;
}
/**
* Wrap module with mock definitions
* @example
* jest.mock("../api");
* import { Api } from "../api";
*
* const myApi: jest.Mocked<Api> = new Api() as any;
* myApi.myApiMethod.mockImplementation(() => "test");
*/
type Mocked<T> = T & {
[P in keyof T]: T[P] & MockInstance<T>;
} & MockInstance<T>;
interface MockInstance<T> {
mock: MockContext<T>;
mockClear(): void;
mockImplementation(fn: Function): Mock<T>;
+78 -1
View File
@@ -171,6 +171,60 @@ describe('toThrow API', function () {
});
});
describe('Assymetric matchers', function () {
it('works', function () {
expect({
timestamp: 1480807810388,
text: 'Some text content, but we care only about *this part*'
}).toEqual({
timestamp: expect.any(Number),
text: expect.stringMatching('*this part*')
});
const callback = jest.fn();
expect(callback).toEqual(expect.any(Function));
callback(5, "test");
expect(callback).toBeCalledWith(expect.any(Number), expect.any(String))
const obj = {
items: [1]
};
expect(obj).toEqual(expect.objectContaining({
items: expect.arrayContaining([
expect.any(Number)
])
}));
expect.assertions(4);
});
});
describe('Extending extend', function () {
it('works', function () {
expect.extend({
toBeNumber(received: any, actual: any) {
const pass = received === actual;
const message =
() => `expected ${received} ${pass ? 'not ' : ''} to be ${actual}`;
return { message, pass };
},
toBeTest(received: any, actual: any) {
this.utils.ensureNoExpected(received);
this.utils.ensureActualIsNumber(received);
this.utils.ensureExpectedIsNumber(actual);
this.utils.ensureNumbers(received, actual);
return {
message: () => `
${this.utils.getType(received).toLowerCase()} \n\n
${this.utils.matcherHint(".not.toBe")} ${this.utils.printExpected(actual)} ${this.utils.printReceived(received)}\n\n
`,
pass: true
};
}
});
});
});
describe('missing tests', function () {
it('creates closures', function () {
class Closure<T> {
@@ -208,7 +262,7 @@ describe('missing tests', function () {
expect(getFruits()).toContain('Orange');
mock.mockReturnValueOnce(['Apple', 'Plum']);
expect(mock()).not.toContain('Orange');
const myBeverage: any = {delicious: true, sour: false};
const myBeverage: any = {delicious: true, sour: false};
expect(myBeverage).toContainEqual({delicious: true, sour: false});
mock.mockReturnValue([]); //Deprecated: Use jest.fn(() => value) instead.
mock.mockClear();
@@ -248,6 +302,10 @@ describe('toMatchSnapshot', function () {
it('compares snapshots', function () {
expect({ type: 'a', props: { href: 'https://www.facebook.com/' }, children: [ 'Facebook' ] }).toMatchSnapshot();
});
it('can give name to snapshot', function () {
expect({ type: 'a', props: { href: 'https://www.facebook.com/' }, children: [ 'Facebook' ] }).toMatchSnapshot('given name');
});
});
describe('toThrowErrorMatchingSnapshot', function () {
@@ -367,3 +425,22 @@ describe('strictNullChecks', function () {
done();
})
});
interface TestApi {
testProp: boolean;
testMethod(a: number): void;
}
describe("Mocked type", function () {
it("Works", function () {
const mock: jest.Mocked<TestApi> = jest.fn(() => ({
testProp: true,
testMethod1: jest.fn(),
testMethod2: jest.fn()
})) as any;
mock.testProp;
mock.testMethod.mockImplementation(() => "test");
mock.testMethod(5);
mock.mockClear();
});
});