Skip to content
/ inject Public

Inject a dependency (service locator pattern)

Notifications You must be signed in to change notification settings

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