当我想用命令运行我的项目时npm run test,我得到以下错误.是什么造成的?
FAIL
? Test suite failed to run
SecurityError: localStorage is not available for opaque origins at Window.get localStorage [as localStorage] (node_modules/jsdom/lib/jsdom/browser/Window.js:257:15)
at Array.forEach (<anonymous>)
Run Code Online (Sandbox Code Playgroud) 我正在尝试为一个简单的React组件编写一个简单的测试,我想使用Jest来确认当我用酶模拟点击时已经调用了一个函数.根据Jest文档,我应该可以使用spyOn这个:spyOn.
然而,当我尝试这个时,我不断得到TypeError: Cannot read property '_isMockFunction' of undefined我的意思,我的间谍是未定义的.我的代码看起来像这样:
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
myClickFunc = () => {
console.log('clickity clickcty')
}
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<p className="App-intro" onClick={this.myClickFunc}>
To get started, edit <code>src/App.js</code> and save to reload.
</p>
</div>
);
}
}
export default App;
Run Code Online (Sandbox Code Playgroud)
在我的测试文件中:
import React …Run Code Online (Sandbox Code Playgroud) 我的package.json的设置如下
如果我从命令行运行npm test所有jest测试用例都正确执行.如果我直接从命令行使用命令jest,我收到此错误:
测试套件无法运行
Run Code Online (Sandbox Code Playgroud)TypeError: Path must be a string. Received undefined at assertPath (path.js:7:11) at Object.relative (path.js:538:5)
这发生在任何测试文件上.
知道什么可能是错的,以及如何解决它?
"scripts": {
"test": "standard && jest",
"format": "standard --fix",
"start": "webpack-dev-server --config webpack.config.dev.js",
"build": "webpack --config webpack.config.prod.js"
},
Run Code Online (Sandbox Code Playgroud) 这对我来说似乎有点奇怪.我正在尝试使用Jest测试实际(即真实网络)请求.
这些是经过测试的场景:
node终端的 标头测试相同的本地API <---这是有效的这种行为背后的原因是什么?什么是解决方案?
//This WORKS
test('testing no headers', () => {
return axios.get('http://api.fixer.io/latest')
.then( res => console.log(res) )
});
//This DOES NOT work
test('testing no headers', () => {
return axios.get('http://localhost:3000/users/4/profile',
{headers:{authorization:`Bearer ${mytoken}`}})
.then( res => console.log(res) )
});
//...
//Node Terminal
//This WORKS
> axios.get('http://localhost:3000/users/4/profile',
{headers:{authorization:`Bearer ${mytoken}`}})
.then( res => console.log(res) )
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用jest(v20.0.0)w /我的React Native应用程序(v0.42.0)但是当我运行时,yarn jest我收到以下错误:
yarn jest v0.27.5
$ jest
FAIL __tests__/routing/router-test.js
? Test suite failed to run
Cannot find module 'ReactNative' from 'react-native.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:179:17)
at Object.<anonymous> (node_modules/react-native/Libraries/react-native/react-native.js:188:25)
Run Code Online (Sandbox Code Playgroud)
这是jest我的一部分package.json
"jest": {
"testPathIgnorePatterns": [
"/node_modules/"
],
"transformIgnorePatterns": [
"node_modules/(?!react-native|react-native-geocoding)/"
],
"globals": {
"__DEV__": false
},
"collectCoverage": false
},
Run Code Online (Sandbox Code Playgroud)
这是失败的测试文件(我删除了除了之外的所有内容,import并且错误仍然存在).
import 'react-native';
import React from 'react';
describe('Router', () => {
});
Run Code Online (Sandbox Code Playgroud) 我试图使用Jest的新属性匹配器功能(自Jest 23.0.0)来匹配包含生成字段的对象数组.我尝试使用普通对象和匹配器定义,expect.arrayContaining并且expect.objectContaining像手动匹配时一样.目前有什么办法吗?
const sportsBallPeople = [
{
createdAt: new Date(),
name: 'That one famous guy from Cleveland'
},
{
createdAt: new Date(),
name: 'That tall guy'
}
];
expect(sportsBallPeople).toMatchSnapshot(<something goes here>);
Run Code Online (Sandbox Code Playgroud) 我在类型定义中prop-types遇到了这一行:
export type ValidationMap<T> = { [K in keyof T]-?: Validator<T[K]> };
Run Code Online (Sandbox Code Playgroud)
没有-它将是一个非常标准的部分映射类型,但我找不到它所谈论的文档中的任何地方-?.
谁能解释一下是什么-?意思?
我注意到React可以这样导入:
import * as React from 'react';
Run Code Online (Sandbox Code Playgroud)
...或像这样:
import React from 'react';
Run Code Online (Sandbox Code Playgroud)
第一个导入模块中的所有内容react(请参阅:导入整个模块的内容)
似乎这两种方法是不同的,并且根本上是不兼容的。
为什么它们都起作用?
请参考源代码并解释该机制...我有兴趣了解其工作原理。
更新资料
这与import *作为'react'的反应与import'react'的反应之间的区别不是重复的
ES6常规模块信息回答了该问题。
我在问使react模块像这样工作的机制。在这里,它似乎与源代码中的 “ hacky”导出机制有关,但尚不清楚如何将整个模块导入到默认的导出中React,以及如何将这两种方法都可以与转译JSX等一起使用。
我在我的组件中有这个选择器,其默认状态是'' (empty)字符串,但是当change事件被触发时,用户可以选择三个值中的任何一个,即6,12或24
it("feedform testing the selector feed frequency for value of 6, 12, 24 ", () => {
const addFeedForm = shallow(
<FeedForm
submitForm={() => {}}
setFeedData={() => {}}
formType="add"
feedsubmit={{
status: null,
error: {
formsubmitwarning: "",
feedname: "",
feedurl: "",
feedposttype: "",
feedfrequency: "",
feedpost: "",
feedhashtag: "",
formloginid: ""
}
}}
/>
);
expect(addFeedForm.state().feedfrequency).toEqual("");
addFeedForm.simulate("change");
expect(addFeedForm.state().feedfrequency).toEqual(6 || 12 || 24);
});
Run Code Online (Sandbox Code Playgroud)
现在在为此编写单元测试用例时,我很快通过jest文档找到三个值中的任何一个的匹配器,但发现没有匹配器执行此操作
我甚至尝试使用|| (or)operator in toEqual和toBematcher,但是你猜对它没有用,是否有办法使它工作或者我应该一起跳过测试.
注意:我正在使用酶和开玩笑
有谁知道如何测试nodejs中的mail-notifier模块?我想测试里面的逻辑n.on():
const notifier = require('mail-notifier');
const imap = {
user: 'mailId',
password: 'password',
host: 'host',
port: 'port',
tls: true,
tlsOptions: { rejectUnauthorized: false }
};
const n = notifier(imap);
n.on('mail', async function (mail) {
console.log('mail:', mail);
}).start();
Run Code Online (Sandbox Code Playgroud) javascript ×8
jestjs ×8
reactjs ×4
testing ×3
ecmascript-6 ×2
node.js ×2
react-native ×2
typescript ×2
axios ×1
babel ×1
enzyme ×1
npm ×1
unit-testing ×1
webpack ×1