简体   繁体   English

在React Native项目中开玩笑进行单元测试(“ Not a function”错误)

[英]Unit testing with jest in React Native project (“Not a function” error)

I am working on a React Native project (v0.46). 我正在做一个React Native项目(v0.46)。 I've been able to write component snapshot tests successfully, but I'm having trouble wrapping my head around unit testing JavaScript ES6 functions. 我已经能够成功编写组件快照测试,但是在将单元测试JavaScript ES6函数进行包装时遇到了麻烦。

Here are my functions in FakeUtils.js: 这是我在FakeUtils.js中的函数:

export function sum(a, b) {
    return a + b;
}

export function subtract(a, b) {
    return a - b;
}

And here are my tests for them: 这是我对他们的测试:

import sum from '../src/utils/FakeUtils.js';
import subtract from '../src/utils/FakeUtils.js';

describe('sum', () => {   
    it('should sum 2 nums', () => {
        expect(sum(1,2)).toBe(3);
    });
});

describe('subtract', () => {   
    it('should subtract 2 nums', () => {
        expect(subtract(2,1)).toBe(1);
    });
});

When I run npm test, this is the output I receive: 当我运行npm test时,这是我收到的输出:

FAIL  __tests__/FakeUtilsTest.js
  ● sum › should sum 2 nums

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

      at Object.<anonymous> (__tests__/FakeUtilsTest.js:6:31)
      at tryCallTwo (node_modules/promise/lib/core.js:45:5)
      at doResolve (node_modules/promise/lib/core.js:200:13)
      at new Promise (node_modules/promise/lib/core.js:66:3)
      at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)
      at tryCallOne (node_modules/promise/lib/core.js:37:12)
      at node_modules/promise/lib/core.js:123:15

  ● subtract › should subtract 2 nums

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

      at Object.<anonymous> (__tests__/FakeUtilsTest.js:12:31)
      at tryCallTwo (node_modules/promise/lib/core.js:45:5)
      at doResolve (node_modules/promise/lib/core.js:200:13)
      at new Promise (node_modules/promise/lib/core.js:66:3)
      at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)
      at tryCallOne (node_modules/promise/lib/core.js:37:12)
      at node_modules/promise/lib/core.js:123:15

Any idea what I'm doing wrong here? 知道我在做什么错吗?

Your import statements are off a bit. 您的导入语句有点偏离。 Try this instead: 尝试以下方法:

import { sum, subtract } from '../src/utils/FakeUtils.js';

Since you aren't exporting with the default keyword, you need to place your imports in curly braces. 由于未使用default关键字导出,因此需要将导入内容放在大括号中。 You can only have one default export, so this approach makes sense. 您只能有一个默认导出,因此这种方法很有意义。

I happens because you are exporting your functions incorrectly in relation to import. 我发生是因为您相对于导入错误地导出了函数。 You should export it this way 你应该这样导出

const function sum(a, b) {
  return a + b;
}

const function subtract(a, b) {
  return a - b;
}
export { sum, subtract}

or import it this way 或以这种方式导入

import {sum, subtract } from '../src/utils/FakeUtils.js';

You are importing the functions wrong. 您导入的功能错误。 You can't do import sum from '../src/utils/FakeUtils.js'; import subtract from '../src/utils/FakeUtils.js' 您不能import sum from '../src/utils/FakeUtils.js'; import subtract from '../src/utils/FakeUtils.js' import sum from '../src/utils/FakeUtils.js'; import subtract from '../src/utils/FakeUtils.js'

Unless you are default exporting the functions , like: 除非您默认导出函数,如:

default export sum;

It works that way to ease with importing modules with one exported object. 它的工作方式可简化带有一个导出对象的模块的导入。 In your case you need to import not default functions , like: 在您的情况下,您不需要导入默认功能,例如:

import {subtract,sum} from '../src/utils/FakeUtils.js'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM