gpt4 book ai didi

node.js - Karma/Grunt 未加载 Chai 变量

转载 作者:太空宇宙 更新时间:2023-11-03 22:44:12 27 4
gpt4 key购买 nike

我无法让 chai 在 karma/grunt 设置中与 chai-as-promised 一起工作。

当我运行 grunt testkarma test/karma.conf.js 时,出现以下错误:

PhantomJS 1.9.8 (Mac OS X 0.0.0) ERROR
ReferenceError: Can't find variable: chai
at /Users/isaac/Sites/ChessGame/node_modules/chai-as-promised/lib/chai-as-promised.js:19

这看起来与 this one 类似。 ,但我遇到了 chai-as-promised 的问题,而不是 sinon-chai 的问题。我已验证 node_modules/chai/chai.js 存在。

这是我的 package.json 文件:

{
"name": "chessgame",
"private": true,
"devDependencies": {
"angular-mocks": "^1.4.3",
"chai": "^3.2.0",
"chai-as-promised": "^5.1.0",
"chai-things": "^0.2.0",
"grunt": "^0.4.5",
"grunt-angular-templates": "^0.5.7",
"grunt-autoprefixer": "^2.0.0",
"grunt-concurrent": "^1.0.0",
"grunt-contrib-clean": "^0.6.0",
"grunt-contrib-compass": "^1.0.0",
"grunt-contrib-concat": "^0.5.0",
"grunt-contrib-connect": "^0.9.0",
"grunt-contrib-copy": "^0.7.0",
"grunt-contrib-cssmin": "^0.12.0",
"grunt-contrib-htmlmin": "^0.4.0",
"grunt-contrib-imagemin": "^0.9.2",
"grunt-contrib-jshint": "^0.11.0",
"grunt-contrib-uglify": "^0.7.0",
"grunt-contrib-watch": "^0.6.1",
"grunt-filerev": "^2.1.2",
"grunt-google-cdn": "^0.4.3",
"grunt-karma": "*",
"grunt-mocha": "^0.4.13",
"grunt-newer": "^1.1.0",
"grunt-ng-annotate": "^0.9.2",
"grunt-svgmin": "^2.0.0",
"grunt-usemin": "^3.0.0",
"grunt-wiredep": "^2.0.0",
"jit-grunt": "^0.9.1",
"jshint-stylish": "^1.0.0",
"karma-browserify": "^4.2.1",
"karma-chai": "^0.1.0",
"karma-chai-as-promised": "^0.1.2",
"karma-chai-things": "^0.1.4",
"karma-coverage": "^0.4.2",
"karma-jasmine": "*",
"karma-mocha": "^0.2.0",
"karma-mocha-reporter": "^1.0.3",
"karma-phantomjs-launcher": "*",
"karma-requirejs": "^0.2.2",
"karma-sinon": "^1.0.4",
"karma-sinon-chai": "^1.0.0",
"mocha": "^2.2.5",
"mockfirebase": "^0.11.0",
"requirejs": "^2.1.19",
"sinon": "^1.15.4",
"sinon-chai": "^2.8.0",
"time-grunt": "^1.0.0"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "grunt test"
}
}

以及 test/karma.conf.js 文件:

module.exports = function(config) {
'use strict';

config.set({
autoWatch: true,
basePath: '../',
frameworks: [
"mocha",
"chai",
"sinon-chai",
"chai-as-promised"
],
files: [
// bower:js
'bower_components/jquery/dist/jquery.js',
'bower_components/angular/angular.js',
'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap.js',
'bower_components/angular-animate/angular-animate.js',
'bower_components/angular-cookies/angular-cookies.js',
'bower_components/angular-messages/angular-messages.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-sanitize/angular-sanitize.js',
'bower_components/angular-touch/angular-touch.js',
'bower_components/firebase/firebase.js',
'bower_components/angularfire/dist/angularfire.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/mocha/mocha.js',
'bower_components/chai/chai.js',
'bower_components/mockfirebase/browser/mockfirebase.js',
// endbower
'node_modules/sinon/pkg/sinon.js',
'node_modules/chai/chai.js',
'node_modules/sinon-chai/lib/sinon-chai.js',
"app/scripts/**/*.js",
"test/mock/**/*.js",
"test/spec/**/*.js"
],
exclude: [
],
port: 8080,
browsers: [
"PhantomJS"
],
reporters: ['mocha','coverage'], //
preprocessors: {
'app/scripts/controllers/*.js': ['coverage'],
'app/scripts/directives/*.js': ['coverage'],
'app/scripts/app.js': ['coverage']
},
coverageReporter: {
type : 'text',
dir: 'test-results/coverage/'
},
singleRun: false,
colors: true,
logLevel: config.LOG_INFO,
});
};

最佳答案

不幸的是,这是 karma 的一个怪癖。 frameworks 键必须采用相反的顺序,因此您必须在加载 chai 本身之前加载每个 chai 插件。现在你有

 frameworks: [
"mocha",
"chai",
"sinon-chai",
"chai-as-promised"
],

如果你将其更改为

 frameworks: [
"mocha",
"sinon-chai",
"chai-as-promised",
"chai"
],

那么一切都会顺利进行!一些karma-chai plugins document this ,尽管它肯定会更加突出

关于node.js - Karma/Grunt 未加载 Chai 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31825745/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com