ts-mock-imports
Advanced tools
Comparing version 1.2.6 to 1.3.0
@@ -5,2 +5,4 @@ /// <reference types="sinon" /> | ||
export declare class ImportMock { | ||
private static sandboxedItems; | ||
private static sandbox; | ||
static mockClass<T, K extends IModule = any>(module: { | ||
@@ -18,2 +20,3 @@ [importName: string]: IConstruct<T>; | ||
} | T, importName?: K, replaceWith?: Partial<T[K]>): OtherManager<T[K]>; | ||
static restore(): void; | ||
} |
@@ -9,17 +9,26 @@ "use strict"; | ||
} | ||
ImportMock.sandbox = function (mock) { | ||
ImportMock.sandboxedItems.push(mock); | ||
return mock; | ||
}; | ||
ImportMock.mockClass = function (module, importName) { | ||
if (importName === void 0) { importName = 'default'; } | ||
return new index_1.MockManager(module, importName); | ||
return ImportMock.sandbox(new index_1.MockManager(module, importName)); | ||
}; | ||
ImportMock.mockStaticClass = function (module, importName) { | ||
if (importName === void 0) { importName = 'default'; } | ||
return new index_1.StaticMockManager(module, importName); | ||
return ImportMock.sandbox(new index_1.StaticMockManager(module, importName)); | ||
}; | ||
ImportMock.mockFunction = function (module, importName, returns) { | ||
if (importName === void 0) { importName = 'default'; } | ||
return sinon.stub(module, importName).returns(returns); | ||
return ImportMock.sandbox(sinon.stub(module, importName).returns(returns)); | ||
}; | ||
ImportMock.mockOther = function (module, importName, replaceWith) { | ||
return new index_1.OtherManager(module, importName || 'default', replaceWith); | ||
return ImportMock.sandbox(new index_1.OtherManager(module, importName || 'default', replaceWith)); | ||
}; | ||
ImportMock.restore = function () { | ||
ImportMock.sandboxedItems.forEach(function (item) { return item.restore(); }); | ||
ImportMock.sandboxedItems = []; | ||
}; | ||
ImportMock.sandboxedItems = []; | ||
return ImportMock; | ||
@@ -26,0 +35,0 @@ }()); |
@@ -1,3 +0,3 @@ | ||
import { IModule } from '../types'; | ||
export declare class Manager { | ||
import { IModule, IManager } from '../types'; | ||
export declare class Manager implements IManager { | ||
protected module: IModule; | ||
@@ -4,0 +4,0 @@ protected importName: string; |
export * from './construct'; | ||
export * from './manager'; | ||
export * from './module'; | ||
export * from './stringKeyOf'; |
{ | ||
"name": "ts-mock-imports", | ||
"version": "1.2.6", | ||
"version": "1.3.0", | ||
"description": "Intuitive mocking for Typescript class imports", | ||
@@ -32,3 +32,3 @@ "main": "lib/index.js", | ||
"typescript": ">=2.6.1 < 4", | ||
"sinon": ">= 4.1.2 < 7.4" | ||
"sinon": ">= 4.1.2" | ||
}, | ||
@@ -41,3 +41,4 @@ "scripts": { | ||
"test": "npm run dtslint && npm run unit-test", | ||
"compile": "rimraf lib && tsc -p src" | ||
"compile": "rimraf lib && tsc -p src", | ||
"build": "npm run compile" | ||
}, | ||
@@ -44,0 +45,0 @@ "dependencies": {}, |
148
README.md
# Typescript Mock Imports | ||
#### Intuitive mocking for Typescript class imports. | ||
#### Intuitive mocking for Typescript imports. | ||
[![npm](https://img.shields.io/npm/v/ts-mock-imports.svg)](https://www.npmjs.com/package/ts-mock-imports) [![Build Status](https://travis-ci.org/EmandM/ts-mock-imports.svg)](https://travis-ci.org/EmandM/ts-mock-imports) | ||
- [Installation](#installation) | ||
- [About](#about) | ||
- [Usage](#usage) | ||
- [API](#api) | ||
## About | ||
ts-mock-imports leverages the ES6 `import` syntax to mock out imported code with stub versions of the imported objects. This allows ES6 code to be easily unit-tested without the need for an explicit dependency injection library. | ||
ts-mock-imports is built on top of sinon. [Sinon stub documentation](https://sinonjs.org/releases/latest/stubs/) | ||
Mocked classes take all of the original class functions, and replace them with noop functions (functions returning `undefined`) while maintaining type safety. | ||
This library needs to be run on TypeScript 2.6.1 or later. | ||
- [Typescript Mock Imports](#typescript-mock-imports) | ||
- [Intuitive mocking for Typescript imports.](#intuitive-mocking-for-typescript-imports) | ||
- [About](#about) | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [API](#api) | ||
- [ImportMock](#importmock) | ||
- [MockManager (and MockStaticManager)](#mockmanager-and-mockstaticmanager) | ||
- [OtherManager](#othermanager) | ||
- [Test](#test) | ||
- [Typescript Tests](#typescript-tests) | ||
- [Unit Tests](#unit-tests) | ||
- [Limitations](#limitations) | ||
- [Test](#test) | ||
@@ -34,21 +45,15 @@ ## Installation | ||
## About | ||
ts-mock-imports is useful if you want to replace classes that are exported from local files with stub versions of those classes. This allows ES6 code to be easily unit-tested without the need for a dependency injection library. | ||
ts-mock-imports is built on top of sinon. | ||
The mocked class takes all of the original class functions, and replaces them with noop functions (functions returning `undefined`). | ||
This library needs to be run on TypeScript 2.6.1 or later. | ||
## Usage | ||
`src/foo.ts` | ||
```javascript | ||
```typescript | ||
export class Foo { | ||
private count: number; | ||
constructor() { | ||
throw new Error(); | ||
} | ||
public getCount(): number { | ||
return count; | ||
} | ||
} | ||
@@ -58,3 +63,3 @@ ``` | ||
`src/bar.ts` | ||
```javascript | ||
```typescript | ||
import { Foo } from './foo'; | ||
@@ -70,3 +75,3 @@ | ||
`test/bar.spec.ts` | ||
```javascript | ||
```typescript | ||
import { ImportMock } from 'ts-mock-imports'; | ||
@@ -84,4 +89,7 @@ import { Bar } from './Bar'; | ||
// Call restore to reset to original imports | ||
mockManager.restore(); | ||
// Easily add mock responses for testing | ||
mockmanager.mock('getCount', 3) | ||
// Call restore to reset all mocked objects to original imports | ||
ImportMock.restore(); | ||
``` | ||
@@ -106,3 +114,3 @@ | ||
Using importName: | ||
```javascript | ||
```typescript | ||
// export class Foo | ||
@@ -115,3 +123,3 @@ import * as fooModule from '../src/foo'; | ||
Default imports: | ||
```javascript | ||
```typescript | ||
// export default Foo | ||
@@ -126,3 +134,3 @@ import * as foo from '../foo'; | ||
Explicit typing: | ||
```javascript | ||
```typescript | ||
import * as fooModule from '../foo'; | ||
@@ -136,3 +144,3 @@ | ||
Explicit typing with full type assurance | ||
```javascript | ||
```typescript | ||
import * as fooModule from '../foo'; | ||
@@ -146,2 +154,4 @@ | ||
`mockClass` replaces the original export with a fake class. All original functions exist on the fake class as noop functions (functions returning `undefined`). | ||
--- | ||
@@ -155,3 +165,3 @@ | ||
(Only recreates static methods) | ||
```javascript | ||
```typescript | ||
import * as fooModule from '../foo'; | ||
@@ -171,3 +181,3 @@ | ||
Function exports: | ||
```javascript | ||
```typescript | ||
import * as fooModule from '../foo'; | ||
@@ -190,3 +200,3 @@ | ||
Variable mocking: | ||
```javascript | ||
```typescript | ||
import * as fooModule from '../foo'; | ||
@@ -204,2 +214,27 @@ | ||
**`restore(): void`** | ||
`restore()` will restore all mocked items. Allows `ImportMock` to be used as a sandbox. | ||
Useful for restoring when multiple mocks have been created. | ||
Variable mocking: | ||
```typescript | ||
import * as fooModule from '../foo'; | ||
import * as bazModule from '../baz'; | ||
ImportMock.mockClass(fooModule, 'Foo'); | ||
ImportMock.mockClass(fooModule, 'Bar'); | ||
ImportMock.mockFunction(bazModule, 'mainFunction') | ||
// <run tests> | ||
ImportMock.restore() | ||
// all mocked imports will now be restored to their original values | ||
``` | ||
--- | ||
### MockManager (and MockStaticManager) | ||
@@ -209,3 +244,3 @@ | ||
This function returns a sinon stub object. | ||
Returns a sinon stub object. | ||
@@ -224,6 +259,5 @@ **functionName:** | ||
Mocking functions: | ||
(Returns a sinon stub) | ||
```javascript | ||
```typescript | ||
import * as fooModule from '../foo'; | ||
@@ -234,7 +268,8 @@ | ||
// Will throw a type error if bar() does not exist on Foo | ||
const sinonStub = fooManager.mock('bar'); | ||
fooManager.mock('bar'); | ||
// new Foo().bar() will return undefined | ||
``` | ||
Mocking functions with a return object: | ||
```javascript | ||
```typescript | ||
import * as fooModule from '../foo'; | ||
@@ -244,7 +279,15 @@ | ||
const returnVal = 'Bar'; | ||
const sinonStub = mockManager.mock('bar', returnVal); | ||
mockManager.mock('bar', 'Bar'); | ||
// new Foo().bar() now returns 'Bar' | ||
``` | ||
If you wish to run modified code when the mocked function is called, you can use `sinon.callsFake()` | ||
```typescript | ||
const mockManager = ImportMock.mockClass(fooModule, 'Foo'); | ||
const sinonStub = mockManager.mock('bar'); | ||
sinonStub.callsFake(() => { | ||
// custom code here | ||
}) | ||
``` | ||
--- | ||
@@ -270,3 +313,3 @@ | ||
Mocking variable with a return object: | ||
```javascript | ||
```typescript | ||
import * as fooModule from '../foo'; | ||
@@ -286,3 +329,3 @@ | ||
Returns an instance of the mocked class. | ||
```javascript | ||
```typescript | ||
import * as fooModule from '../foo'; | ||
@@ -320,3 +363,3 @@ | ||
Mocking variable with a return object: | ||
```javascript | ||
```typescript | ||
import * as fooModule from '../foo'; | ||
@@ -337,3 +380,3 @@ | ||
Returns the current mockValue | ||
```javascript | ||
```typescript | ||
import * as fooModule from '../foo'; | ||
@@ -356,21 +399,14 @@ | ||
Requirejs is not currently compatible with this library | ||
Import mock works best when mocking es6 exports. Due to JavaScript's sometimes winding development history, there are some modules that use alternate export patterns that may not work correctly when mocked using Import mock. To reduce the chance of issues, all production code should aim to use `import { item } from 'module';` syntax. This allows the test code to use `import * as object from 'module';` syntax seamlessly. | ||
Requirejs is not currently compatible with this library. | ||
## Test | ||
``` | ||
npm run test | ||
``` | ||
### Typescript Tests | ||
## Test | ||
``` | ||
npm run dtslint | ||
``` | ||
This library contains two types of tests. | ||
1. Typescript tests to ensure typing works as intended: `npm run dtslint` | ||
2. Unit tests to check the runtime functionality of the library: `npm run unit-test` | ||
### Unit Tests | ||
``` | ||
npm run unit-test | ||
``` | ||
Both test suites are run when using `npm run test` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
32783
40
325
391