Jest离子6角14 Jest遇到了一个意想不到的令牌-如何设置Jest

7xllpg7q  于 2023-11-15  发布在  Jest
关注(0)|答案(1)|浏览(152)

我面对离子6角14 Jest设置错误@awrsome-cordova库。
Jest.config.js

const { pathsToModuleNameMapper } = require('ts-jest');
globalThis.ngJest = {
  skipNgcc: false,
  tsconfig: 'tsconfig.spec.json',
};

const esModules = ['@awesome-cordova-plugins',
'@ionic','@ionic/core','@ionic/angular','@ionic-native','@angular/core','@angular','aws-amplify' ];
module.exports = {
  preset: 'jest-preset-angular',
  testEnvironment: 'jsdom',
  moduleNameMapper:{
     "^aws-amplify": "<rootDir>/node_modules/aws-amplify/dist/aws-amplify.js",
          
  },
  setupFilesAfterEnv: ['<rootDir>/src/setup-jest.ts'],
  collectCoverage: true,
  testMatch: ['**/+(*.)+(spec).+(ts)'],
    testPathIgnorePatterns: [
    "<rootDir>/node_modules/",
    "<rootDir>/dist/"
  ],
  transform: {
    '^.+\\.(ts|js|mjs|svg)$': 'jest-preset-angular',
  },
  moduleFileExtensions: [
    "ts",
    "tsx",
    "js",
    "jsx",
    "json",
    "node",
    ".html"
  ],
  transformIgnorePatterns: [
    `<rootDir>/node_modules/(?!${esModules.join('|')})`,
  ],
};

字符串
Setup-jest.ts

import 'jest-preset-angular';


Babel.config.js

module.exports = function (api) {
process.env.NODE_ENV === "development" ? api.cache(false) : api.cache(true);
const presets = [
  [
    "@babel/preset-env",
    {
      targets: {
        node: "current",
      },
    },
  ],
  "@babel/preset-typescript",
];
const plugins = ["@babel/plugin-transform-modules-commonjs"];
return {
  presets,
  plugins,
};


};
这是我的错误:npm run test
email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)测试笑话
ts-jest[config](WARN)消息TS 151001:如果您遇到与导入相关的问题,应考虑在TypeScript配置文件中将esModuleInterop设置为true(通常为tsconfig.json)。https://blogs.msdn.microsoft.com/typescript/2018/01/31/announcing-typescript-2-7/#easier-ecmascript-module-interoperability有关详细信息,请参阅www.example.com。ts-jest[ts-jest-Transformer](WARN)有未知的文件类型要编译(档案:app\src\app\pages\side-menu-exp\side-menu-exp.page.html)。要修复此问题,在你的Jest配置中修改transform键,其值为ts-jest,这样它就不再匹配这种文件了。失败src/app/pages/side-menu-exp/side-menu-exp.page.spec.ts ●测试套件无法运行

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.
 • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
 • 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:

SyntaxError: \app\src\app\pages\side-menu-exp\side-menu-exp.page.html: Support for the experimental syntax 'jsx' isn't currently enabled (1:1):

> 1 | <ion-content>
    | ^
  2 |   <!-- side hand menu starts -->
  3 |   <div class="side-nav-sect menuOpen" #sideMenu_content (click)="backdropDismiss($event)">
  4 |     <div class="sde-nav-wrp">

Add @babel/preset-react (https://github.com/babel/babel/tree/main/packages/babel-preset-react) to the 'presets' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://github.com/babel/babel/tree/main/packages/babel-plugin-syntax-jsx) to the 'plugins' section to enable parsing.

  19 | @Component({
  20 |   selector: 'app-side-menu-exp',
> 21 |   templateUrl: './side-menu-exp.page.html',

pbwdgjma

pbwdgjma1#

This answer将为您指明正确的方向。
确保你已经安装了所有需要的包。你需要jestts-jest@types/jest@angular-builders/jest。你可以像这样使用NPM安装:
npm install --save-dev jest ts-jest @types/jest @angular-builders/jest
按照上面的链接中的答案,你会很好地去。

相关问题