小编Can*_*nta的帖子

如何使用酶测试 onClick 的 redux 操作

我正在尝试用酶测试以下功能。

renderLoginLink(caption) {
return (
  <a href="#" id="lnkAuthenticateWithGoogle" onClick={() => this.props.actions.authenticateWithGoogle()}>
    {caption}
  </a>
);
}
Run Code Online (Sandbox Code Playgroud)

我用它来模拟点击。

let container, store
const mockStore = configureMockStore();
store = mockStore(initialState);
container = mount(<Provider store={store}><Login {...initialState} /></Provider>)
container.find("a#lnkAuthenticateWithGoogle").simulate("click")
Run Code Online (Sandbox Code Playgroud)

我收到一个错误,指出this.props.actions未定义。我不确定我错过了什么。

reactjs jestjs redux enzyme

4
推荐指数
1
解决办法
932
查看次数

虽然测试失败了,但是开始测试显示通过

import React from 'react';
import CrudApi from '../api/CrudApi'; 
import nock from 'nock';

describe('CrudList Component', () => {

    it('should have users', () => {

        afterEach(() => {
          nock.cleanAll()
        })

        CrudApi.getAll().then(
          data => {expect(data).toHaveLength(9) // this failed
          console.log(data.length) // 10}
        )
    });
});
Run Code Online (Sandbox Code Playgroud)

这是我的测试用例,它假设失败,因为getAll返回10个数组.在我的cmd我看到测试通过?

在此输入图像描述

javascript reactjs jestjs enzyme

3
推荐指数
1
解决办法
1398
查看次数

Jest和Enzyme使用ES6 Arrow功能测试React Component

我已经在运行webpack 2的React项目中添加了Jest和Enzyme.在运行jest时,我在React Component中使用ES6箭头函数时遇到了错误.

组件代码示例如下:

import React, { Component } from 'react';

class Add extends Component {
    constructor(props){
        super(props);
        this.state = this.defaultState();
    }

    defaultState = () => {
        return {
            name : ""
        }
    }
    onChange = (e) =>{
        this.setState({
            name: e.target.value
        });
    }
    handleAdd = (e) => {
        e.preventDefault();
        this.props.onAdd(this.state.name);
    }

    render= () => {
        return (
            <form>
        <!-- more markup -->
            </form>
        );
    }


}

export default Add;
Run Code Online (Sandbox Code Playgroud)

Jest在运行测试套件时失败了,我是否需要在我的软件包json/babelrc中添加任何内容以启用箭头功能测试?

reactjs jestjs enzyme

3
推荐指数
1
解决办法
1467
查看次数

在 SCSS 中将标签名称嵌套到类样式中

我想<a>在 class 中嵌套标签的选择器.class-name,以便仅将这些样式应用于同时具有<a>标签和.class-name使用 SCSS的类的元素。我想做的事情可以通过以下方式执行:

.class-name {
  font-size: 40px;
 }
 
 a.class-name {
   color: #ff6666;
 }
Run Code Online (Sandbox Code Playgroud)
<div class="class-name">Hello</div>
<a class="class-name" href="#">World!</a>
Run Code Online (Sandbox Code Playgroud)

我使用&父选择器在 SCSS 中尝试了两种不同的方法

# successfully transpiled but no change
.class-name {
  font-size: 40px;
  &a {
    color: #ff6666;
  }
}

# Got an error trying to transpile
.class-name {
  font-size: 40px;
  a& {
    color: #ff6666;
  }
}
Run Code Online (Sandbox Code Playgroud)

希望巢周围(另一路.class-namea)。

css sass

3
推荐指数
2
解决办法
5374
查看次数

如何在开玩笑测试中测试链式承诺?

下面我对我的登录操作进行了测试.我正在嘲笑firebase函数,并想测试是否调用了signIn/signOut函数.

测试通过然而我没有看到我的第二个控制台日志.这是哪一行console.log('store ==>', store);.

it('signIn should call firebase', () => {
  const user = {
    email: 'first.last@yum.com',
    password: 'abd123'
  };

  console.log('111');
  return store.dispatch(signIn(user.email, user.password)).then(() => {
    console.log('222'); // does not reach
    expect(mockFirebaseService).toHaveBeenCalled();
  });
  console.log('333');
});
Run Code Online (Sandbox Code Playgroud)

●登录操作> signIn应该调用firebase

TypeError:auth.signInWithEmailAndPassword不是函数

在此输入图像描述

正在测试的行动

// Sign in action
export const signIn = (email, password, redirectUrl = ROUTEPATH_DEFAULT_PAGE) => (dispatch) => {
  dispatch({ type: USER_LOGIN_PENDING });

  return firebase
    .then(auth => auth.signInWithEmailAndPassword(email, password))
    .catch((e) => {
      console.error('actions/Login/signIn', e);
      // Register a new user
      if (e.code …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing reactjs jestjs

3
推荐指数
1
解决办法
1984
查看次数

如果有多个测试文件,则不会输出描述名称或测试名称

TLDR

当仅使用一个文件运行Jest(23.5.0)时,它将输出“描述名称”和“测试名称”。我希望Jest在与多个测试文件一起运行时输出“描述名称”和“测试名称”,但这似乎是自动禁止的。

TMI

例:

 PASS  src/components/UsersTable.test.jsx
  UsersTable
    ? is a table (4ms)
    ? has columns 'first', 'last', 'email' (1ms)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        1.768s, estimated 2s
Ran all test suites.
Run Code Online (Sandbox Code Playgroud)

当运行两个时,它会抑制。例:

 PASS  src/components/UsersTableRow.test.jsx
 PASS  src/components/UsersTable.test.jsx

Test Suites: 2 passed, 2 total
Tests:       4 passed, 4 total
Snapshots:   0 total
Time:        2.254s
Ran all test suites.
Run Code Online (Sandbox Code Playgroud)

部分代码示例:

describe('UsersTable', () => {
    const component = renderer.create(
        <UsersTable />
    ); …
Run Code Online (Sandbox Code Playgroud)

javascript testing unit-testing jestjs

3
推荐指数
1
解决办法
468
查看次数

类型错误:无法读取未定义的属性“SHORT”

我正在使用笑话和酶为 React Native 应用程序编写单元测试。当我尝试运行测试用例时,出现以下错误:

\n\n
\n

\xe2\x97\x8f 测试套件运行失败

\n\n

类型错误:无法读取未定义的属性“SHORT”

\n\n

在对象。(node_modules/react-native-simple-toast/index.js:7:23)\n 在对象处。(src/client/components/Catalogue/events.js:10:29)\n 在对象处。(测试/events-test.js:5:13)\n 在 Generator.next ()\n 在 new Promise ()\n 在 Generator.next ()\n 在

\n
\n\n

该错误是由于从react-native-simple-toast导入Toast造成的。如果没有这个导入,测试运行正常,但我的组件中需要react-native-simple-toast,因此无法删除它。我以某种方式需要测试在我的组件中使用react-native-simple-toast运行:

\n\n
import React, { Component } from 'react'\nimport { Platform, ActivityIndicator, Linking, Dimensions, View, Text, \nTouchableOpacity, TextInput, StyleSheet, Keyboard, Image, \nKeyboardAvoidingView } from 'react-native'\nimport { Container, Header, Card, CardItem, Content, Left, Right, Body, \nTitle, Icon, Button } from 'native-base';\nimport IconIOSback from 'react-native-vector-icons/Ionicons';\nimport IconMenu from 'react-native-vector-icons/MaterialIcons';\nimport axios from 'axios';\nimport SERVER_URL from …
Run Code Online (Sandbox Code Playgroud)

unit-testing jestjs react-native enzyme react-native-ios

3
推荐指数
1
解决办法
3234
查看次数

package.json 中的 Jest 配置失败

在此输入图像描述

您好,这是我第一次使用 Jest。我正在尝试将其配置为与 Enzyme 和 webpack 一起使用。我将其添加到我的 package.json 文件中进行测试:

"scripts": {
    "test": "jest",
    "test:watch": "jest --watch",
  }
Run Code Online (Sandbox Code Playgroud)

和笑话配置:

"jest": {
    "setupTestFrameworkScriptFile": "<rootDir>_tests_/setup/setupEnzyme.js",
    "testPathIgnorePatterns": [
      "<rootDir>/_tests_/setup/"
    ],
    "verbose": true
  }
Run Code Online (Sandbox Code Playgroud)

这是我的 setupEnzyme 文件:

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });
Run Code Online (Sandbox Code Playgroud)

但我收到错误Module <rootDir>_tests_/setup/setupEnzyme.js in the setupTestFrameworkScriptFile option was not found.

configuration package reactjs jestjs enzyme

3
推荐指数
1
解决办法
9950
查看次数

配置 Jest 以使用 Create React App 在测试之间重置模拟

当我将以下代码添加到我的时,package.json我收到一条错误消息,指出它不受支持:

"jest": {
    "resetMocks": true
},
Run Code Online (Sandbox Code Playgroud)
  1. 我很困惑为什么这不起作用,因为它似乎已添加到此拉取请求中
  2. 还有另一种方法吗?

提前致谢!

reactjs jestjs

3
推荐指数
1
解决办法
1729
查看次数

vue.js unit:test w test-utils 和 Jest:如何在方法中测试 window.open()?

在我的组件 AudioPlayer 中,我有一个 download() 方法:

download() {
  this.audio.pause();
  window.open(this.file, "download");
},
Run Code Online (Sandbox Code Playgroud)

我可以测试第一行:

this.audio.pause();
Run Code Online (Sandbox Code Playgroud)

但是我如何测试(我应该吗?第二行:

window.open(this.file, "download");
Run Code Online (Sandbox Code Playgroud)

这是我当前的规范文件测试

  it("should open a window from downloadBtn", async () => {
    // jsdom doesn't support any loading or playback media operations. 
    // As a workaround you can add a few stubs in your test setup:
    window.HTMLMediaElement.prototype.pause = () => { /* do nothing */ };
    // given
    const wrapper = mount(AudioPlayer, {
      attachToDocument: true,
      propsData: {
        autoPlay: false,
        file: file,
        ended,
        canPlay
      }
    }); …
Run Code Online (Sandbox Code Playgroud)

unit-testing vue.js jestjs vue-test-utils

3
推荐指数
1
解决办法
1412
查看次数