Jest cannot find module sometimes

Viewed 1750

actually i'm trying to test a function and getting that error:

error

Files that i'm using:

searchUserService

import { SearchUserService } from '../../src/service/searchUser.service';
import { SearchUserRepository} from '../../src/repository/searchUser.repository'
import { Util } from 'src/util/util'

let userId = '123456'

describe('', () => {
  it('', async () =>{

  const res =  new SearchUserService(new SearchUserRepository(new Util)).searchCredential(userId) 

    expect(res).toBe('')
})
})

package.json

    {
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nodemon",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "tslint -p tsconfig.json -c tslint.json",
"test": "node_modules/.bin/jest --config jest.conf.js --detectOpenHandles",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "6.7.2",
"@nestjs/core": "6.7.2",
"@nestjs/platform-express": "6.7.2",
"axios": "0.19.2",
"lodash": "4.17.15",
"reflect-metadata": "0.1.13",
"rimraf": "3.0.0",
"rpc-proxy": "^1.0.7",
"rxjs": "6.5.3",
"swagger-ui-express": "4.1.3",
"typescript": "3.6.3"
},
"devDependencies": {
"@nestjs/cli": "6.9.0",
"@nestjs/schematics": "6.7.0",
"@nestjs/swagger": "4.2.7",
"@nestjs/testing": "6.7.1",
"@types/express": "4.17.1",
"@types/jest": "24.0.18",
"@types/node": "12.7.5",
"@types/supertest": "2.0.8",
"dotenv": "8.2.0",
"jest": "24.9.0",
"nodemon": "2.0.2",
"prettier": "1.18.2",
"supertest": "^4.0.2",
"ts-jest": "24.1.0",
"ts-node": "8.4.1",
"tsconfig-paths": "3.9.0",
"tslint": "5.20.0",
"yamljs": "0.3.0"
}
}

searchUser.service.ts

  import { Injectable } from '@nestjs/common';
  import { BaseService } from '********************';
  import { SearchUserRepository } from '../repository/searchUser.repository';

  @Injectable()
  export class SearchUserService extends BaseService {
   constructor(private readonly searchUserRepository: SearchUserRepository) {
    super();
 }

async searchCredential(userId: string) {

  this.logger.info(`[SearchUserService] - searchCredential - userId = ${userId}`, '');

  return await this.searchUserRepository.searchCredential(userId);
}
}

jest.conf.js

        const path = require('path');

    module.exports = {
    rootDir: path.resolve(__dirname, './'),
    verbose: true,
    preset: 'ts-jest',
    testEnvironment: 'node',
    collectCoverage: true,
    coverageDirectory: process.env.JEST_CLOVER_OUTPUT_DIR || './_devops/coverage',
    coverageReporters: ['text', 'clover', 'lcov'],
    collectCoverageFrom: [
        '**/src/**/*.{ts}',
        '!**/spec/**/*.{ts}',
        '!**/tests/**/*.{ts}',
    ],
    moduleFileExtensions: [
        'ts',
        'tsx',
        'js',
        'json',
        'jsx'
    ]
    };

I've tried with another functions and debug, when i start the debug, the error instantly happens doesn't give any chance, with another functions I get the same.

it appears to be an Import error from Jest or NestJs.

I've checked the documentation of Jest and some Issues that appears to be same as i'm getting, but non of them resolved my problem.

0 Answers
Related