javascript - afterAll ReferenceError 中抛出错误:未定义模块,nodejs 中的 Karma/Jasmine 测试错误
问题描述
我正在尝试使用 karma 和 jasmine 作为框架为 m nodejs 简单应用程序编写测试。我正在使用 karma-coverage 作为预处理器。这是项目的结构:
包.json
{
"name": "tests",
"version": "1.0.0",
"description": "tests with karma in jasmine framework",
"directories": {
"test": "test"
},
"scripts": {
"test": "karma start karma.conf.js"
},
"author": "",
"license": "MIT",
"devDependencies": {
"jasmine-core": "^3.4.0",
"karma": "^4.2.0",
"karma-chrome-launcher": "^3.1.0",
"karma-coverage": "^1.1.2",
"karma-jasmine": "^2.0.1"
}
}
业力.conf.js
// Karma configuration
// Generated on Sat Aug 17 2019 09:37:53 GMT+0500 (Uzbekistan Standard Time)
module.exports = function (config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'js/*.js',
'test/*.test.js'
],
// list of files / patterns to exclude
exclude: [],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'test/*.test.js': ['coverage']
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress', 'coverage'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,
// optionally, configure the reporter
coverageReporter: {
type: 'html',
dir: 'coverage/'
}
})
}
每当我运行时npm test
,我都会收到以下错误:
自定义 lodash.js
class CustomLodash {
compact(array) {
let newArray = [];
for (let i = 0; i < array.length; i++) {
if (!array[i] || array[i] === undefined || array[i] === null) {
continue;
}
this.push(array[i])(newArray);
}
return newArray;
}
}
let _ = new CustomLodash;
module.exports = {
compact: _.compact
};
自定义 lodash.test.js
describe("CustomLodash", function () {
let utils;
//This will be called before running each spec
beforeEach(function () {
console.log('before each');
utils = new CustomLodash();
});
describe("when calc is used to peform basic math operations", function () {
it("creates an array with all falsey values removed", function () {
// expect(utils.compact([0, 1, false, 2, '', 3])).toBe([1, 2, 3]);
let compact = utils.compact([0, 1, false, 2, '', 3]);
console.log('before each in it is: ', compact);
expect(compact).toEqual([1, 2, 3]);
//console.log('is defined ', expect(compact).toEqual([1, 2, 3]));
});
})
});
如您所见,console.log
正在给出输出,这意味着正在读取文件。但是,当我打电话toBe()
或者toEqual()
我可以看到输出是未定义的(console.log
也检查了这个)。非常感谢任何帮助。我搜索了很多答案和相关问题,但无法解决这个问题。
解决方案
我想分享我对这个案例的解决方案。
添加 browserify 对我有用。另外,我添加了 watchify 来监视文件以进行增量编译。
Browserify 的诞生是为了在浏览器中制作你的 Node 代码。迄今为止,它仅支持 commons 的 node 风格(包括 JSON 支持),并为许多 node 核心模块提供内置的垫片。其他一切都是不同的包装。
以下是package.json
添加包以在浏览器和 Node 中运行我的 ES6 代码后的样子:
"dependencies": {
"browserify": "^16.2.3",
"jasmine-core": "^3.2.1",
"karma": "^4.2.0",
"karma-chrome-launcher": "^3.1.0",
"karma-browserify": "^5.3.0",
"karma-commonjs": "^1.0.0",
"karma-coverage": "^1.1.2",
"karma-jasmine": "^2.0.1"
"watchify": "^3.11.0"
}
不要忘记在karma.config.js
配置中添加 browserify,如下所示:
将其添加到您的框架列表中
将其添加到您的预处理器列表中。
例子:
preprocessors: {
'src/**/*.js': ['coverage'],
'js/**/*.js': ['browserify'],
'test/**/*.[sS]pec.js': ['browserify']
}
使用 ES6 可以通过构建来解决此类问题webpack
,但browserify
更可能以最少的配置工作,我选择在这种情况下使用它作为解决方案。希望它会帮助某人。
推荐阅读
- algorithm - Minimax 递归备份树
- ready-api - 有什么方法可以在 ReadyAPI 测试用例期间检索数据库生成的创建记录的 id?
- python - 计算变化时如何考虑不同的日期
- c# - ASP.NET FriendlyURLS 路由,url 中间有值
- android - 访问回收站/列表视图中的所有(包括不可见的)视图
- java - 投掷和投掷方法相同
- c# - 使用 Castle.Windsor 从 web.config 注入值
- django - df.to_html() 方法在网页上显示实际的 html,而不是它应该显示的表示(Django 网站)
- java - 即使条件为假也执行 if 语句
- python - 受约束的 TypeVar 和 Union 有什么区别?