Skip to content

QUESTION: dependency injection for unit tests #1

@kpeters-cbsi

Description

@kpeters-cbsi

So how do you do dependency injection for testing with this framework? Say I have a function, listThings, implemented like so:


import type { Props } from '@exobase/core';
import makeStorageService, {
  StorageService,
} from '../storage-service';
import { useService } from '@exobase/hooks';
import _ from 'radash';
import config from '../config';

interface Args {
  listThingsArgs: any;
}

interface Services {
  storage: StorageService;
}

type Response = Array<string>;

async function listThings({args, services}: Props<Args, Services>): Promise<Response> {
  return this.services.storage.listThings(args.listThingsArgs)
}

export default _.compose(
  useService<Services>({
    storage: await makeStorageService(config.storageService)
  }),
  listThings
);

(As I'm sure you can deduce, I lifted a lot of this pattern from your published endpoint code)

So two things to note here:

First, there's no framework code here. While this is intended to run in Lambda / API Gateway, my goal is to be able to compose the framework code from Exobase with my function code so that I can very easily port this to another framework if need be.

Second, I'm loading my config in the function definiiton for listThings. If I want to use a custom config for my tests, how am I supposed to inject that? Or should I just mock the call to makeStorageService?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions