Я проверяю Cypress в качестве основы тестирования.Однако у меня возникают проблемы с выполнением базовой настройки для тестирования некоторого кода приложения, например некоторых служебных функций.Я перешел по ссылке здесь https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/unit-testing__application-code
Вот код, который я хотел бы проверить (COPIED FROM EXAMPLE).
src / lib / math:
export default {
add: (a, b) => {
return a + b
},
subtract: (a, b) => {
return a - b
},
divide: (a, b) => {
return a / b
},
multiply: (a, b) => {
return a * b
}
}
Вот мой тест: cypress / интеграции / unit.spec.js:
import {add} from '../../src/lib/math'; <---I'd like to use aliases!
context('math.js', function() {
if('can add numbers', function() {
expect(add(1,2)).to.eq(3)
})
})
Но когда я запускаю его, я получаю эту ошибку:
Oops...we found an error preparing this test file:
/home/terry/myProjects/tester/cypress/integration/unit.spec.js
This occurred while Cypress was compiling and bundling your test code. This is usually caused by:
A missing file or dependency
A syntax error in the file or one of its dependencies
Fix the error in your code and re-run your tests.
Кажется, не импортируетфайл как в примере.Какой шаг настройки я пропустил?Я использую веб-пакет.
Кроме того, возможно ли использовать функцию разрешения псевдонимов модуля webpack в интеграционных тестах?Чтобы избежать "../../../" и т. Д.?
РЕДАКТИРОВАТЬ: Для полноты вот мои настройки babel, webpack и package.json:
.babelrc
{
"presets": [
"@babel/preset-react",
"@babel/preset-env"
],
"plugins": [
"@babel/plugin-proposal-object-rest-spread",
"babel-plugin-webpack-alias"
]
}
webpack.config.js
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
inject: 'body'
})
module.exports = {
entry: './src/index.js',
output: {
path: path.join(__dirname, 'build'),
filename: 'app.bundle.js'
},
devtool: 'inline-source-map',
devServer: {
contentBase: './dist'
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.html$/,
use: [
{
loader: "html-loader",
options: { minimize: true }
}
]
},
{
test: /\.(scss|sass|css)$/,
use: [
MiniCssExtractPlugin.loader,
{ loader: 'css-loader' },
{ loader: 'postcss-loader',
options: {
plugins: () => [autoprefixer({ grid: false})]
}
},
{
loader: 'fast-sass-loader',
options: {
includePaths: [ path.resolve(__dirname, 'src'), path.resolve(__dirname, 'src','styles') ,'./node_modules', '/node_modules/materialize-css/sass/components'],
sourceMap: true
}
}
]
},
{
test: /\.(jpg|png)$/,
loader: 'url-loader',
options: {
limit: 8192 // inline base64 URLs for <=8k images, direct URLs for the rest
},
},
{
test: /\.svg/,
use: {
loader: 'svg-url-loader',
options: {}
}
},
{
test: /\.mp3$/,
loader: 'file-loader'
},
]
},
resolve: {
alias: {
components: path.resolve(__dirname, 'src', 'components'),
routes: path.resolve(__dirname, 'src', 'routes'),
styles: path.resolve(__dirname, 'src', 'styles'),
lib: path.resolve(__dirname, 'src', 'lib')
},
},
devtool: 'source-map',
plugins: [HtmlWebpackPluginConfig]
}
package.json
{
"name": "react-cypress-boilerplate",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack",
"watch": "webpack --watch --mode development --port 8080",
"dev": "webpack-dev-server --open",
"test": "cypress open"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.18.0",
"concurrently": "^4.1.0",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react-router-dom": "^5.0.0"
},
"devDependencies": {
"@babel/core": "^7.4.0",
"@babel/plugin-proposal-object-rest-spread": "^7.4.0",
"@babel/preset-env": "^7.4.2",
"@babel/preset-react": "^7.0.0",
"@cypress/webpack-preprocessor": "^4.0.3",
"autoprefixer": "^9.5.0",
"babel-loader": "^8.0.5",
"babel-plugin-webpack-alias": "^2.1.2",
"css-loader": "^2.1.1",
"cypress": "^3.2.0",
"fast-sass-loader": "^1.4.7",
"file-loader": "^3.0.1",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"mini-css-extract-plugin": "^0.5.0",
"postcss-loader": "^3.0.0",
"style-loader": "^0.23.1",
"svg-url-loader": "^2.3.2",
"url-loader": "^1.1.2",
"webpack": "^4.29.6",
"webpack-cli": "^3.3.0",
"webpack-dev-server": "^3.2.1"
}
}
РЕДАКТИРОВАТЬ: Вот файл плагинов для Cypress ниже.Я пробовал как с, так и без него.
plugins / index.js
const webpack = require('@cypress/webpack-preprocessor')
module.exports = (on, config) => {
const options = {
// send in the options from your webpack.config.js, so it works the same
// as your app's code
webpackOptions: require('../../webpack.config'),
watchOptions: {},
}
// `config` is the resolved Cypress config
on('file:preprocessor', webpack(options));
}