我有一个正在使用TS和汇总的项目。我的代码可以构建并正常运行,但是当我通过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文件:(我已经尝试从排除中删除规格文件,但不幸的是,它相同)
{
"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
})
],
}
答案 0 :(得分:0)
您的摩卡咖啡设置看起来正确。
./node_modules/mocha/bin/mocha --inspect-brk=9230 -r ts-node/register tests/**/*spec.ts"
npm i ts-node
吗?inspect-brk
会停止执行,直到您在检查器中继续执行? +同时检查端口答案 1 :(得分:0)
使用以下方法解决了此问题:TS_NODE_COMPILER_OPTIONS={\\\"module\\\":\\\"commonjs\\\"} mocha -r ts-node/register test/**/*.spec.ts