我很高兴
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
C:\Code\SPFx\BCO\node_modules\@microsoft\sp-core-library\lib\index.js:11
export { default as _BrowserDetection } from './BrowserDetection';
^^^^^^
SyntaxError: Unexpected token export
19 | } from 'office-ui-fabric-react/lib/Utilities';
20 | import { IUserProvider } from "../UserProviders/IUserProvider";
> 21 | import {
| ^
22 | Environment,
23 | EnvironmentType
24 | } from '@microsoft/sp-core-library';
at ScriptTransformer._transformAndBuildScript (node_modules/jest/node_modules/jest-runtime/build/script_transformer.js:403:17)
at Object.<anonymous> (src/webparts/BCO/components/EmployeeSelector/EmployeeSelector.tsx:21:1)
at Object.<anonymous> (src/webparts/BCO/components/FieldMapping/FieldMapping.tsx:13:1)
并在config.json中尝试了这些transformIgnorePatterns表达式
"transformIgnorePatterns": [
"\\node_modules\\@microsoft\\sp-dialog",
"\\node_modules\\@microsoft\\sp-core-library",
"node_modules/(?!sp-core-library)",
"node_modules/(?!@microsoft/sp-core-library)"
],
,但没有一个起作用。我在Windows 10上运行此程序,因此也尝试了this格式
答案 0 :(得分:0)
transformIgnorePatterns
表示如果测试路径与任何模式都匹配,则不会进行转换。
笑话会默认忽略所有node_modules
。
所以您必须这样写:
"transformIgnorePatterns": [
// all exceptions must be first line
"/node_modules/(?!@microsoft/sp-core-library|sp-dialog|other_libs_need_transform)"
],
答案 1 :(得分:0)
我将此设置添加到了配置中,但没有任何乐趣。仍然出现与Sergey Aslanvov相同的错误。
"transformIgnorePatterns": [
"/node_modules/(?!@vuetify)"
],
也尝试过
"transformIgnorePatterns": [
"\\node_modules\\@vuetify",
],
下面是我的扩展VDataTable的组件的示例
import {
Vue,
Mixins,
Component,
} from 'vue-property-decorator';
import { VDataTable } from 'vuetify/lib';
@Component
export class BPagedGrid extends Mixins(Vue, VDataTable) { }
答案 2 :(得分:0)
在使用jest,webpack和vanilla js的项目中工作时,我遇到了类似的错误,因此当jest遇到此行代码时,会抛出该错误 import'../ css / styles.css'; < / strong>在任何 / * ?. js $ / 文件中。 我通过将jest配置从 package.json 文件移动到具有以下最小配置的 jest.config.js 文件中来解决了该问题;
// jest.config.js
module.exports =
"moduleNameMapper": {
"\\.(css|less|scss)$": "identity-obj-proxy"
}
}
然后在babel.config.js中
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
};
答案 3 :(得分:0)
实际上,看来打字稿在JEST测试用例中进展不佳,所以我觉得JEST需要忽略那些文件或将那些包转换为JEST可以理解的js代码,
您可以尝试
"transformIgnorePatterns": [ "node_modules/(?!(@microsoft/sp-core-library))" ],
和tsconfig.json中
"allowJs": true,
有关更多详细信息,请查看此帖子
https://github.com/SharePoint/sp-dev-docs/issues/2325#issuecomment-446416878