开玩笑的子代码与ts-node

问题描述

正常运行项目并在Jest中运行时,我无法使代码正常工作。

运行项目需要将dayjs导入这种类型:

import * as dayjs from 'dayjs';
import * as utc from 'dayjs/plugin/utc';
import * as customParseFormat  from 'dayjs/plugin/customParseFormat';

npm run dev(nodemon --exec ./node_modules/.bin/ts-node ./src/app.ts):没有问题

笑话: TypeError: t is not a function

运行Jest测试需要将dayjs导入这种类型:

import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import customParseFormat  from 'dayjs/plugin/customParseFormat';

npm run dev(nodemon --exec ./node_modules/.bin/ts-node ./src/app.ts):

dayjs.extend(utc);
      ^
TypeError: Cannot read property 'extend' of undefined`

笑话:成功!

所以现在我无法编写测试。为什么要进行不同的翻译?

解决方法

我切换到ts-jest,它解决了我的问题。我以为可以编写JS测试来测试TS代码,但我认为那是不可能的。

,

就我而言,

tsconfig.json

{
  "compilerOptions": {
    "esModuleInterop": true
  }
}

解决了。