小编Ric*_* II的帖子

使用jest测试react-native app时,未定义XMLHttpRequest

我正在尝试使用jest(集成测试)在基于反应本机的应用程序中测试apiwrapper.当我在iOs模拟器中运行时,一切运行正常,但它不会正确运行我的开玩笑测试 - 我总是得到:

ReferenceError: XMLHttpRequest is not defined
Run Code Online (Sandbox Code Playgroud)

当我尝试使用我的api包装器运行测试时,例如:

it('Login successful with correct data', () => {
  let api = Api.getInstance();
  return api.login("test", "testpass")
         .then(result => expect(result).toEqual('login_successful'));
});
Run Code Online (Sandbox Code Playgroud)

我试图在这里测试的api类确实使用了fetch api(不是vanilla xhr).我假设它的某些东西与试图嘲笑某些东西有关,但还没有找到让它工作的方法.

提前致谢.

xmlhttprequest jestjs react-native babel-jest

8
推荐指数
1
解决办法
2791
查看次数

从git空白检查中排除Jest快照

我正在使用git提交修剪空白git diff-index --check --cached HEAD --.我想使用快照添加Jest测试,但快照文件包含空格,如果删除它,我的测试将始终失败.所以我想*.js.snap从空白检查中排除文件.如何告诉git从中排除*.js.snap(或替代**/__snapshots/*)文件git diff-index?我在OSX上使用bash.

与此同时,我正在通过将提交挂钩更改为交互式来解决此问题:

# If there are whitespace errors, print the offending file names and fail.
git diff-index --check --cached HEAD --
if [ $? -ne 0 ]; then
    # Allows us to read user input below, assigns stdin to keyboard
    exec < /dev/tty

    while true; do
        echo "Your commit introduces trailing whitespace.  Are you sure you want to commit? y/n"
        read yn
        case $yn …
Run Code Online (Sandbox Code Playgroud)

git whitespace githooks jestjs

8
推荐指数
1
解决办法
1081
查看次数

使用jest 15.1.1编码覆盖率"未知"

当我尝试包含所有项目源代码以获得更合理的代码覆盖率时,我最终得到了

----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files |  Unknown |  Unknown |  Unknown |  Unknown |                |
----------|----------|----------|----------|----------|----------------|
Run Code Online (Sandbox Code Playgroud)

我的配置包含以下内容:

"collectCoverageFrom": [
    "<rootDir>/app_modules/",
    "<rootDir>/src/"
],    
Run Code Online (Sandbox Code Playgroud)

我也试过没有尾随/,**/*.js只有一个尾随*.js所有无济于事.

基于该--debug选项,路径扩展到我想要从中收集覆盖信息的路径(不是问题)

那么获得更准确的覆盖信息的魔力是什么?

我能找到的最好的文档来自这个Github PR:https: //github.com/facebook/jest/pull/1349/files


我最终做了:

"collectCoverageFrom": [
    "**/*.js",
    "!webpack.config.js"
],
Run Code Online (Sandbox Code Playgroud)

这只是工作,因为这是默认配置的一部分

"testPathIgnorePatterns": [
    "/node_modules/"
],
Run Code Online (Sandbox Code Playgroud)

它确实为测试运行增加了大量时间.

javascript jestjs babel-jest

7
推荐指数
1
解决办法
3763
查看次数

React Native 玩笑不断抛出 -TypeError: Component is not a function 错误

我目前在尝试使用 jest 添加简单的单元测试时遇到问题。它不断抛出 TypeError:Component is not a function 错误,我无法修复它。有人以前遇到过同样的错误,或者有人对如何修复它有任何想法吗?真的很感激!

\n\n

我从 jest 得到的测试结果如下:

\n\n
Using Jest CLI v11.0.2, jasmine2, babel-jest\nRunning 1 test suite...Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components).\n FAIL  app/views/connections/__tests__/Connections-test.js (3.217s)\nConnections\n  \xe2\x9c\x95 it should return View\n\nConnections \xe2\x80\xba it should return View\n  - TypeError: Component is not a function\n        at ShallowComponentWrapper._constructComponentWithoutOwner (node_modules/react/lib/ReactCompositeComponent.js:250:8)\n        at ShallowComponentWrapper.mountComponent (node_modules/react/lib/ReactCompositeComponent.js:159:15)\n        at ShallowComponentWrapper.wrapper [as mountComponent] (node_modules/react/lib/ReactPerf.js:66:13)\n        at ReactShallowRenderer._render …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing jestjs react-native

5
推荐指数
2
解决办法
8794
查看次数

模拟 axios 时无法读取 JEST 中未定义的属性“baseURL”

