Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model not getting injected while unit/integration test #88

Open
th-tarang-sachdev opened this issue Oct 30, 2020 · 0 comments
Open

Model not getting injected while unit/integration test #88

th-tarang-sachdev opened this issue Oct 30, 2020 · 0 comments

Comments

@th-tarang-sachdev
Copy link

th-tarang-sachdev commented Oct 30, 2020

Hello , I am facing issue for getting injected model instance in test case
here is the detail

I have test file called
inventory.service.test.ts

import { Container } from 'typedi';
import InventoryService from '../../../src/services/inventory';
import { random } from '../../util';
import express from 'express';
import loaders from '../../../src/loaders';
import Inventory from '../../../src/models/inventory';
import startServer from '../../../src/server';

let body;
beforeAll(async done => {
  // const expressApp = express();
  // await loaders({ expressApp });
  await startServer();
  body = random.inventory();
  done();
});

describe('Inventory Service', () => {
  it('should add inventory', async () => {
    const InventoryModel: typeof Inventory = Container.get('InventoryModel');
    const Logger = Container.get('logger');
    const inventoryService = new InventoryService(InventoryModel, Logger);
    const { inventory } = await inventoryService.addInventory(body);
    expect(inventory).toBeDefined();
    expect(inventory.id).toBeDefined();
    expect(inventory.title).toBe(body.title);
  });
});

when I do like above, all test cases passes.

now I am trying to start up the server in global.setup.ts file, not in beforeAll of each test case file

so I am doing like that in jest.config.ts file

// A path to a module which exports an async function that is triggered once before all test suites
  globalSetup : "./tests/jest/globalSetup.ts",

// A path to a module which exports an async function that is triggered once after all test suites
  globalTeardown : "./tests/jest/globalTearDown.ts",

and I have added 2 files

globalSetup.ts

import startServer from '../../src/server'
const globalSetup = async () => {
    await startServer();
}

export default globalSetup;

and in globalTearDown.ts


const globalTearDown = async () => {
    // will close down that server instance
}

export default globalTearDown 

when I do test now it is throwing an error like below

_ServiceNotFoundError: Service "InventoryModel" was not found, looks like it was not registered in the container. Register it by calling Container.set("InventoryModel", ...) before using service._

@th-tarang-sachdev th-tarang-sachdev changed the title Model not injected while unit/integration test Model not getting injected while unit/integration test Oct 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant