摩卡测试使用打字稿失败

问题描述 投票:1回答:1

我有一个项目,我正在使用TS和Rollup。我的代码构建并运行正常,但是当我通过npm run test运行测试时,它们会失败并显示SyntaxError: Unexpected token {错误消息。

现在我知道问题在于ES6语法,因为我在我的测试文件中使用import,似乎测试没有被编译但不确定原因。

我的package.json的相关部分:

 "main": "./dist/index.js",
  "module": "dist/index.es.js",
  "types": "./dist/index.d.ts",
  "scripts": {
    "build": "rollup -c",
    "watch": "rollup -cw",
    "prepublish": "rollup -c > index.js && BROWSER=true rollup -c > browser.js",
    "clean": "rm -rf dist/",
    "test": "mocha -r ts-node/register src/**/*.spec.ts",
    "testdebug": "mocha --inspect-brk --colors --recursive --require ts-node/register --watch-extensions ts \"**/*spec.ts\" --exclude \"node_modules/**\"",
    "lint": "tslint --project tsconfig.json -t codeFrame"
  },
  "devDependencies": {
    "@types/chai": "4.1.7",
    "@types/chai-as-promised": "7.1.0",
    "@types/long": "4.0.0",
    "@types/mocha": "5.2.6",
    "@types/node": "11.9.4",
    "@types/sinon": "7.0.6",
    "@types/sinon-chai": "3.2.2",
    "@types/supertest": "2.0.7",
    "@types/uuid": "3.4.4",
    "chai": "4.2.0",
    "chai-as-promised": "7.1.1",
    "husky": "1.3.1",
    "lint-staged": "8.1.4",
    "mocha": "5.2.0",
    "mocha-jenkins-reporter": "0.4.1",
    "prettier": "1.16.4",
    "rollup": "^1.10.0",
    "rollup-plugin-replace": "^2.2.0",
    "rollup-plugin-typescript2": "^0.20.1",
    "sinon": "7.2.3",
    "sinon-chai": "3.3.0",
    "supertest": "3.4.2",
    "ts-node": "8.0.2",
    "tslint": "5.12.1",
    "tslint-config-prettier": "1.18.0",
    "tslint-config-standard": "8.0.1",
    "tslint-microsoft-contrib": "6.0.0",
    "typescript": "^3.3.3"
  },

我的test.spec.ts文件:

    import { loginUser } from './loginUser'

    console.log(loginUser)

我的tsconfig文件:(我已经尝试从排除中删除spec文件,但遗憾的是它相同)

{
  "compilerOptions": {
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
    "module": "es6",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "declaration": true,                   /* Generates corresponding '.d.ts' file. */
    "outDir": "./dist",                        /* Redirect output structure to the directory. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "skipLibCheck": true,
    "sourceMap": true,
    "moduleResolution": "node"
   },
   "exclude":[
     "node_modules",
     "dist",
     "./**/*.spec.ts",
     "./test/**/*.ts"
   ]
  }

我的rollup.config文件

import typescript from 'rollup-plugin-typescript2'
import replace from 'rollup-plugin-replace'
import pkg from './package.json'
export default {
    input: 'src/index.ts',
    output: [
        {
            file: pkg.main,
            format: 'cjs',
        },
        {
            file: pkg.module,
            format: 'es',
        },
    ],
    external: [
        ...Object.keys(pkg.dependencies || {}),
        ...Object.keys(pkg.peerDependencies || {}),
    ],
    plugins: [
        typescript({
            typescript: require('typescript'),
        }),
        replace({
            'process.browser': !!process.env.BROWSER
        })
    ],
}
javascript typescript unit-testing ecmascript-6 mocha
1个回答
0
投票

你的摩卡设置看起来是正确的。

./node_modules/mocha/bin/mocha --inspect-brk=9230 -r ts-node/register tests/**/*spec.ts"

  • 你安装npm i ts-node了吗?
  • 您是否认为inspect-brk会在检查员中继续执行之前停止执行? +还检查端口
© www.soinside.com 2019 - 2024. All rights reserved.