Nest: Correct way to reference other Entities/Models and Services from other modules

Created on 1 Sep 2018  路  2Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior


There doesn't appear to be any documentation on how to import Services, Models (TypeORM) or Repositories from other modules in nest.js. The auto completion in Visual Studio Code works for running the application.

import { Base } from 'common/model/Base.model';

However when you run tests in ts-jest, we always get the following error.

Cannot find module 'common/model/Base.model' from 'Event.model.ts'

Changing the import to a relative path below fixes the issue.

import { Base } from '../../common/model/Base.model';

but is this the right way to do this?

Expected behavior

import { Base } from 'common/model/Base.model'; should satisfy both Nest.js and ts-jest tests.

Minimal reproduction of the problem with instructions

Create an application and a module called common and another called events

// common/model/Base.model.ts

export abstract class Base {
    id: number;
    created: Date;
    updated: Date;
}

// events/model/Event.model.ts

import { Base } from 'common/model/Base.model';
export class Event extends Base {}

// events/model/Event.model.spec.ts

import { Event } from './event.model';

describe('EventModel', () => {
    it(`This doesn't work`, () => {
        const event = new Event();
        expect(event).not.toBeUndefined();
    });
})

Run yarn test

What is the motivation / use case for changing the behavior?


So that my tests pass, and there's clear documentation on how to ensure that ts-jest and Nest.js work together.

Environment


Nest version: 5.0.0-beta.6


For Tooling issues:
- Node version: v10.4.1  
- Platform:  Mac, running Docker - gcr.io/google-appengine/nodejs 

Others:

No

Most helpful comment

I managed to resolve this by setting the jest.modulePaths property in package.json to ["<rootDir>"]

```
{
...
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"modulePaths": [""],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\.(t|j)s$": "ts-jest"
},
"mapCoverage": true,
"coverageDirectory": "../coverage"
}
}

All 2 comments

I managed to resolve this by setting the jest.modulePaths property in package.json to ["<rootDir>"]

```
{
...
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"modulePaths": [""],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\.(t|j)s$": "ts-jest"
},
"mapCoverage": true,
"coverageDirectory": "../coverage"
}
}

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rafal-rudnicki picture rafal-rudnicki  路  3Comments

KamGor picture KamGor  路  3Comments

thohoh picture thohoh  路  3Comments

artaommahe picture artaommahe  路  3Comments

breitsmiley picture breitsmiley  路  3Comments