首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当从node_modules内部导入碳图标- svelte时,无法在svelte组件中使用Jest测试: Jest遇到意外令牌

当从node_modules内部导入碳图标- svelte时,无法在svelte组件中使用Jest测试: Jest遇到意外令牌
EN

Stack Overflow用户
提问于 2021-10-20 15:39:39
回答 1查看 575关注 0票数 0

我想导入一个图标从包装碳图标-瘦到我的苗条组件。它在浏览器中工作得很好,但是我不能测试这个组件。在进口碳素图标之前,睾丸运转良好。这是我的配置:

svelte.config.test.cjs

代码语言:javascript
复制
const preprocess = require('svelte-preprocess');
require('dotenv').config()

module.exports = {
    preprocess: preprocess({
        replace: [[/import.meta.env.([A-Z_]+)/, (importMeta) =>
        { return JSON.stringify(eval(importMeta.replace('import.meta', 'process')))} ]]
    })
};

jest.config.cjs

代码语言:javascript
复制
const { pathsToModuleNameMapper } = require('ts-jest/utils');
const { compilerOptions } = require('./tsconfig.json');

module.exports = {
    transform: {
        '^.+\\.svelte$': [
            'svelte-jester',
            {
                preprocess: './svelte.config.test.cjs'
            }
        ],
        "^.+\\.(js)$": "babel-jest",
        '^.+\\.(ts)$': [require.resolve('jest-chain-transform'),
            { transformers: ['../../../build-utils/importMetaTransformer.cjs', 'ts-jest'] }
        ]
    },
    testMatch: ["**/spec/**/*.js"],
    moduleFileExtensions: ['js', 'ts', 'svelte'],
    setupFilesAfterEnv: ['<rootDir>/jest-setup.ts'],
    moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {prefix: '<rootDir>/'})
};

tsconfig.json

代码语言:javascript
复制
{
  "compilerOptions": {
    "moduleResolution": "node",
    "module": "es2020",
    "lib": ["es2020", "DOM"],
    "target": "es2019",
    "importsNotUsedAsValues": "error",
    "allowSyntheticDefaultImports": true,
    "isolatedModules": true,
    "resolveJsonModule": true,
    "sourceMap": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": ".",
    "allowJs": true,
    "checkJs": true,
    "paths": {
      "$/*": ["src/*"]
    }
  },
  "include": [
    "src/**/*.d.ts",
    "src/**/*.js",
    "src/**/*.ts",
    "src/**/*.svelte",
    "src/**/*.svelte-kit",
    "./jest-setup.ts"
  ],
  "exclude": ["node_modules"]
}

我有一个关于笑话中的错误的信息:

代码语言:javascript
复制
Test suite failed to run                                                                                                                          
                                                                                                                                                  
Jest encountered an unexpected token                                                                                                              
                                                                                                                                                  
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.                                                                                                                            
                                                                                                                                                  
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.                   
                                                                                                                                                  
By default "node_modules" folder is ignored by transformers.                                                                                      
                                                                                                                                                  
Here's what you can do:                                                                                                                           
 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
 • 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/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation

Details:

/home/dev/src/iroco-app-client/node_modules/carbon-icons-svelte/lib/Information32/Information32.svelte:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){<script>
                                                                                  ^

SyntaxError: Unexpected token '<' 

   9 |   import { createPopper } from '@popperjs/core';
  10 |   import Information32 from 'carbon-icons-svelte/lib/Information32/Information32.svelte';
> 11 |
     | ^

我加入了jest.config.test.cjs

代码语言:javascript
复制
transformIgnorePatterns: ["<rootDir>/node_modules/(?!(carbon-icons-svelte))"]

在moduleNameMapper之后,但仍然不起作用。谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-19 08:51:18

在节点16上运行,我将babel更改为cjs,它为我工作,这就是它看起来的样子。

代码语言:javascript
复制
module.export = {
  presets: [['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-typescript']
};

我的jest.config.js

代码语言:javascript
复制
const config = {
  testEnvironment: 'jsdom',
  transform: {
    '^.+\\.js$': 'babel-jest',
    '^.+\\.ts$': 'ts-jest',
    '^.+\\.svelte$': ['svelte-jester', { preprocess: true }]
  },
  transformIgnorePatterns: [
    '<rootDir>/node_modules/(?!(carbon-icons-svelte))',
    '<rootDir>/node_modules/(?!(carbon-components-svelte))'
  ],
  moduleFileExtensions: ['js', 'ts', 'svelte']
};

export default config;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69649069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档