Skip to content

Commit

Permalink
move tests to src
Browse files Browse the repository at this point in the history
  • Loading branch information
Frondor committed Dec 14, 2021
1 parent b10de70 commit 819e5ac
Show file tree
Hide file tree
Showing 9 changed files with 7,591 additions and 9,027 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
'plugin:jest/all',
'prettier',
],
plugins: ['@typescript-eslint', 'jest', 'standard', 'prettier'],
plugins: ['@typescript-eslint', 'jest', 'prettier'],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
Expand Down
16,510 changes: 7,528 additions & 8,982 deletions package-lock.json

Large diffs are not rendered by default.

53 changes: 28 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
"service-locator",
"inversion-of-control"
],
"main": "dist/binder.js",
"exports": "./dist/binder.modern.js",
"main": "./dist/binder.js",
"exports": {
"require": "./dist/binder.js",
"default": "./dist/binder.modern.js"
},
"module": "dist/binder.module.js",
"unpkg": "dist/binder.umd.js",
"types": "dist/index.d.ts",
"sideEffects": false,
"files": [
"dist"
Expand All @@ -23,37 +27,36 @@
},
"scripts": {
"lint": "eslint . --ext .js,.ts --cache --fix",
"prettier:fix": "prettier 'src' 'test' --write",
"prettier:fix": "prettier --write src",
"build": "microbundle src/index.ts",
"test:ci": "jest --coverage",
"test:ci": "jest",
"test": "jest",
"preversion": "git merge origin/master && git push -u origin HEAD && npm ci && npm run lint && npm run build && npm test",
"postversion": "git push && git push --tags",
"prepare": "husky install"
},
"devDependencies": {
"@types/jest": "26.0.22",
"@types/node": "14.14.37",
"@typescript-eslint/eslint-plugin": "4.21.0",
"@typescript-eslint/parser": "4.21.0",
"eslint": "7.24.0",
"eslint-config-prettier": "8.1.0",
"eslint-config-standard": "16.0.2",
"eslint-plugin-import": "2.22.1",
"eslint-plugin-jest": "24.3.5",
"@types/jest": "27.0.3",
"@types/node": "16.11.12",
"@typescript-eslint/eslint-plugin": "5.7.0",
"@typescript-eslint/parser": "5.7.0",
"eslint": "7.32.0",
"eslint-config-prettier": "8.3.0",
"eslint-config-standard": "16.0.3",
"eslint-plugin-import": "2.25.3",
"eslint-plugin-jest": "25.3.0",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-prettier": "3.3.1",
"eslint-plugin-promise": "5.1.0",
"eslint-plugin-standard": "4.1.0",
"husky": "6.0.0",
"jest": "26.6.3",
"jest-config": "26.6.3",
"lint-staged": "10.5.4",
"microbundle": "0.13.0",
"prettier": "2.2.1",
"ts-jest": "26.5.4",
"ts-node": "9.1.1",
"typescript": "4.2.4"
"eslint-plugin-prettier": "4.0.0",
"eslint-plugin-promise": "5.2.0",
"husky": "7.0.4",
"jest": "27.4.5",
"jest-config": "27.4.5",
"lint-staged": "12.1.2",
"microbundle": "0.14.2",
"prettier": "2.5.1",
"ts-jest": "27.1.1",
"ts-node": "10.4.0",
"typescript": "4.5.4"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/Binding.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Container } from '.'
import { Resolvable } from './types'
import { Resolvable } from './types.d'

