Skip to content

Commit

Permalink
Update readme override and unit testing sections
Browse files Browse the repository at this point in the history
  • Loading branch information
betula committed Jun 26, 2019
1 parent 3440f10 commit aac449d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tmp
examples
.editorconfig
tslint.json
Expand Down
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export default class App {
) { /* ... */ }
// ...
}

// index.ts
new App().start(); // You can create instance directly as usually class
```

JavaScript with decorators
Expand Down Expand Up @@ -86,6 +89,9 @@ export default class App {
}
// ...
}

// index.js
new App().start(); // You can create instance directly as usually class
```

Pure JavaScript without decorators
Expand Down Expand Up @@ -141,10 +147,88 @@ class App {
// ...
}
module.exports = inject(services)(App);

// index.js
new App().start(); // You can create instance directly as usually class
```

## Override dependencies

If you use modules architecture of your application you can override you dependencies.

```TypeScript
import { override, inject } from "node-provide";

class A {
send() {
console.log("Hello A!");
}
}

@inject
class B {
constructor(private a: A) {
// After `override(A, A2)` property
// `this.a` will be instance of A2, but not A
this.a.send();
}
}

class A2 {
send() {
console.log("Hello A2!");
}
}

override(A, A2); // After then A and A2 dependencies will use only one instance of A2
new B(); // "Hello A2!"
```

## Unit testing

You can use `override` or `assign` for provide mocks in you dependencies.

```TypeScript
import { override, inject } from "node-provide";

// world.ts
class World {
hello() {
// ...
}
}

// hello.ts
@inject
class Hello {
constructor(world: World) {
this.world.hello();
}
}

// hello.test.ts
it("It works!", () => {
const worldMock = {
hello: jest.fn(),
}
assign(World, worldMock);
new Hello();
expect(worldMock.start).toBeCalled();
})
```

If you use `Jest` for unit testing you need add some code to your `jest.config.js` file.
```JavaScript
// jest.config.js
{
// ...
setupFilesAfterEnv: [ "node-provide/jest-cleanup-after-each" ],
// ...
}
```

This code means that after each test cached dependency instances will be clear.


## API Reference

Expand All @@ -166,6 +250,8 @@ module.exports = inject(services)(App);

**isolate**

**reset**


---

Expand Down

0 comments on commit aac449d

Please sign in to comment.