mocha-typescript
Advanced tools
Comparing version 1.1.15 to 1.1.16
@@ -73,2 +73,12 @@ declare namespace Mocha { | ||
} | ||
export interface TestClass<T> { | ||
new(...args: any[]): T; | ||
prototype: T; | ||
} | ||
export interface DependencyInjectionSystem { | ||
handles<T>(cls: TestClass<T>): boolean; | ||
create<T>(cls: TestClass<T>): typeof cls.prototype; | ||
} | ||
} | ||
@@ -94,2 +104,4 @@ | ||
export const skipOnError: MochaTypeScript.SuiteTrait; | ||
export function registerDI(instantiator: MochaTypeScript.DependencyInjectionSystem): boolean; | ||
} |
29
index.js
@@ -103,3 +103,3 @@ "use strict"; | ||
beforeEachFunction = noname(function (done) { | ||
instance = new target(); | ||
instance = getInstance(target); | ||
applyDecorators(this, prototype, prototype.before, instance); | ||
@@ -111,3 +111,3 @@ return prototype.before.call(instance, done); | ||
beforeEachFunction = noname(function () { | ||
instance = new target(); | ||
instance = getInstance(target); | ||
applyDecorators(this, prototype, prototype.before, instance); | ||
@@ -120,3 +120,3 @@ return prototype.before.call(instance); | ||
beforeEachFunction = noname(function () { | ||
instance = new target(); | ||
instance = getInstance(target); | ||
}); | ||
@@ -632,4 +632,27 @@ } | ||
} | ||
const defaultDependencyInjectionSystem = { | ||
handles() { return true; }, | ||
create(cls) { | ||
return new cls(); | ||
}, | ||
}; | ||
const dependencyInjectionSystems = [defaultDependencyInjectionSystem]; | ||
function getInstance(testClass) { | ||
const di = dependencyInjectionSystems.find((di) => di.handles(testClass)); | ||
return di.create(testClass); | ||
} | ||
/** | ||
* Register a dependency injection system. | ||
*/ | ||
function registerDI(instantiator) { | ||
// Maybe check if it is not already added? | ||
if (dependencyInjectionSystems.some((di) => di === instantiator)) { | ||
return false; | ||
} | ||
dependencyInjectionSystems.unshift(instantiator); | ||
return true; | ||
} | ||
exports.registerDI = registerDI; | ||
module.exports = Object.assign(tsdd, exports); | ||
Mocha.interfaces["mocha-typescript"] = tsdd; | ||
//# sourceMappingURL=index.js.map |
41
index.ts
@@ -140,3 +140,3 @@ import * as Mocha from "mocha"; | ||
beforeEachFunction = noname(function(this: Mocha.IHookCallbackContext, done: Function) { | ||
instance = new target(); | ||
instance = getInstance(target); | ||
applyDecorators(this, prototype, prototype.before, instance); | ||
@@ -147,3 +147,3 @@ return prototype.before.call(instance, done); | ||
beforeEachFunction = noname(function(this: Mocha.IHookCallbackContext) { | ||
instance = new target(); | ||
instance = getInstance(target); | ||
applyDecorators(this, prototype, prototype.before, instance); | ||
@@ -155,3 +155,3 @@ return prototype.before.call(instance); | ||
beforeEachFunction = noname(function(this: Mocha.IHookCallbackContext) { | ||
instance = new target(); | ||
instance = getInstance(target); | ||
}); | ||
@@ -682,3 +682,38 @@ } | ||
} | ||
interface TestClass<T> { | ||
new(...args: any[]): T; | ||
prototype: T; | ||
} | ||
interface DependencyInjectionSystem { | ||
handles<T>(cls: TestClass<T>): boolean; | ||
create<T>(cls: TestClass<T>): typeof cls.prototype; | ||
} | ||
const defaultDependencyInjectionSystem: DependencyInjectionSystem = { | ||
handles() { return true; }, | ||
create<T>(cls: TestClass<T>) { | ||
return new cls(); | ||
}, | ||
}; | ||
const dependencyInjectionSystems: DependencyInjectionSystem[] = [defaultDependencyInjectionSystem]; | ||
function getInstance<T>(testClass: TestClass<T>) { | ||
const di = dependencyInjectionSystems.find((di) => di.handles(testClass)); | ||
return di.create(testClass); | ||
} | ||
/** | ||
* Register a dependency injection system. | ||
*/ | ||
export function registerDI(instantiator: DependencyInjectionSystem) { | ||
// Maybe check if it is not already added? | ||
if (dependencyInjectionSystems.some((di) => di === instantiator)) { return false; } | ||
dependencyInjectionSystems.unshift(instantiator); | ||
return true; | ||
} | ||
module.exports = Object.assign(tsdd, exports); | ||
(Mocha as any).interfaces["mocha-typescript"] = tsdd; |
{ | ||
"name": "mocha-typescript", | ||
"version": "1.1.15", | ||
"version": "1.1.16", | ||
"description": "TypeScript decorators based wrapper over mocha's interface", | ||
@@ -19,3 +19,4 @@ "main": "index.js", | ||
"tslint-fix": "tslint --fix --project .", | ||
"tslint": "tslint --project ." | ||
"tslint": "tslint --project .", | ||
"changelog": "conventional-changelog --infile CHANGELOG.md --same-file --preset angular --append --release-count 1 --verbose" | ||
}, | ||
@@ -38,5 +39,6 @@ "author": "Panayot Cankov", | ||
"@types/chai": "^4.1.3", | ||
"@types/cross-spawn": "^6.0.0", | ||
"@types/node": "^10.1.4", | ||
"@types/cross-spawn": "^6.0.0", | ||
"chai": "^4.1.2", | ||
"conventional-changelog-cli": "^2.0.1", | ||
"mocha": "^5.2.0", | ||
@@ -46,3 +48,5 @@ "rimraf": "^2.6.2", | ||
"tslint": "^5.10.0", | ||
"typescript": "^2.9.1" | ||
"typescript": "^2.9.1", | ||
"reflect-metadata": "^0.1.12", | ||
"typedi": "^0.7.3" | ||
}, | ||
@@ -49,0 +53,0 @@ "files": [ |
@@ -41,2 +41,4 @@ Writing mocha tests with style - OOP style: | ||
- [Skipping Tests In Suite After First Failure - skipOnError](#skipping-tests-in-suite-after-first-failure---skiponerror) | ||
- [Dependency Injection](#dependency-injection) | ||
- [typedi](#typedi) | ||
@@ -524,3 +526,3 @@ # Summary | ||
That object is exposed to the TypeScript decorators based UI through a field decorated with the `@context` decorator: | ||
``` | ||
``` TypeScript | ||
@suite class MyClass { | ||
@@ -539,3 +541,3 @@ @context mocha; // Set for instenace methods such as tests and before/after | ||
This can be done with the `skipOnError` suite trait: | ||
``` | ||
``` TypeScript | ||
@suite(skipOnError) | ||
@@ -549,1 +551,30 @@ class StockSequence { | ||
``` | ||
# Dependency Injection | ||
Custom dependency injection systems can be provided using `registerDI`. | ||
## typedi | ||
To use the built-in support: | ||
- Set your `tsconfig.json` to `emitDecoratorMetadata`. | ||
- Import `mocha-typescript/di/typedi`. | ||
This will let test instances to be instantiated using typedi, for example: | ||
``` TypeScript | ||
import { assert } from "chai"; | ||
import { Service } from "typedi"; | ||
import "../../di/typedi"; | ||
import { register, suite, test } from "../../index"; | ||
@Service() | ||
class Add { | ||
public do(a: number, b: number) { | ||
return a + b; | ||
} | ||
} | ||
@suite class TypeDITest { | ||
// typedi will resolve `add` here to an instance of the `Add` service. | ||
constructor(public add: Add) { } | ||
@test public "test linear function"() { | ||
assert.equal(this.add.do(1, 2), 3); | ||
} | ||
} | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
120860
10
1580
576
12