You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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",
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._
The text was updated successfully, but these errors were encountered:
th-tarang-sachdev
changed the title
Model not injected while unit/integration test
Model not getting injected while unit/integration test
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
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
and I have added 2 files
globalSetup.ts
and in globalTearDown.ts
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._
The text was updated successfully, but these errors were encountered: