我正在嘗試進行一個簡單的測驗以了解使用 mocha 的單元測驗。
檔案夾結構
- 節點模塊
- 包.json
- 包鎖.json
- testA.ts
- testA.spec.ts
- 組態檔
組態檔
{
"compilerOptions": {
"target": "ES2019",
"module": "commonjs",
"rootDir": "./",
"moduleResolution": "node",
"allowJs": false,
"declaration": false,
"outDir": "./dist",
"esModuleInterop": false,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitAny": false,
"noImplicitThis": true,
"alwaysStrict": true,
"skipLibCheck": true
},
"exclude": [
"node_modules"
]
}
包.json
{
"name": "unittest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "mocha",
"build": "rm -rf dist && tsc"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/chai": "^4.2.22",
"@types/jest": "^27.0.3",
"@types/mocha": "^9.0.0",
"@types/sinon": "^10.0.6",
"chai": "^4.3.4",
"mocha": "^9.1.3",
"nyc": "^15.1.0",
"sinon": "^12.0.1",
"typescript": "^4.5.2",
"ts-node": "^10.4.0"
},
"mocha": {
"check-leaks": true,
"globals": [
"crypto"
],
"recursive": true,
"spec": [
"./*.ts"
]
}
}
testA.spec.ts
import * as chai from 'chai'
const expect = chai.expect
describe('First Test', () => {
it('should run the test', () => {
const a = 12
expect(a).to.be.equal(12)
})
})
當我運行時npm test,出現以下錯誤。
/home/user/dev/unitTest/node_modules/@babel/core/src/config/files/index-browser.ts:1 import type { Handler } from "gensync";
^^^^^^語法錯誤:不能在模塊外使用匯入陳述句
嘗試了所有可以在互聯網上搜索的內容,但仍然無法解決這個問題。
uj5u.com熱心網友回復:
我找到了解決方案。在里面package.json我添加了對 mocha的要求:
"devDependencies":{...},
"mocha": {
"require": [
"support/ts-node-register.js"
],
ts-node-register.js
const tsNode = require('ts-node');
tsNode.register({
files: true,
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/363580.html
標籤:javascript 节点.js 打字稿 单元测试 摩卡.js
上一篇:Quarkus單元測驗和屬性
