gpt4 book ai didi

javascript - 在 facebookincubator/create-react-app 中编写测试

转载 作者:行者123 更新时间:2023-11-30 20:56:30 27 4
gpt4 key购买 nike

我现在是第一次学习ReactJS。

来自https://egghead.io/lessons/react-redux-writing-a-counter-reducer-with-tests

样板:facebookinculator/create-react-app
使用测试:npm test

我决定不使用普通的在线网站进行练习的主要原因是因为我想尽可能多地熟悉真实的工具。这是我的代码。

export const counter = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
}

还有我的utils.test.js

import React from 'react';
import counter from '../utils/utils';

it('expect 1 to be return from reducer', () => {
expect(counter(0, {type: 'INCREMENT'})).toEqual(1)
});

错误:

 FAIL  src/utils/utils.test.js
● expect 1 to be return from reducer

TypeError: (0 , _utils2.default) is not a function

at Object.<anonymous>.it (src/utils/utils.test.js:5:30)
at new Promise (<anonymous>)
at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)

PASS src/App.test.js

Test Suites: 1 failed, 1 passed, 2 total
Tests: 1 failed, 1 passed, 2 total
Snapshots: 0 total
Time: 0.062s, estimated 1s
Ran all test suites.

Watch Usage: Press w to show more.

问题:
我哪里错了?我已经检查过文档。我还尝试了 expect(1).toEqual(1) 来确认我的配置是正确的。

引用资料:
https://facebook.github.io/jest/docs/en/expect.html#toequalvalue

最佳答案

看起来您的计数器命名导入 而不是默认导入

尝试使用它来导入 counter:

import { counter } from '../utils/utils';

只有当您将counter 导出为utils 模块的默认值时,您当前使用的语法才有效。

关于javascript - 在 facebookincubator/create-react-app 中编写测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47605217/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com