【问题标题】:Javascript Test unit with Jest an ES6 module带有 Jest 和 ES6 模块的 Javascript 测试单元
【发布时间】:2019-05-28 14:10:27
【问题描述】:

谷歌搜索时有太多不同的帖子,无法选择清晰且最新的解决方案...

我写了 3 个测试来检查不同的可能性

===========。测试 1 正常 =================

// helloJest.js

function sayHello() {
  return "hello there jest"
}
module.exports = sayHello;

// helloJestTest

const sayHello = require('../../src/client/js/helloJest');
test('string returning hello there jest', () => {// 
  expect(sayHello()).toEqual('hello there jest');
});

===========。测试 2 失败 ================

// helloJest.js

function sayHello() {
  return "hello there jest"
}
export default { sayHello }; // <= changed

// helloJestTest

const sayHello = require('../../src/client/js/helloJest');
test('string returning hello there jest', () => {// 
  expect(sayHello()).toEqual('hello there jest');
});

TypeError: sayHello is not a function

      3 |
      4 | test('string returning hello there jest', () => {//
    > 5 |   expect(sayHello()).toEqual('hello there jest');
        |          ^
      6 | });
      7 |

===========。测试 3 失败 ================

// helloJest.js

function sayHello() {
  return "hello there jest"
}
export default { sayHello }; //  <= changed

// helloJestTest

import { sayHello } from '../../src/client/js/helloJest'; // <= changed
test('string returning hello there jest', () => {// 
  expect(sayHello()).toEqual('hello there jest');
});

    TypeError: (0 , _helloJest.sayHello) is not a function

      3 |
      4 | test('string returning hello there jest', () => {//
    > 5 |   expect(sayHello()).toEqual('hello there jest');
        |          ^
      6 | });

如何正确通过TEST 3???

我正在使用以下软件包

package.json

"babel-core": "^6.26.3",
"babel-jest": "^23.6.0",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
...
"jest": {
    "moduleFileExtensions": ["js"],
    "transform": { "^.+\\.js?$": "babel-jest" },
    "testRegex": "/tests/.*\\.(js)$"
  }

我在

.babelrc

{
  "presets": ["env"]
}

【问题讨论】:

  • 2和3有什么区别?你似乎只做了一半的改变;在这两种情况下,默认导出都是 object,而不仅仅是函数。
  • 你有没有尝试在你的 test2 中做const { sayHello } = require('../../src/client/js/helloJest');
  • 在 2. (export default ES6, require()) In 3. (export default ES6, import ES6)

标签: javascript ecmascript-6 jestjs babeljs testunit


【解决方案1】:

你在那里的几个地方绊倒了。主要是:您不要将{} 与默认导入/导出一起使用。

这个:

export default { sayHello };

导出一个对象作为模块的默认导出。该对象有一个属性,sayHello,指的是函数。要将函数设为默认导出,请勿使用{}

export default sayHello;

那么,导入时,如果要默认导入,不要使用{}

import sayHello from '../../src/client/js/helloJest';

如果你想导出一个命名的导出,你可以使用{}:

export { sayHello };

import { sayHello } from '../../src/client/js/helloJest';

plunker 上的示例:https://embed.plnkr.co/f8PhOhULphQUrVXwdXF3/

【讨论】:

  • 默认情况下没问题...但是 Jest 似乎不喜欢导入命名导出 ....
  • @erwin - 您确定导出正确吗?如果 Jest 在命名导出方面有问题,那就太奇怪了。
  • @erwin 一件事让我绊倒了一段时间。 {} 没有解构。它只是从模块中导入 named exports
【解决方案2】:

测试 2

您确实将具有单个属性的对象导出为默认值,即sayHello 函数,因此您应该通过以下方式开玩笑地导入它:

const { sayHello } = require('../../src/client/js/helloJest');

测试 3

你再次像上面那样导出。 在这种情况下,您可以将其导入如下:

import Hello from '../../src/client/js/helloJest';

然后你应该能够将你的函数用作:

Hello.sayHello

【讨论】:

  • 就是这样!我现在在测试 3 中得到了它......并且理解了所有 3 个测试......非常感谢!!!
猜你喜欢
  • 2018-09-14
  • 1970-01-01
  • 2018-03-20
  • 2015-12-10
  • 2018-06-03
  • 1970-01-01
  • 2023-03-13
  • 2015-07-10
  • 2011-11-16
相关资源
最近更新 更多