无法开始业力测试 - 无法解析模块 [sqlite3] 等

Posted

技术标签:

【中文标题】无法开始业力测试 - 无法解析模块 [sqlite3] 等【英文标题】:Can't start karma test - unable to resolve module [sqlite3] and more 【发布时间】:2018-10-15 09:16:24 【问题描述】:

我的堆栈运行单元测试有问题: - 节点 - ts节点 - express.js - 续集 - mysql - 业力

当我运行 Karma 时,会遇到很多类似这样的错误:

04 05 2018 20:49:57.255:ERROR [karma]: Error: Unable to resolve module [sqlite3] from [/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/sequelize/lib/dialects/sqlite/connection-manager.js]

  "basedir": "/home/userName/Projects/commercial/projectName/projectName-cpf",
  "extensions": [
    ".js",
    ".json",
    ".ts",
    ".tsx"
  ],
  "moduleDirectory": [
    "node_modules"
  ],
  "modules": 
    "assert": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/assert/assert.js",
    "buffer": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/buffer/index.js",
    "child_process": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "cluster": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "console": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/console-browserify/index.js",
    "constants": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/constants-browserify/constants.json",
    "crypto": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/crypto-browserify/index.js",
    "dgram": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "dns": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "domain": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/domain-browser/source/index.js",
    "events": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/events/events.js",
    "fs": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "http": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/stream-http/index.js",
    "https": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/https-browserify/index.js",
    "module": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "net": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "os": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/os-browserify/browser.js",
    "path": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/path-browserify/index.js",
    "punycode": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/punycode/punycode.js",
    "querystring": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/querystring-es3/index.js",
    "readline": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "repl": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "stream": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/stream-browserify/index.js",
    "_stream_duplex": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/readable-stream/duplex.js",
    "_stream_passthrough": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/readable-stream/passthrough.js",
    "_stream_readable": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/readable-stream/readable.js",
    "_stream_transform": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/readable-stream/transform.js",
    "_stream_writable": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/readable-stream/writable.js",
    "string_decoder": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/string_decoder/lib/string_decoder.js",
    "sys": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/util/util.js",
    "timers": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/timers-browserify/main.js",
    "tls": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "tty": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/tty-browserify/index.js",
    "url": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/url/url.js",
    "util": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/util/util.js",
    "vm": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/vm-browserify/index.js",
    "zlib": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/browserify-zlib/lib/index.js",
    "_process": "/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/process/browser.js"
  ,
  "filename": "",
  "paths": []

Error: Cannot find module 'sqlite3' from '/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/sequelize/lib/dialects/sqlite'
    at /home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/karma-typescript/src/bundler/resolve/resolver.ts:194:27
    at /home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/browser-resolve/index.js:265:24
    at /home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/browser-resolve/node_modules/resolve/lib/async.js:46:14
    at process (/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/browser-resolve/node_modules/resolve/lib/async.js:173:43)
    at ondir (/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/browser-resolve/node_modules/resolve/lib/async.js:188:17)
    at load (/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/browser-resolve/node_modules/resolve/lib/async.js:69:43)
    at onex (/home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/browser-resolve/node_modules/resolve/lib/async.js:92:31)
    at /home/userName/Projects/commercial/projectName/projectName-cpf/node_modules/browser-resolve/node_modules/resolve/lib/async.js:22:47
    at FSReqWrap.oncomplete (fs.js:152:21)

上述错误与sqlite3有关,但我也有[pg]模块和[tedious]

我看到我应该为我在开发中使用的数据库系统安装一个依赖项,所以我使用npm install mysql2,但我无法启动任何 Karma 任务。

知道我应该在哪里搜索吗?我在任何 github 和堆栈问题上都没有看到这个问题,所以我认为这是在我的配置中。

下面我发布了我的karma.conf.jstsconfig.jsonpackage.json

karma.conf.js

module.exports = function (config) 
    config.set(
        frameworks: ["karma-typescript"],
        files: [
            "src/**/*.spec.ts",
            "src/**/*.ts"
        ],
        plugins: [
            require('karma-typescript'),
            require('karma-typescript-preprocessor'),
            require('karma-phantomjs-launcher'),
            require('karma-chrome-launcher'),
        ],
        preprocessors: 
            '**/*.ts': ['karma-typescript']
        ,
        karmaTypescriptConfig: 
            tsconfig: "./tsconfig.json"
        ,

        reporters: ["progress", "karma-typescript"],
        browsers: [
            // "PhantomJS"
            "Chrome"
        ],
        singleRun: false
    );
;

tsconfig.json


  "compileOnSave": true,
  "compilerOptions": 
    "removeComments": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es6",
    "sourceMap": true,
    "outDir": "dist",
    "watch": true,
    "skipLibCheck": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  ,
  "exclude": [
    "./node_modules/*"
  ]

还有我对package.json的依赖:

"dependencies": 
    "axios": "^0.18.0",
    "errorhandler": "^1.5.0",
    "express": "^4.16.3",
    "http-signature": "^1.2.0",
    "mysql2": "^1.5.3",
    "nconf": "^0.10.0",
    "nodemon": "^1.17.3",
    "request": "^2.85.0",
    "sequelize": "^4.37.6"
  ,
  "devDependencies": 
    "@types/chai": "^4.1.3",
    "@types/errorhandler": "0.0.32",
    "@types/express": "^4.11.1",
    "@types/mocha": "^5.2.0",
    "@types/nconf": "0.0.37",
    "@types/sinon": "^4.3.1",
    "chai": "^4.1.2",
    "gulp": "^4.0.0",
    "gulp-bump": "^3.1.1",
    "gulp-rimraf": "^0.2.2",
    "gulp-touch-fd": "^2.0.0",
    "gulp-typescript": "^4.0.2",
    "karma": "^2.0.2",
    "karma-chai": "^0.1.0",
    "karma-phantomjs-launcher": "^1.0.4",
    "karma-chrome-launcher": "^2.2.0",
    "karma-mocha": "^1.3.0",
    "karma-sinon": "^1.0.5",
    "karma-typescript": "^3.0.12",
    "karma-typescript-preprocessor": "^0.3.1",
    "mocha": "^5.1.1",
    "reflect-metadata": "^0.1.12",
    "sequelize-typescript": "^0.6.4",
    "sinon": "^5.0.3",
    "ts-node": "^6.0.1",
    "typedoc": "^0.11.1",
    "typescript": "^2.8.3"
  

【问题讨论】:

【参考方案1】:

虽然这很旧,但我也遇到了同样的问题。对我来说,问题是业力——它不是为了测试 node.js。 Karma 通常在 chrome-headless 浏览器中运行 javascript 代码,这是一个完全不同的环境。

解决方案是删除所有业力依赖项,然后使用 mocha 运行测试

"scripts": 
    "test": "mocha './src/**/*.test.js'"
  ,

https://blog.logrocket.com/unit-testing-node-js-applications-using-mocha-chai-and-sinon/

【讨论】:

以上是关于无法开始业力测试 - 无法解析模块 [sqlite3] 等的主要内容,如果未能解决你的问题,请参考以下文章

无法对我的 Angular 4 代码运行简单的业力测试

无法使用 chrome-headless 在 docker 中运行 angular-cli 业力测试

使用业力和 webpack 4 运行单元测试时无法读取未定义的属性“externalModuleIndicator”

詹金斯构建中无法识别业力

错误:无法解析ContactService的所有参数:(?)

使用gulp将业力测试分成可运行的块的最佳方法是什么?