ts-jest-mocker
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
import { Class } from "./types"; | ||
import { Class } from './types'; | ||
export declare class FunctionsFinder { | ||
@@ -3,0 +3,0 @@ static find<T>(clazz: Class<T>): Set<string>; |
@@ -10,3 +10,3 @@ "use strict"; | ||
static filter(functions) { | ||
return new Set(Array.from(functions).filter(f => !excludeFunctions.includes(f))); | ||
return new Set(Array.from(functions).filter((f) => !excludeFunctions.includes(f))); | ||
} | ||
@@ -13,0 +13,0 @@ static findOwnProperties(clazz) { |
@@ -1,2 +0,2 @@ | ||
import { Mock } from "./types"; | ||
import { Mock } from './types'; | ||
export declare function mock<T>(clazz?: { | ||
@@ -3,0 +3,0 @@ new (...args: any[]): T; |
@@ -1,2 +0,2 @@ | ||
import { Class, Mock } from "./types"; | ||
import { Class, Mock } from './types'; | ||
export declare const createClassProxy: <T>(clazz: Class<T>) => Mock<T>; |
@@ -16,5 +16,5 @@ "use strict"; | ||
return target[property]; | ||
} | ||
}, | ||
}); | ||
}; | ||
exports.createClassProxy = createClassProxy; |
@@ -1,2 +0,2 @@ | ||
import { Mock } from "./types"; | ||
import { Mock } from './types'; | ||
export declare const createGenericProxy: <T>() => Mock<T>; |
@@ -10,7 +10,9 @@ "use strict"; | ||
} | ||
target[property] = jest.fn(); | ||
target[property] = jest.fn().mockImplementation(() => { | ||
throw new Error(`Method ${String(property)} is not mocked`); | ||
}); | ||
return target[property]; | ||
} | ||
}, | ||
}); | ||
}; | ||
exports.createGenericProxy = createGenericProxy; |
@@ -25,2 +25,11 @@ "use strict"; | ||
})); | ||
it('should throw error on methods whose implementation is not mocked explicitly', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const proxy = (0, proxy_generic_1.createGenericProxy)(); | ||
expect(() => { | ||
proxy.method1(); | ||
}).toThrowError('Method method1 is not mocked'); | ||
expect(() => { | ||
proxy[Symbol('foo')](); | ||
}).toThrowError('Method Symbol(foo) is not mocked'); | ||
})); | ||
}); |
{ | ||
"name": "ts-jest-mocker", | ||
"version": "0.5.0", | ||
"description": "Powerful, lightweight and TypeScript friendly library that extends Jest with capability of mocking classes and interfaces.", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"main": "lib/lib/index.js", | ||
"typings": "lib/lib/index.d.ts", | ||
"scripts": { | ||
"test": "jest", | ||
"build": "rimraf ./lib && tsc" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/dariosn85/ts-jest-mocker.git" | ||
}, | ||
"keywords": [ | ||
"jest", | ||
"typescript", | ||
"mock", | ||
"class", | ||
"interface", | ||
"unit", | ||
"tests" | ||
], | ||
"author": "Dario Snajder", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/dariosn85/ts-jest-mocker/issues" | ||
}, | ||
"homepage": "https://github.com/dariosn85/ts-jest-mocker#readme", | ||
"devDependencies": { | ||
"@types/jest": "^28.1.7", | ||
"rimraf": "^3.0.2", | ||
"ts-jest": "^28.0.8", | ||
"typescript": "^4.7.4" | ||
}, | ||
"dependencies": { | ||
"@types/jest": "^28.1.7", | ||
"jest": "^28.1.3" | ||
}, | ||
"files": [ | ||
"lib/lib/*" | ||
] | ||
"name": "ts-jest-mocker", | ||
"version": "1.0.0", | ||
"description": "Powerful, lightweight and TypeScript friendly library that extends Jest with capability of mocking classes and interfaces.", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"main": "lib/lib/index.js", | ||
"typings": "lib/lib/index.d.ts", | ||
"scripts": { | ||
"prepare": "husky install", | ||
"test": "jest", | ||
"build": "rimraf ./lib && tsc", | ||
"format:check": "prettier . --check", | ||
"format": "prettier . --write" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/dariosn85/ts-jest-mocker.git" | ||
}, | ||
"keywords": [ | ||
"jest", | ||
"typescript", | ||
"mock", | ||
"class", | ||
"interface", | ||
"unit", | ||
"tests" | ||
], | ||
"author": "Dario Snajder", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/dariosn85/ts-jest-mocker/issues" | ||
}, | ||
"homepage": "https://github.com/dariosn85/ts-jest-mocker#readme", | ||
"devDependencies": { | ||
"@types/jest": "^28.1.7", | ||
"husky": "^8.0.3", | ||
"prettier": "^3.2.4", | ||
"rimraf": "^3.0.2", | ||
"ts-jest": "^28.0.8", | ||
"ts-node": "^10.9.2", | ||
"typescript": "^4.7.4" | ||
}, | ||
"dependencies": { | ||
"@types/jest": "^28.1.7", | ||
"jest": "^28.1.3" | ||
}, | ||
"files": [ | ||
"lib/lib/*" | ||
] | ||
} |
@@ -11,14 +11,14 @@ <h1>ts-jest-mocker</h1> | ||
*** | ||
--- | ||
## Table of Contents | ||
- [Getting started](#getting-started) | ||
- [Mocking classes](#mocking-classes) | ||
- [Mocking interfaces](#mocking-interfaces) | ||
- [More advanced example](#more-advanced-example) | ||
- [Jest API compatibility](#jest-api-compatibility) | ||
- [Why to use ts-jest-mocker](#why-to-use-ts-jest-mocker) | ||
- [Getting started](#getting-started) | ||
- [Mocking classes](#mocking-classes) | ||
- [Mocking interfaces](#mocking-interfaces) | ||
- [More advanced example](#more-advanced-example) | ||
- [Jest API compatibility](#jest-api-compatibility) | ||
- [Why to use ts-jest-mocker](#why-to-use-ts-jest-mocker) | ||
*** | ||
--- | ||
@@ -36,2 +36,4 @@ ## Getting started | ||
```typescript | ||
import { mock } from 'ts-jest-mocker'; | ||
const serviceMock = mock(YourService); // automatically mocks all methods | ||
@@ -45,2 +47,4 @@ | ||
```typescript | ||
import { mock } from 'ts-jest-mocker'; | ||
const interfaceMock = mock<YourInterface>(); // automatically mocks all interface methods | ||
@@ -51,2 +55,14 @@ | ||
### Using `Mock` type | ||
```typescript | ||
import { Mock, mock } from 'ts-jest-mocker'; | ||
let serviceMock: Mock<YourService>; | ||
serviceMock = mock(YourService); | ||
serviceMock.yourMethod.mockReturnValue('Test'); | ||
``` | ||
## More advanced example | ||
@@ -61,3 +77,3 @@ | ||
export interface User { | ||
name: string | ||
name: string; | ||
age: number; | ||
@@ -71,8 +87,9 @@ } | ||
name: 'User1', | ||
age: 30 | ||
}, { | ||
age: 30, | ||
}, | ||
{ | ||
name: 'User2', | ||
age: 40 | ||
} | ||
] | ||
age: 40, | ||
}, | ||
]; | ||
} | ||
@@ -85,7 +102,6 @@ } | ||
```typescript title="users-service.ts" | ||
import {User, UsersRepository} from "./users-repository"; | ||
import { User, UsersRepository } from './users-repository'; | ||
export class UsersService { | ||
constructor(private readonly usersRepository: UsersRepository) { | ||
} | ||
constructor(private readonly usersRepository: UsersRepository) {} | ||
@@ -105,5 +121,5 @@ getUsers(): Array<User> { | ||
```typescript title="users-service.test.ts" | ||
import {mock} from "ts-jest-mocker"; | ||
import {UsersRepository} from "./users-repository"; | ||
import {UsersService} from "./users-service"; | ||
import { mock } from 'ts-jest-mocker'; | ||
import { UsersRepository } from './users-repository'; | ||
import { UsersService } from './users-service'; | ||
@@ -114,6 +130,8 @@ describe('UsersService', () => { | ||
const repositoryMock = mock(UsersRepository); | ||
repositoryMock.getUsers.mockReturnValue([{ | ||
name: 'Mocked user 1', | ||
age: 40 | ||
}]); | ||
repositoryMock.getUsers.mockReturnValue([ | ||
{ | ||
name: 'Mocked user 1', | ||
age: 40, | ||
}, | ||
]); | ||
const service = new UsersService(repositoryMock); | ||
@@ -129,3 +147,3 @@ | ||
name: 'Mocked user 1', | ||
age: 40 | ||
age: 40, | ||
}); | ||
@@ -143,3 +161,3 @@ }); | ||
While using ts-jest-mocker you don't need to use any custom calls to reset mock or anything. | ||
You call for example `jest.resetAllMocks()` as you usually do. | ||
You call for example `jest.resetAllMocks()` as you usually do. | ||
@@ -170,3 +188,3 @@ ## Why to use ts-jest-mocker? | ||
// so mock and UsersRepository are compatible? | ||
yourMethod20: jest.fn() | ||
yourMethod20: jest.fn(), | ||
}; | ||
@@ -180,3 +198,3 @@ | ||
yourMethod1: jest.fn(), | ||
yourMethod2: jest.fn() | ||
yourMethod2: jest.fn(), | ||
} as any; | ||
@@ -195,7 +213,7 @@ // ❌ you mock only what you need and then cast explicitly to any | ||
// ❌️ You often skip specifying mock types like jest.fn<User, [User]>() and | ||
// then need to check over and over again in the code what actually | ||
// then need to check over and over again in the code what actually | ||
// mocked methods should return? | ||
mockUserRepository.yourMethod1.mockReturnedValue({ | ||
name: 'User1', | ||
age: 20 | ||
age: 20, | ||
}); | ||
@@ -215,11 +233,11 @@ | ||
name: 'User1', | ||
age: 20 | ||
age: 20, | ||
}); // ✅ return type is automatically checked while compilation | ||
mockUserRepository.yourMethod1.mockReturnedValue({ | ||
name: 'User1' | ||
name: 'User1', | ||
}); // ❗ [compilation error] - you will catch incorrect types | ||
mockUserRepository.yourMethod1.mockReturnedValue({ | ||
age: 20 | ||
age: 20, | ||
}); // ❗ [compilation error] - you will catch incorrect types | ||
@@ -231,2 +249,2 @@ | ||
const userService = new UserService(mockUserRepository); | ||
``` | ||
``` |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
17560
7.76%237
4.87%0
-100%235
8.8%7
75%