Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用ts节点测试时,TypeScript Express Api类不是构造函数_Node.js_Typescript_Express_Mocha.js_Ts Node - Fatal编程技术网

Node.js 使用ts节点测试时,TypeScript Express Api类不是构造函数

Node.js 使用ts节点测试时,TypeScript Express Api类不是构造函数,node.js,typescript,express,mocha.js,ts-node,Node.js,Typescript,Express,Mocha.js,Ts Node,我使用typescript类设置了一个express应用程序,遇到了一个奇怪的问题。所有测试都通过了,今天当我去更新一些路由时,我的测试不再运行。当我运行测试脚本时,我会在控制台中返回以下错误消息: $ mocha -c --reporter spec --compilers ts:ts-node/register ./test/*.test.ts -- timeout 20000 /Users/christiantodd/Development/projects/bby-react-api/

我使用typescript类设置了一个express应用程序,遇到了一个奇怪的问题。所有测试都通过了,今天当我去更新一些路由时,我的测试不再运行。当我运行测试脚本时,我会在控制台中返回以下错误消息:

$ mocha -c --reporter spec --compilers ts:ts-node/register ./test/*.test.ts --
timeout 20000

/Users/christiantodd/Development/projects/bby-react-api/src/index.ts:8
const app: Api = new Api();
             ^
TypeError: Api_1.default is not a constructor
at Object.<anonymous> (/Users/christiantodd/Development/projects/bby-react-api/src/index.ts:8:18)
at Module._compile (module.js:635:30)
at Module.m._compile (/Users/christiantodd/Development/projects/bby-react-api/node_modules/ts-node/src/index.ts:392:23)
at Module._extensions..js (module.js:646:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/christiantodd/Development/projects/bby-react-api/node_modules/ts-node/src/index.ts:395:12)
和我的
索引.ts

import Api from './Api';
require('dotenv').config();
const mongoose = require('mongoose');
/* Set mongoose promise to native ES6 promise */
mongoose.Promise = global.Promise;

/* Instantiate our app instance */
const app: Api = new Api();

const connectOptions = {
  useMongoClient: true,
  keepAlive: true,
  reconnectTries: Number.MAX_VALUE
};

/* Get current environment */
export const ENV = app.currentEnv();

let DATABASE_URL;
let PORT;

/* set environment variables */
if (ENV === 'production') {
  DATABASE_URL = process.env.MONGODB_URI;
  PORT = parseInt(process.env.PORT, 10);
} else {
  DATABASE_URL = process.env.TEST_DATABASE_URL;
  PORT = 3000;
}

let server;

export const runServer = async (
  dbURL: string = DATABASE_URL,
  port: number = PORT
) => {
  try {
    await mongoose.connect(dbURL, connectOptions);
    await new Promise((resolve, reject) => {
      server = app.express
        .listen(port, () => {
          console.info(`The ${ENV} server is listening on port ${port} 

Debugging "
is not a ...
" errors

So this is likely NOT going to be the answer but this question was the top result for the error I was debugging and here is a debugging tip.

I was running
mocha
with the following:

test/mocha.opts

--require ts-node/register
--require source-map-support/register
--watch-extensions ts
从“/Api”导入Api;
require('dotenv').config();
const mongoose=require('mongoose');
/*将mongoose承诺设置为本机ES6承诺*/
mongoose.Promise=global.Promise;
/*实例化我们的应用程序实例*/
const-app:Api=new-Api();
常量连接选项={
useMongoClient:对,
基帕利夫:是的,
重新连接:Number.MAX\u值
};
/*获取当前环境*/
export const ENV=app.currentEnv();
让数据库访问URL;
让港口;
/*设置环境变量*/
如果(环境===‘生产’){
DATABASE_URL=process.env.MONGODB_URI;
PORT=parseInt(process.env.PORT,10);
}否则{
DATABASE_URL=process.env.TEST_DATABASE_URL;
端口=3000;
}
让服务器;
export const runServer=async(
dbURL:string=DATABASE\u URL,
端口号=端口号
) => {
试一试{
wait mongoose.connect(dbURL,connectOptions);
等待新的承诺((决定,拒绝)=>{
服务器=app.express
.侦听(端口,()=>{
info(`The${ENV}服务器正在侦听端口${port}调试“
不是一个…
”错误 所以这可能不是答案,但这个问题是我调试错误的首要结果,下面是一个调试提示

我当时正在用以下工具运行摩卡咖啡

测试/mocha.opts

import * as app from './app'
console.log({app}); // Pretty print object
{
  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    "target":"es2017",

    "esModuleInterop":true,
    // "strict": true,
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "types":["node"],
    "rootDirs":[
      "src"    
    ]
  },
  "include": [
    "src/**/*",
    "test/**/*"
  ],
  "exclude":[
    "**/node_modules/**"
  ]

}
但是无论我如何导入
/app
我都无法让类或函数正常工作,尽管
tsc
编译良好,
node
mocha
在编译的
.js
文件上工作良好

{
  "nyc": {
    "extension": [
      ".ts"
    ],
    "include": [
      "src/**/*.ts"
    ],
    "exclude": [
      "src/**/*-test.ts",
      "test/**/*.ts"
    ],
    "require": [
      "ts-node/register"
    ],
    "reporter": [
      "text-summary"
    ],
    "sourceMap": true,
    "instrument": true,
    "all": true
  }
}
正如所发生的那样,我有一个带有
app.json
app.ts
的Heroku项目

mocha
ts节点的组合将
.json
文件扩展名作为更高的优先级加载,而不是
.ts
文件和Typescript将不允许我指定文件扩展名。因此,这种行为在
tsc
mocha+ts节点
中不同

奖励积分-Typescript代码覆盖率 单元测试:
nyc摩卡src/***-test.ts

集成测试:
nyc摩卡测试/***.ts

package.json

import * as app from './app'
console.log({app}); // Pretty print object
{
  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    "target":"es2017",

    "esModuleInterop":true,
    // "strict": true,
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "types":["node"],
    "rootDirs":[
      "src"    
    ]
  },
  "include": [
    "src/**/*",
    "test/**/*"
  ],
  "exclude":[
    "**/node_modules/**"
  ]

}
tsconfig.json

import * as app from './app'
console.log({app}); // Pretty print object
{
  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    "target":"es2017",

    "esModuleInterop":true,
    // "strict": true,
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "types":["node"],
    "rootDirs":[
      "src"    
    ]
  },
  "include": [
    "src/**/*",
    "test/**/*"
  ],
  "exclude":[
    "**/node_modules/**"
  ]

}

您是如何运行单元测试的,
脚本的
如果您提供一个package.json文件将有助于解决问题,您最终会得到这个问题的答案吗?我想我有一个类似的问题,Mocha使用ts节点模块加载器,并试图解决一个express应用程序作为功能承诺的结果。