我尝试在 React 组件中使用 AXIOS 进行 ajax 调用,如下所示,

    let that = this;
    this.serverRequest = axios({
        url: '/login',
        method: 'post',
        data: JSON.stringify({ userName: "testing", passWord: "passWord" }),
        headers: {
            "Content-Type": 'application/json; charset=utf-8'
        }
    }).then(function (successData) {                    
            localStorage.setItem('userData', JSON.stringify(successData.data));
            this.setState({
                successMsge: "Valid credentials"
            });
        }).catch(function(errorData){                   
            this.setState({
                errorMessage: errorData.data.message,
                errorDisplay: true
            });
        }.bind(that));
Run Code Online (Sandbox Code Playgroud)

React 组件工作完美,当我尝试在 JEST 中模拟上述组件时,会显示错误。

我附上了下面的 JEST 代码,

expect(axios).toBeCalledWith({
    type: 'POST',
    baseURL: '/login',
    data: JSON.stringify(dataParams),
    headers: {
        "Content-Type": 'application/json; charset=utf-8'
    }
});
Run Code Online (Sandbox Code Playgroud)

当我运行 npm jest 时,我收到上述组件的以下错误

  - TypeError: Cannot read property 'baseURL' …
Run Code Online (Sandbox Code Playgroud)

reactjs jestjs axios

5
推荐指数
0
解决办法
1301
查看次数

离子应用程序的Jest单元测试

我试图用Jest而不是传统的Jasmine和Karma来测试我的Ionic应用程序.我可以运行简单的测试

expect(1).toEqual(1)
Run Code Online (Sandbox Code Playgroud)

并且测试通过没有问题.

我遇到的问题是测试服务.我有类似的东西

expect(CalculatorService).toBeDefined();
Run Code Online (Sandbox Code Playgroud)

并且测试结果将传递,表明服务已定义.但是当我试图调用CalculatorService的方法时

expect(CalculatorService.add(1,1).toEqual(2)
Run Code Online (Sandbox Code Playgroud)

结果是

"TypeError: CalculatorService.add is not a function"
Run Code Online (Sandbox Code Playgroud)

我知道Jasmine&Karma需要注入服务.但我不知道Jest的等价物是什么.

有人可以解释一下吗?

javascript unit-testing angularjs ionic-framework jestjs

5
推荐指数
0
解决办法
289
查看次数

如何在 Jest 中模拟 Image 的 propType(React Native)

我在使用react-native和react-native-router-flux的玩笑测试中出现以下错误

Runtime Error
  - TypeError: Cannot read property 'source' of undefined
        at Object.<anonymous> (node_modules/react-native-router-flux/src/NavBar.js:155:45)
        at Object.<anonymous> (node_modules/react-native-router-flux/src/DefaultRenderer.js:21:13)
        at Object.<anonymous> (node_modules/react-native-router-flux/index.js:3:22)
Run Code Online (Sandbox Code Playgroud)

这是导致麻烦的行

backButtonImage:Image.propTypes.source,

所以我猜 Image.propTypes 定义不正确

我怎样才能解决这个问题?

snapshot mocking jestjs react-native

5
推荐指数
0
解决办法
675
查看次数

Jest 忽略链接模块中的对等依赖项

我正在开发一个 React Web 应用程序,它使用我们作为 npm 模块包含的组件库。因为我同时在处理库和 webapp,所以我将库链接为 npm 中的模块。

目录设置如下:

~/Development/
|
|-- myWebapp/
|   |-- package.json # (react included here as dependency)
|   |-- src/
|   |-- node_modules/
|       |-- jest-cli/
|       |-- react/
|       |-- myLibrary/ -> /usr/local/lib/node_modules/myLibrary
|
|-- myLibrary/       # (linked to /usr/local/lib/node_modules by npm link)
|   |-- package.json
|   |-- src/
|   |-- node_modules/
|       |-- react/   # (included as a devDependency and peerDependency)
Run Code Online (Sandbox Code Playgroud)

我正在 Jest (0.8.2) 中编写我的测试。

我正在尝试将 Jest 配置为在为 myWebapp 运行测试时不从 myLibrary/node_modules/react 加载 …

symlink node.js npm reactjs jestjs

5
推荐指数
1
解决办法
2100
查看次数

开玩笑无法加载下划线

收到错误TypeError:无法读取未定义的属性“ _”

从'下划线'导入_处;

运行Jest-React测试用例时。

underscore.js jestjs

5
推荐指数
2
解决办法
1155
查看次数

在Jest中进行每次测试之前运行全局测试设置

我正在尝试在Jest测试套件中的每个测试之前执行测试设置功能。我知道我可以beforeEach在单个测试文件中完成此操作,但是我想在所有测试文件中全局执行此操作,不必显式修改每个单个文件。

我查看了jest的配置文件,发现有两个配置可能起作用:globalSetupsetupFiles,但是它们似乎只能运行一次(在测试运行的开始)。就像我说的那样,我需要it在测试文件中的“每个” 块之前运行它。

这可能吗?

unit-testing jestjs

5
推荐指数
3
解决办法
4390
查看次数