Skip to content

unlight/inject

Repository files navigation

inject

Inject a dependency (service locator pattern).

USAGE

// app.ts
import { inject } from 'njct';
const fs = inject('fs', () => require('fs'));
fs.readFileSync('data.json');

// app.spec.ts
import { injector } from 'njct';
injector.provide('fs', () => ({
  readFileSync: () => 'result of call of fs.readFileSync()',
}));
class Car {
  static count = 0;
  constructor() {
    Car.count++;
  }
}
let vehicle = inject(Car);
vehicle = inject(Car);
expect(vehicle).toBeA(Car);
expect(Car.count).toEqual(1);

Similar Projects

License

MIT License (c) 2022

About

Inject a dependency (service locator pattern)

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •