我有一个包含多个模块的项目(使用Lerna),我想使用Jest来运行测试。但是,当我测试使用共享模块的代码(通过Lerna的npm链接模块)时,似乎Babel未正确应用,我收到以下错误:
# split into train and test
from sklearn import cross_validation
data_train, data_test, labels_train, labels_test = cross_validation.train_test_split(
data.ApiCall,
data.Malware,
test_size=0.25,
random_state=42)
print (data_train[:10])
### text vectorization--go from strings to lists of numbers
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_selection import SelectPercentile, f_classif
vectorizer = TfidfVectorizer(sublinear_tf=True, max_df=0.5)
data_train_transformed = vectorizer.fit_transform(data_train)
data_test_transformed = vectorizer.transform(data_test)
print (data_train_transformed[:10])
# slim the data for training and testing
selector = SelectPercentile(f_classif, percentile=10)
selector.fit(data_train_transformed, labels_train)
data_train_transformed = selector.transform(data_train_transformed).toarray()
data_test_transformed = selector.transform(data_test_transformed).toarray()
print (data_train_transformed[:10])
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score
clf = GaussianNB()
clf.fit(data_train_transformed, labels_train)
predictions = clf.predict(data_test_transformed)
print (accuracy_score(labels_test, predictions))
我的项目结构如下:
SyntaxError: Unexpected token import
- my-project
|- shared
|- native
|- web
和web
需要native
模块。当我进入shared
目录并在Jest中运行本地测试时,一切正常。如果我在shared
目录中运行Jest测试,只要我在web
中包含某些内容,就会出现上述错误。
这是一个导致错误的超级简单测试:
shared
我的import { util } from 'shared';
it('returns false if not prod', () => {
expect(util.isProd()).toBe(false);
});
看起来像这样:
.babelrc
我尝试了所有可以找到的东西,包括:
{
"presets": [
"env",
"flow",
"react"
],
"plugins": [
"flow-react-proptypes",
"transform-object-rest-spread",
"transform-class-properties"
]
}
预设且为测试环境启用es2015
的配置modules
选项transform
模块中执行,因此,Jest和shared
也会安装在那里。答案 0 :(得分:5)
我必须在transformIgnorePatterns
模块的package.json
中更改Jest的web
配置选项。
{
"jest": {
"transformIgnorePatterns": [
"<rootDir>/node_modules/(?!shared|another)"
]
},
}