用Jest找不到模块

时间:2020-06-09 18:12:15

标签: javascript typescript jestjs

我有一个要在nodeJS项目中用Jest测试的类

import moment from 'moment-timezone';

export class DateUtil {       

    static toTimeZone(date: Date, tz: string): string {
        return moment(date).tz(tz).format();
    }    
}

我创建了此测试:

import { DateUtil } from '../../src/util/date.util';


describe('DateUtil', () => {
..

但是当我运行测试时,出现此错误:

  ● Test suite failed to run

    Cannot find module 'moment-timezone' from 'date.util.ts'

      1 | import { Constants } from './constants';
    > 2 | import moment from 'moment-timezone';
        | ^
      3 | 
      4 | 
      5 | export class DateUtil {

我试图在测试类中使用导入

import moment from 'moment-timezone';

但是我有这个问题

TS1259: Module '"C:/Users/sandro/IdeaProjects/bendiciones/node_modules/@types/moment-timezone/index"' can only be default-imported using the 'esModuleInterop' flag

这是我的tsconfig.json文件,其中esModuleInterop设置为true

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "lib": [
            "es6",
            "esnext"
        ],
        "declaration": true,
        "sourceMap": true,
        "outDir": "lib",
        "rootDir": "src",
        "removeComments": true,
        "strict": true,
        "noImplicitAny": true,
        "moduleResolution": "node",
        "strictNullChecks": true,
        "resolveJsonModule": true,
        "esModuleInterop": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true
    },
    "exclude": [
        "test"
    ]
}

我也尝试过使用此tsconfig.json并得到相同的结果

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "lib": [
            "es6",
            "esnext"
        ],
        "declaration": true,
        "sourceMap": true,
        "outDir": "lib",
        "rootDir": "src",
        "removeComments": true,
        "strict": true,
        "noImplicitAny": true,
        "moduleResolution": "node",
        "strictNullChecks": true,
        "resolveJsonModule": true,
        "esModuleInterop": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true
    }
}

这就是在package.json中配置jtest的方式

  "jest": {
        "transform": {
            "^.+\\.tsx?$": "ts-jest"
        },
        "testEnvironment": "node",
        "testRegex": "/test/.*\\.(test|it)\\.ts",
        "testPathIgnorePatterns": [
            "/coverage",
            "/lib"
        ],
        "moduleFileExtensions": [
            "ts",
            "tsx",
            "js",
            "jsx",
            "json",
            "node"
        ],
        "coverageThreshold": {
            "global": {
                "branches": 80,
                "functions": 80,
                "lines": 80,
                "statements": -20
            }
        },
        "collectCoverageFrom": [
            "src/**/*.ts",
            "!src/index.ts"
        ],
        "coverageReporters": [
            "json-summary",
            "text",
            "lcov"
        ]
    },

0 个答案:

没有答案