babel-jest ES2015导入声明 [英] babel-jest ES2015 import statements
问题描述
有人用es2015语法编写茉莉花/开玩笑的测试吗?它需要多少匀场/填充/胶接?
has anyone been writing jasmine / jest tests using es2015 syntax? how much shimming / polyfill / gerrymandering does it require?
我无法正确导入功能.我有一个模块: ..../utils/TweetUtils.js
i’m having trouble importing functions correctly. i have one module: …./utils/TweetUtils.js
'use strict';
export function getListOfTweetIds (tweets) {
return Object.keys(tweets);
};
和一个测试套件:
..../__ tests __/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();
});
});
如果我将控制台输出侵入套件,如下所示:expect(‘’).toBe(TweetUtils);
If I hack a console output into the suite with something like this:expect(‘’).toBe(TweetUtils);
茉莉花报告了这一点:
- Expected: '' toBe: {
default: {
getListOfTweetIds: Function
},
getListOfTweetIds: Function
}
所以看起来import语句正在做某事,但是显然不是诚实地导入我的方法.使用命名函数语法导入时,我得到相同的结果:import {getListOfTweetIds} from ‘../TweetUtils’;
但是,如果我使用默认语法:import getListOfTweetIds from ‘../TweetUtils’;
第二个规范失败-不再是typeof function
,而是typeof object // => {default: 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’;
But if I use the default syntax: import getListOfTweetIds from ‘../TweetUtils’;
The second spec fails - it’s no longer typeof function
, but typeof object // => {default: Function}
我一直在整理文档和公开问题.已经有相关问题几个月了,但是已知问题似乎并不正确.我尝试导入我的jest.dontMock语句来避免吊起,大约是: https://github .com/babel/babel-jest/issues/16 但没有骰子.
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.
如果我将 TweetUtils.js 修改为使用module.exports = function…
并使用const myFunction = require(‘../TweetUtils’)
将其带入套件,则一切正常,但是感觉不像是在传播真正的ES2015魔术.在生态系统赶上新语法的同时,每个人现在都只是在处理一些怪异的解决方法吗?
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. Is everyone just dealing with wonky work-arounds right now while the ecosystem catches up to the new syntax?
推荐答案
正如您所说,import
语句被吊起,并且会导致开玩笑的自动嘲笑功能出现问题(该模块先于导入 >您告诉玩笑要取消嘲笑).
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).
TweetUtils.getListOfTweetIds
已正确导入,但已被模拟,因此每个调用都返回undefined
.这就是第三个期望失败的原因.
TweetUtils.getListOfTweetIds
is correctly imported but it's mocked, so each calls return undefined
. That's why the third expectation fails.
导入jest.dontMock
语句可以工作(我对其进行了测试),但对我来说听起来很脏(您是否真的要为每个测试模块创建一个"dontmock模块"文件?)
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 ?)
您必须对测试的模块使用require
语法.替换
You have to use the require
syntax for the tested module. Replace
import * as TweetUtils from '../TweetUtils';
通过
const TweetUtils = require('../TweetUtils');
在修复问题之前,在笑话示例中是相同的: jest#379
It was the same in jest example before I fixed it : jest#379
这篇关于babel-jest ES2015导入声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!