Jestjs 如何编写Jest transformIgnorePatterns

Jestjs 如何编写Jest transformIgnorePatterns,jestjs,Jestjs,我有这个借口 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,

我有这个借口

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)

他们都没有成功。我在Windows 10上运行了这个程序,所以我也尝试了格式化
transformIgnorePatterns
意味着如果测试路径与任何模式匹配,它将不会被转换

注意:不要拆分多行

默认情况下,jest将忽略所有节点模块

所以你必须这样写:

“transformIgnorePatterns”:[
//所有例外情况必须在第一行
“/node_modules/(?!@microsoft/sp核心库| sp dialog |其他_库_需要_转换)”
],

我将此设置添加到配置中,但没有乐趣。还是和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) { }

在使用jest、webpack和vanilla js进行项目时,我遇到了一个类似的错误,因此当jest遇到这行代码
import'../css/styles.css'时就会抛出错误/*?.js$/
文件中的code>

我通过将jest配置从
package.json
文件移动到
jest.config.js
文件,并使用以下最小配置解决了这个问题:

//jest.config.js
module.exports=
“moduleNameMapper”:{
“\\(css |减去| scss)$”:“标识obj代理”
}
}
然后在
babel.config.js
中:

module.exports={
预设:[
[
“@babel/preset env”,
{
目标:{
节点:“当前”,
},
},
],
],
};

事实上,typescript似乎不适合JEST测试用例,所以我觉得JEST需要忽略那些文件,或者那些包需要传输到JEST可以理解的js代码中

你可以试试这个

"transformIgnorePatterns": [ "node_modules/(?!(@microsoft/sp-core-library))" ],
在tsconfig.json中

"transformIgnorePatterns": [
  "\\node_modules\\@microsoft\\sp-dialog",
  "\\node_modules\\@microsoft\\sp-core-library",
  "node_modules/(?!sp-core-library)",
  "node_modules/(?!@microsoft/sp-core-library)"
],
"allowJs": true,
欲了解更多详情,请查看此帖子


有人知道如何解决这个问题吗?在我的例子中,我有一些扩展Vuetify组件的组件(即从“Vuetify/lib”导入{VDataTable})。JEST文档严重缺乏这种复杂的配置。更实用、更常见的示例可能会有所帮助。请尝试
“@microsoft[/\\\]sp dialog”
,或者更可能是
“@microsoft[/\\\\]sp dialog”
。我认为这里需要双重逃逸。你用的是哪种技术?是react/react native吗?Dipen Shah,我正在使用VueJS v2这一款似乎适合我