Jestjs Jest配置能够使用节点模块源运行测试

Jestjs Jest配置能够使用节点模块源运行测试,jestjs,ts-jest,Jestjs,Ts Jest,我有两个项目: 应用程序 库(用于共享代码) 在我的应用程序中,我有一个测试直接使用我的npm包库的源代码 从'@xxx/library/src/entities/address'导入{mockAddress} 问题 Jest遇到了意外的标记 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

我有两个项目:

  • 应用程序
  • 库(用于共享代码)
  • 在我的应用程序中,我有一个测试直接使用我的npm包库的源代码

    从'@xxx/library/src/entities/address'导入{mockAddress}

    问题 Jest遇到了意外的标记

    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
    
    选择权

    我试过的 我试图将我的库文件夹添加到
    transformIgnorePatterns

      transformIgnorePatterns: [
        '<roodDir>/node_modules/(?!vee-validate/dist/rules)',
        '<roodDir>/node_modules/(?!@xxx/library/src)',
      ],
    
    编辑1 jest.config
    您的transformIgnorePatterns是相互排斥的。节点应该只有一个模式_modules@EstusFlask我确实更改了编辑1,但仍然是相同的“node_modules/(?!vee validate/dist/rules |@xxx/lib/)”,。我不确定是否需要添加beforerootDir不会有帮助,因为这样做限制更大。路径过多并且没有考虑反斜杠,
    /
    应该是
    [/\\\\]
    。你的意思是用
    [/\\\]
    替换所有的
    //\\\\\\\\]
    ,所以它应该是`节点单元模块[/\\\](?!vee validate[/\\\\\\\]dist[/\\\\\]规则.@xxx[/\\\\\\\\]lib[/\]')``?我直接使用库中的源代码,这可能是一个问题吗?
    '\\.(ts|js)x?$': 'ts-jest'
    
     transform: {
        'vee-validate/dist/rules': 'babel-jest',
        '\\.(ts|tsx)?$': 'ts-jest',
      },
    
      transformIgnorePatterns: [
        'node_modules/(?!vee-validate/dist/rules|@xxx/lib/)',
      ],
    
      module.exports = {
      globals: {
        'ts-jest': {
          isolatedModules: true,
        },
      },
      verbose: true,
      preset: '@vue/cli-plugin-unit-jest/presets/typescript-and-babel',
      collectCoverage: false,
    
      collectCoverageFrom: [
        '**/entities/**/*.ts',
        '**/services/**/*.ts',
        '**/store/**/*.ts',
        '**/ui/components/**/*.{js,vue}',
        '**/ui/views/**/**.{js,vue}',
        '!src/main.ts', // No need to cover bootstrap file
        '!**/store.ts',
        '!**/*.mock.ts',
        '!**/*.types.ts',
        '!**/index.ts',
      ],
      transform: {
        'vee-validate/dist/rules': 'babel-jest',
        '^.+\\.tsx?$': 'ts-jest',
      },
      transformIgnorePatterns: [
        '<roodDir>/node_modules/(?!vee-validate/dist/rules)',
      ],
      moduleNameMapper: {
        '^@/(.*)$': '<rootDir>/src/$1',
      },
      testMatch: [
        '**/*.(spec|test).(js|jsx|ts|tsx)',
      ],
      roots: [
        '<rootDir>/src',
      ],
      testEnvironmentOptions: {
        // Allow test environment to fire onload event
        // See https://github.com/jsdom/jsdom/issues/1816#issuecomment-355188615
        resources: 'usable',
      },
      reporters: [
        'default',
        [
          'jest-trx-results-processor',
          {
            outputFile: './coverage/test-results.trx',
            defaultUserName: 'user name to use if automatic detection fails',
          },
        ],
      ],
    };
    
    {
      "compilerOptions": {
        "strictNullChecks": false,
        "target": "esnext",
        "module": "esnext",
        "strict": true,
        "jsx": "preserve",
        "importHelpers": true,
        "moduleResolution": "node",
        "experimentalDecorators": true,
        "allowJs": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "sourceMap": true,
        "baseUrl": ".",
        "types": [
          "webpack-env",
          "jest",
          "vuetify",
          "vue-meta"
        ],
        "paths": {
          "@/*": [
            "src/*"
          ]
        },
        "lib": [
          "es6",
          "es2017",
          "dom",
          "dom.iterable",
          "scripthost"
        ]
      },
    
      "include": [
        "src/**/*.ts",
        "src/**/*.tsx",
        "src/**/*.vue",
        "tests/**/*.ts",
        "tests/**/*.tsx",
      ],
      "exclude": [
        "node_modules"
      ]
    }