interface BindingContract {
resolvable: Resolvable<unknown>
Expand Down
2 changes: 1 addition & 1 deletion src/Container.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-dupe-class-members */
import Binding from './Binding'
import { Key, Resolvable } from './types'
import { Key, Resolvable } from './types.d'

export default class Container {
protected bindings = new Map()
Expand Down
40 changes: 27 additions & 13 deletions test/container.test.ts → src/container.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Container } from '../src'
import { Container } from '.'

class Animal {
name: string
Expand All @@ -11,14 +11,9 @@ class Dog extends Animal {}
class Cat extends Animal {}

describe('container', () => {
let container = new Container()
// eslint-disable-next-line jest/no-hooks
beforeEach(() => {
container = new Container()
})

describe('when resolving classes', () => {
it('should return distinct instances of that class', () => {
const container = new Container()
container.bind(Animal)
const animal1 = container.get(Animal)
const animal2 = container.get(Animal)
Expand All @@ -28,6 +23,7 @@ describe('container', () => {
})

it('should resolve function-like classes', () => {
const container = new Container()
// eslint-disable-next-line @typescript-eslint/no-empty-function
const LegacyClass = function () {}
container.bind(LegacyClass)
Expand All @@ -37,42 +33,47 @@ describe('container', () => {

describe('when resolving factories', () => {
it('should resolve factory: arrow function', () => {
const container = new Container()
container.instance('name', 'Rocco')
container.bind('dog', (container: Container) => new Dog(container.get('name') as string))
const dog = container.get('dog') as Dog
expect(dog).toBeInstanceOf(Dog)
expect(dog.name).toStrictEqual('Rocco')
expect(dog.name).toBe('Rocco')
})

it('should resolve factory: non-arrow function', () => {
const container = new Container()
container.instance('name', 'Rocco')
container.bind('dog', function (container: Container) {
return new Dog(container.get('name') as string)
})
const dog = container.get('dog') as Dog
expect(dog).toBeInstanceOf(Dog)
expect(dog.name).toStrictEqual('Rocco')
expect(dog.name).toBe('Rocco')
})

it('should resolve factory: bound arrow function (edge case)', () => {
const container = new Container()
container.instance('name', 'Rocco')
container.bind('dog', (container: Container) => new Dog(container.get('name') as string))
const dog = container.get('dog') as Dog
expect(dog).toBeInstanceOf(Dog)
expect(dog.name).toStrictEqual('Rocco')
expect(dog.name).toBe('Rocco')
})

it('should resolve factory: bound non-arrow function (edge case)', () => {
const container = new Container()
container.instance('name', 'Rocco')
container.bind('dog', function (container: Container) {
return new Dog(container.get('name') as string)
})
const dog = container.get('dog') as Dog
expect(dog).toBeInstanceOf(Dog)
expect(dog.name).toStrictEqual('Rocco')
expect(dog.name).toBe('Rocco')
})

it('should resolve accepting params', () => {
const container = new Container()
container.instance('name', 'Rocco')
// Arrow function
container.bind(
Expand All @@ -84,20 +85,22 @@ describe('container', () => {
container.bind('catdog', function (container: Container, param1: unknown, param2: unknown) {
return new Dog(container.get('name') + (param1 as string) + param2)
})
expect(container.make('dog', ' es ', 'lindo').name).toStrictEqual('Rocco es lindo')
expect(container.make('catdog', ' es ', 'feo').name).toStrictEqual('Rocco es feo')
expect(container.make('dog', ' es ', 'lindo').name).toBe('Rocco es lindo')
expect(container.make('catdog', ' es ', 'feo').name).toBe('Rocco es feo')
})
})

describe('when resolving singletons', () => {
it('class: should always return the same instance', () => {
const container = new Container()
container.singleton(Animal, Dog)
const dog1 = container.get(Animal)
const dog2 = container.get(Animal)
expect(dog1).toStrictEqual(dog2)
})

it('factory: should always return the same instance', () => {
const container = new Container()
container.singleton(Animal, () => new Dog(''))
const dog1 = container.get(Animal)
const dog2 = container.get(Animal)
Expand All @@ -107,6 +110,7 @@ describe('container', () => {

describe('when binding instances', () => {
it('should always get() the same reference', () => {
const container = new Container()
container.instance(Animal, new Dog(''))
const dog1 = container.get(Animal)
const dog2 = container.get(Animal)
Expand All @@ -116,18 +120,21 @@ describe('container', () => {

describe('when binding already bound keys', () => {
it('should override class bindings', () => {
const container = new Container()
container.bind(Animal, Dog)
container.bind(Animal, Cat)
expect(container.get(Animal)).toBeInstanceOf(Cat)
})

it('should override singletons bindings', () => {
const container = new Container()
container.singleton(Animal, () => new Dog(''))
container.singleton(Animal, () => new Cat(''))
expect(container.get(Animal)).toBeInstanceOf(Cat)
})

it('should override factory bindings', () => {
const container = new Container()
container.bind(Animal, () => new Dog(''))
container.bind(Animal, function () {
return new Cat('')
Expand All @@ -138,6 +145,7 @@ describe('container', () => {
})

it('should override instance bindings', () => {
const container = new Container()
container.instance(Animal, new Dog(''))
container.instance(Animal, new Cat(''))
expect(container.get(Animal)).toBeInstanceOf(Cat)
Expand All @@ -146,6 +154,7 @@ describe('container', () => {

describe('when aliasing bindings', () => {
it('should accept any primitive as the key', () => {
const container = new Container()
container.bind(Dog)
container.alias('dog', Dog)
const dogObj = {}
Expand All @@ -161,6 +170,7 @@ describe('container', () => {

describe('when passing params', () => {
it('should resolve class with params', () => {
const container = new Container()
class DogWrapper extends Dog {
constructor(container: Container, name: string) {
super(name)
Expand All @@ -172,13 +182,15 @@ describe('container', () => {
})

it('should resolve factory with params', () => {
const container = new Container()
container.bind(Dog, (container: Container, name = 'Milo') => new Dog(name))
expect((container.get(Dog) as Dog).name).toBe('Milo')
expect(container.make(Dog).name).toBe('Milo')
expect(container.make(Dog, 'Rocco').name).toBe('Rocco')
})

it('should receive container as only param if the class does not injects dependencies', () => {
const container = new Container()
class ContainerWrapper {
theContainer: Container
constructor(container: Container) {
Expand Down Expand Up @@ -209,6 +221,7 @@ describe('container', () => {
})

describe('when using make() with unbound classes', () => {
const container = new Container()
it('should do a one-off resolve', () => {
expect(container.has(Dog)).toBe(false)
expect(container.make(Dog)).toBeInstanceOf(Dog)
Expand All @@ -217,6 +230,7 @@ describe('container', () => {

describe('when binding resolvables', () => {
it('should accept any primitive as the key', () => {
const container = new Container()
container.bind(Dog)
container.bind('dog', Dog)
const dogObj = {}
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"extends": "./tsconfig.json",
"include": ["src/**/*.ts", "test/**/*.test.ts"]
"include": ["src/**/*.ts"],
"exclude": []
}
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
"esModuleInterop": true,
"sourceMap": true,
"allowJs": true,
"declaration": true,
"noImplicitAny": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declarationDir": "dist/types",
"declaration": true,
"typeRoots": ["node_modules/@types"]
},
"include": ["src/**/*.ts", "test/**/*.ts"]
"include": ["src/**/*.ts"],
"exclude": ["src/**/*.test.ts"]
}

0 comments on commit 819e5ac

Please sign in to comment.