简体   繁体   English

babel-jest ES2015导入声明

[英]babel-jest ES2015 import statements

has anyone been writing jasmine / jest tests using es2015 syntax? 有没有人用es2015语法编写茉莉/开玩笑测试? how much shimming / polyfill / gerrymandering does it require? 它需要多少垫/ /填充/分散?

i'm having trouble importing functions correctly. 我无法正确导入功能。 i have one module: …./utils/TweetUtils.js 我有一个模块: .... / utils / TweetUtils.js

'use strict';

export function getListOfTweetIds (tweets) {
  return Object.keys(tweets);
};

and one test suite: 和一个测试套件:

…./__tests__/TweetUtils-test.js ... ./__测试__ / TweetUtils-test.js

'use strict';
jest.dontMock('../TweetUtils');
import * as TweetUtils from '../TweetUtils';

describe('Tweet utilities module', () => {

  it('has access to the TweetUtils methods', () => {

    let testObj = {a:'a',b:'b',c:'c'};
    // Passes
    expect(TweetUtils.getListOfTweetIds).toBeDefined();
    // Passes
    expect(typeof TweetUtils.getListOfTweetIds).toBe('function');
    // Fails
    expect(TweetUtils.getListOfTweetIds(testObj)).toBeTruthy();
  });
});

If I hack a console output into the suite with something like this: expect('').toBe(TweetUtils); 如果我用这样的东西破解控制台输出到套件: expect('').toBe(TweetUtils);

Jasmine reports this: Jasmine报道了这个:

- Expected: '' toBe: {
      default: {
          getListOfTweetIds: Function
      },
      getListOfTweetIds: Function
  }

So it seems like the import statement is doing something, but it's clearly not importing my methods honestly. 因此看起来导入语句正在做某事,但显然不是诚实地导入我的方法。 I get the same results when I import using the named function syntax: import {getListOfTweetIds} from '../TweetUtils'; 当我使用命名函数语法import {getListOfTweetIds} from '../TweetUtils';时得到相同的结果: import {getListOfTweetIds} from '../TweetUtils'; But if I use the default syntax: import getListOfTweetIds from '../TweetUtils'; 但是,如果我使用默认语法: import getListOfTweetIds from '../TweetUtils'; The second spec fails - it's no longer typeof function , but typeof object // => {default: Function} 第二个规范失败 - 它不再是typeof function ,而是typeof object // => {default: Function}

I've been combing the docs and open-issues. 我一直在梳理文档和开放问题。 There've been related issues for a few months, but the known issues don't seem right. 几个月来一直存在相关问题,但已知问题似乎并不正确。 I've tried importing my jest.dontMock statements to avoid hoisting, circa: https://github.com/babel/babel-jest/issues/16 but no dice. 我已经尝试导入我的jest.dontMock语句以避免提升,大约: https//github.com/babel/babel-jest/issues/16但没有骰子。

Everything works if I modify TweetUtils.js to use module.exports = function… and bring it into the suite using const myFunction = require('../TweetUtils') , but it doesn't feel like I'm channeling the true ES2015 magic. 如果我修改TweetUtils.js以使用module.exports = function…并使用const myFunction = require('../TweetUtils')将它带入套件,那么一切都有效,但它并不觉得我正在引导真正的ES2015魔法。 Is everyone just dealing with wonky work-arounds right now while the ecosystem catches up to the new syntax? 当生态系统赶上新语法时,现在每个人都只是处理不稳定的解决方案吗?

As you said, import statements are hoisted and it causes problems with the jest auto-mocking feature (the module is imported before you tell jest to unmocked it). 如你所说, import语句被提升并导致jest自动模拟功能出现问题(模块你告诉jest to unmocked 之前导入)。

TweetUtils.getListOfTweetIds is correctly imported but it's mocked, so each calls return undefined . TweetUtils.getListOfTweetIds被正确导入但是它被TweetUtils.getListOfTweetIds ,因此每个调用都返回undefined That's why the third expectation fails. 这就是第三个期望失败的原因。

Importing jest.dontMock statement could work (I tested it) but it sounds dirty to me (do you really want to create a "dontmock module" file for each test modules ?) 导入jest.dontMock语句可以工作(我测试了它)但对我来说听起来很脏(你真的想为每个测试模块创建一个“dontmock模块”文件吗?)

You have to use the require syntax for the tested module. 您必须对测试模块使用require语法。 Replace 更换

import * as TweetUtils from '../TweetUtils';

by 通过

const TweetUtils = require('../TweetUtils');

It was the same in jest example before I fixed it : jest#379 在我修复它之前,它在开玩笑示例中是相同的: jest#379

The issue seems to be with babel and not with jest . 这个问题似乎与巴贝尔有关,而不是开玩笑。 Try this solution. 试试这个解决方案

If you are using 'create-react-app' then install these dev dependencies: 如果您使用'create-react-app',那么安装这些dev依赖项:

"babel-cli": "^6.26.0",
"babel-jest": "^22.4.1",
"babel-preset-react-app": "^3.1.1",

And also update the .babelrc (create this file if it doesn't exist ) : 并且还更新.babelrc (如果它不存在则创建此文件):

{
"presets": [
    "@babel/preset-env",
    "@babel/preset-react"
      ]
}

And now both jest and npm test both work as they should. 而现在,jest和npm测试都可以正常工作。

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

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