开玩笑有时找不到模块

时间:2020-03-04 20:45:42

标签: javascript testing jestjs nestjs

实际上,我正在尝试测试功能并收到该错误:

error

我正在使用的文件:

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'
    ]
    };

我尝试了其他功能并进行调试,当我开始调试时,错误立即发生并没有任何机会,而其他功能我得到了相同的结果。

这似乎是来自Jest或NestJs的导入错误。

我已经检查了Jest的文档以及一些与我所获得的问题类似的文件,但没有一个解决了我的问题。

0 个答案:

没有答案