How to write Jest transformIgnorePatterns

后端 未结 4 2100
独厮守ぢ
独厮守ぢ 2021-02-19 02:52

I have this excpetion

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 p         


        
相关标签:
4条回答
  • 2021-02-19 03:04

    I added this setting to my config, but no joy. Still getting the same error as Sergey Aslanvov.

    "transformIgnorePatterns": [
      "/node_modules/(?!@vuetify)"
    ],
    

    also tried

    "transformIgnorePatterns": [
      "\\node_modules\\@vuetify",
    ],
    

    Below is a sample of my component that extends VDataTable

    import {
      Vue,
     Mixins,
     Component,
    } from 'vue-property-decorator';
    
    import { VDataTable } from 'vuetify/lib';
    
    @Component
    export class BPagedGrid extends Mixins(Vue, VDataTable) { }
    
    0 讨论(0)
  • 2021-02-19 03:05

    I had a similar error while working on a project with jest, webpack, and vanilla js, so that error is thrown when jest encounters this line of code import '../css/styles.css'; in any /*?.js$/ file. I solved it by moving the jest configurations from the package.json file into a jest.config.js file with the following minimal configuration;

    // jest.config.js
    module.exports = 
      "moduleNameMapper": {
        "\\.(css|less|scss)$": "identity-obj-proxy"
      }
    }

    Then in babel.config.js

    module.exports = {
      presets: [
        [
          '@babel/preset-env',
          {
            targets: {
              node: 'current',
            },
          },
        ],
      ],
    };

    0 讨论(0)
  • 2021-02-19 03:05

    Actually, it seems like typescript is not going well with JEST test cases, so what i feel like either JEST need to ignore those files or those packages need to be transpiled to js code which JEST can understand,

    you can try this

    "transformIgnorePatterns": [ "node_modules/(?!(@microsoft/sp-core-library))" ],
    

    and in tsconfig.json

    "allowJs": true,
    

    for more details please check this post

    https://github.com/SharePoint/sp-dev-docs/issues/2325#issuecomment-446416878

    0 讨论(0)
  • 2021-02-19 03:19

    The transformIgnorePatterns means if the test path matches any of the patterns, it will not be transformed.

    Note: DO NOT SPLIT MULTI-LINES.

    The jest will ignore all node_modules via default.

    So you must write like this:

    "transformIgnorePatterns": [
      // all exceptions must be first line
      "/node_modules/(?!@microsoft/sp-core-library|sp-dialog|other_libs_need_transform)"
    ],
    
    0 讨论(0)
提交回复
热议问题