我正在尝试使用Jasmine设置Karma以测试反应组件。我使用的业力配置是
/* eslint-disable no-undef */
var webpackConf = require('./node_modules/laravel-mix/setup/webpack.config.js');
delete webpackConf.entry
module.exports = function(config) {
config.set({
basePath: "./resources/js/",
files: [
{ pattern: 'spec/**/*.js', watched: true, served: true, included: true }
],
exclude: [],
autoWatch: false,
singleRun: false,
failOnEmptyTestSuite: false,
logLevel: config.LOG_WARN,
frameworks: ["jasmine"],
browsers: [
'PhantomJS'/* 'Chrome','Firefox','Edge','ChromeCanary','Opera','IE','Safari'*/
],
reporters: ["mocha", "kjhtml" /*,'dots','progress','spec'*/],
listenAddress: "0.0.0.0",
hostname: "localhost",
port: 9876,
retryLimit: 2,
browserDisconnectTimeout: 5000,
browserNoActivityTimeout: 10000,
client: {
//capture all console output and pipe it to the terminal, true is default
captureConsole: true,
//if true, Karma clears the context window upon the completion of running the tests, true is default
clearContext: false,
//run the tests on the same window as the client, without using iframe or a new window, false is default
runInParent: false,
//true: runs the tests inside an iFrame; false: runs the tests in a new window, true is default
useIframe: true,
jasmine: {
//tells jasmine to run specs in semi random order, false is default
random: false
}
},
/* karma-webpack config
pass your webpack configuration for karma
add `babel-loader` to the webpack configuration to make
the ES6+ code in the test files readable to the browser
eg. import, export keywords */
webpack: webpackConf,
webpackMiddleware: {
//turn off webpack bash output when run the tests
noInfo: true,
stats: "errors-only"
},
preprocessors: {
//add webpack as preprocessor to support require() in test-suits .js files
"spec/**/*.js": ["webpack"],
"admin.js": ["webpack", "sourcemap"],
"views/**/*.js": ["webpack", "sourcemap"],
"components/**/*.js": ["webpack", "sourcemap"],
"containers/**/*.js": ["webpack", "sourcemap"],
},
/*karma-mocha-reporter config*/
mochaReporter: {
output: "noFailures" //full, autowatch, minimal
},
colors: true,
plugins: [
'karma-webpack',
'karma-jasmine',
'karma-sourcemap-loader',
'karma-phantomjs-launcher',
],
});
};
这是目录结构
anadi@MacAnadi js % pwd
/Users/anadi/Code/github/website/adminpanel/resources/js
anadi@MacAnadi js % ls -ltr
views
app.js
containers
__mocks__
components
bootstrap.js
admin.js
tests
spec
并且所有Jasmine Tests都位于spec
目录中。然后,我将int-test
脚本添加到package.json
"int-test": "karma start --coverage",
并且我为其中一个组件添加了一个简单的测试
import React from 'react'
import { mount, configure } from 'enzyme'
import jasmineEnzyme from 'jasmine-enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
import Editor from "../../views/Blogs/Editor/UpdateEditor";
describe("Blog Update Editor", () => {
beforeEach(() => {
jasmineEnzyme();
});
it("renders without errors", () => {
expect(mount(<Editor match={{ params: { slug: "a-title" }, isExact: true, path: "", url: "" }}/>)).toMatchSnapshot();
});
});
但是当我运行测试时,它们都不执行,此后控制台上也没有消息
anadi@MacAnadi adminpanel % npm run int-test
> @ int-test /Users/anadi/Code/github/website/adminpanel
> karma start --coverage
11 03 2020 17:30:07.265:WARN [karma]: No captured browser, open http://localhost:9876/
11 03 2020 17:30:07.315:INFO [karma-server]: Karma v4.4.1 server started at http://0.0.0.0:9876/
我为此目的安装的NPM模块是:
"enzyme": "3.11.0",
"enzyme-adapter-react-16": "1.15.2",
"jasmine-enzyme": "7.1.2",
"webpack-cli": "3.3.11",
"webpack-karma-jasmine": "3.0.8"