我在这里找到的最接近的东西是karma-typescript cannot find module,但是那是指因果报应类型找不到源文件而不是依赖项的问题。它的解决方案还是行不通的。
我正在尝试使用Karma,Mocha和Chai为TypeScript项目设置单元测试。我决定在npm中使用karma-typescript包来设置测试,因此不必先编译为Javascript。当我运行“ karma start karma.conf.js”时,它将运行一段时间,然后返回:
02 03 2019 14:08:28.791:DEBUG [coverage.karma-typescript]: Initializing
02 03 2019 14:08:28.799:DEBUG [compiler.karma-typescript]: Setting up deferred project compilation
02 03 2019 14:08:28.800:DEBUG [coverage.karma-typescript]: Configuring coverage preprocessor
02 03 2019 14:08:28.805:ERROR [karma-server]: Server start failed on port 9876: Error: Cannot find module 'readable-stream/duplex.js'
npm ERR! Test failed. See above for more details.
我尝试过手动添加可读流,即使可读流在karma-typescript的package.json中,但似乎没有什么不同。我已经这样设置了karma.conf.js:
const puppeteer = require('puppeteer');
process.env.CHROME_BIN = puppeteer.executablePath();
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['mocha', 'chai', 'karma-typescript'],
karmaTypescriptConfig: {
compilerOptions: {
module: "commonjs"
},
tsconfig: "./src/ts/tsconfig.json"
},
files: [
'src/**/*.ts'
],
exclude: [
],
preprocessors: {
"**/*.ts": ["karma-typescript"]
},
reporters: ['progress', 'karma-typescript'],
port: 9876,
colors: true,
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_DEBUG,
autoWatch: false,
browsers: ['ChromeHeadless'],
singleRun: true,
concurrency: Infinity
})
}
我的tsconfig.json看起来像这样,并与所有.ts源文件(src / ts /)存储在同一文件夹中:
{
"compilerOptions": {
"target": "es2018",
"outDir": "../js",
"rootDir": "./",
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true
}
}