我一直在尝试为我的React项目设置一个测试环境,并且在导入CSS文件时遇到了Unexpected标记的问题.我将moduleNameMapper添加到package.json来修复它,现在我正面临这个问题.有谁知道我该怎么做才能解决这个问题?
> myProject@0.0.1 test C:\Users\admin\Documents\myApp
> jest
FAIL _tests_\Home.test.js
? Test suite failed to run
Configuration error:
Could not locate module react-datepicker/dist/react-datepicker-cssmodules.css (mapped as identity-obj-proxy)
Please check:
"moduleNameMapper": {
"/^.+\.(css|less)$/": "identity-obj-proxy"
},
"resolver": undefined
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.894s
Ran all test suites.
npm ERR! Test failed. See above for more details.
Run Code Online (Sandbox Code Playgroud)
package.json的开头部分
"jest": {
"transform": {
"^.+\\.jsx?$": "babel-jest"
},
"moduleNameMapper": {
"\\.(s?css|less)$": "identity-obj-proxy"
}
},
Run Code Online (Sandbox Code Playgroud) 我很难学习如何使用笑话.我遇到的所有教程都教你如何测试渲染到dom的脚本,例如<App />有或没有快照.其他教程讨论了如何使用输入模拟测试.但我似乎无法找到解释清楚的教程并提供我可以使用的示例.
例如,下面的脚本我有一个关于如何测试渲染部分的想法,但我不知道如何测试redux或其余的函数.
任何人都可以举例说明如何测试下面的脚本,我可以用它作为我需要在项目中测试的其余文件的参考吗?
import React, { Component } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import CustomSearch from '../Components/CustomSearch';
import CustomToolBar from '../Components/CustomToolBar';
import Table from '../Components/Table';
import InsertButton from '../Components/InsertButton';
import UserForm from './UserForm ';
import { fetchUsers, deleteUser } from '../../actions/users';
import setModal from '../../actions/modal';
import TableColumns from '../../constants/data/TableColumns';
class Users extends Component {
constructor(props) {
super(props);
this.onInsert = this.onInsert.bind(this);
this.onDelete = this.onDelete.bind(this);
this.onEdit = this.onEdit.bind(this);
this.props.fetchUsers({ accountId: this.props.userData.account.id, token: …Run Code Online (Sandbox Code Playgroud) 我一直在努力挣扎,我不知道该怎么做。在这个问题上似乎有很多问题,我尝试了其中的每一个,但在开玩笑方面似乎仍然有问题。导入似乎根本不起作用。因此,请任何人可以协助我解决这个问题。
错误
C:\Users\admin\Documents\my-app\_tests_\main.spec.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React from 'react';
^^^^^^
SyntaxError: Unexpected token import
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
at process._tickCallback (internal/process/next_tick.js:109:7)
Run Code Online (Sandbox Code Playgroud)
main.spec.js
import React from 'react';
import { shallow, mount } from 'enzyme';
import renderer from 'react-test-renderer';
import { Home } from '../src/Home';
test('Basic Test', () => {
expect(2 + 2).toBe(4);
});
Run Code Online (Sandbox Code Playgroud)
运行此命令将产生错误,但是如果我注释掉导入行,它将起作用。
Bablerc
{
"plugins": [
"transform-async-functions",
"transform-object-rest-spread",
"transform-regenerator"
],
"presets": [
"es2015",
"react"
]
}
Run Code Online (Sandbox Code Playgroud)
我尝试添加“ env”:{“ test”:{“ plugins”:[“ transform-es2015-modules-commonjs”]}},但是它什么也没做,所以我暂时将其取出。
package.json
{
"name": "my-app",
"version": "0.0.1",
"description": "app",
"scripts": {
"start": …Run Code Online (Sandbox Code Playgroud) 我已经在Webpack开发服务器上对我的简单登录网站进行了完整的编码和功能。如何使用外部IP显示它?我已经通过端口9000将端口转发了我的路由器,但是当我尝试www.myIp:portnumber时,它不起作用。以下是我的webpack开发服务器的webpack配置。
//Webpack config
module.exports = {
entry: './app/main.js',
output: {
path: './app',
filename: 'bundle.js'
},
devServer: {
inline: true,
contentBase: './app',
port: 9000
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel'
}
]
}
}
Run Code Online (Sandbox Code Playgroud) node.js的新手,并在下面的链接中遵循基本教程. https://www.tutorialspoint.com/nodejs/nodejs_web_module.htm
var http = require('http');
var fs = require('fs');
var url = require('url');
// Create a server
http.createServer( function (request, response) {
// Parse the request containing file name
var pathname = url.parse(request.url).pathname;
// Print the name of the file for which request is made.
console.log("Request for " + pathname + " received.");
// Read the requested file content from file system
fs.readFile(pathname.substr(1), function (err, data) {
if (err) {
console.log(err);
// HTTP Status: 404 : NOT FOUND
// …Run Code Online (Sandbox Code Playgroud) 我正在使用基本的访存来从快速服务器获取数据,该服务器从数据库查询数据。所以我想做一个登录系统,我想通过获取请求从输入字段中发送用户名/密码。因此我可以执行逻辑检查,以查看密码是否与服务器中的数据匹配。并回应结果。是否可以进行传递参数的提取?
以下更新的代码:
let fetchData = {
method: "post",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: this.state.user,
password: this.state.password
})
}
var fromServer = fetch('http://localhost:3000/', fetchData)
.then(function(response){
if( !response.ok){
throw Error (response.statusText);
}
return response.json();
})
.then(function(response) {
console.log(response);
})
.catch(error => console.log("there was an error --> " + error));
Run Code Online (Sandbox Code Playgroud)
快速功能
app.post('/', function(req, res){
var uname = req.body.username;
var pw = req.body.password;
console.log(uname);
console.log(pw);
});
Run Code Online (Sandbox Code Playgroud)