angular单测框架从Karma切换到Jest

git init

git remote add origin git@github.com:yourname:yourproject.git
git push -u origin master

…or create a new repository on the command line

echo "# strict-ng-hero" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:jay-xzj/strict-ng-hero.git
git push -u origin main

…or push an existing repository from the command line

git remote add origin git@github.com:jay-xzj/strict-ng-hero.git
git branch -M main
git push -u origin main

jest配置

删除karma相关东西

  1. remove karma dependencies
// Remove dependencies
npm uninstall karma karma-chrome-launcher karma-coverage karma-jasmine karma-jasmine-html-reporter

//or if you are using yarn as package manager:
yarn remove karma karma-chrome-launcher karma-coverage-istanbul-reporter karma-jasmine karma-jasmine-
  1. 删除karma.config.js
  2. 删除package-lock.json (不然会报错:Jest configuration setupFilesAfterEnv option was not found) 如果报错删除node_modules npm i 然后 npm cache clean --force
  3. 删除 angular.json下面的"test"配置,因为我们不再使用karma了

在karma.config.js同级替换成jest.config.js

const { pathsToModuleNameMapper } = require('ts-jest');
const { compilerOptions } = require('./tsconfig');

module.exports = {
  preset: 'jest-preset-angular',
  roots: ['<rootDir>/src/'],
  testMatch: ['**/+(*.)+(spec).+(ts)'],
  setupFilesAfterEnv: ['<rootDir>/src/test.ts'],
  collectCoverage: true,
  coverageReporters: ['html'],
  coverageDirectory: 'coverage/[project-name]',
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths || {}, {
    prefix: '<rootDir>/'
  })
};

修改 test.ts 改成jest的setup配置

import 'jest-preset-angular/setup-jest';

Object.defineProperty(window, 'CSS', {value: null});
Object.defineProperty(window, 'getComputedStyle', {
  value: () => {
    return {
      display: 'none',
      appearance: ['-webkit-appearance']
    };
  }
});

Object.defineProperty(document, 'doctype', {
  value: '<!DOCTYPE html>'
});
Object.defineProperty(document.body.style, 'transform', {
  value: () => {
    return {
      enumerable: true,
      configurable: true
    };
  }
});

tsconfig.json 和 tsconfig.spec.json

  1. 首先,删除第一行的注释,以为jest不允许json文件里面出现注释
  2. 配置tsconfig.spec.json
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/spec",
    "types": [
      //"jasmine"
      "jest"//1
    ],
    "esModuleInterop": true, // 2
    "emitDecoratorMetadata": true //3
  },
  "files": [
    "src/test.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.spec.ts",
    "src/**/*.d.ts"
  ]
}

  • 1.Register Jest’s type definitions files with the TypeScript compiler.
  • 2.Enable the esModuleInterop option of the TypeScript compiler otherwise, Jest will output a lot of warnings in the console.
  • 3.Enable the emitDecoratorMetadata option of the TypeScript compiler otherwise, Angular’s Dependency Injection won’t work with Jest.

运行测试

可以安装jest runner插件对单个测试进行运行
也可以直接npx jest 运行所有的

参考

主要参考:

Jest configuration 参数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Angular是一个流行的前端框架,用于构建Web应用程序。它由Google开发并维护,采用TypeScript语言编写。Angular提供了一套完整的工具和功能,用于开发现代化的单页应用程序(SPA)和响应式Web应用程序。 Angular的主要特点包括: 1. 组件化架构:Angular使用组件化架构来构建应用程序。每个组件都有自己的模板、样式和逻辑,并可以嵌套在其他组件中,以构建复杂的用户界面。 2. 双向数据绑定:Angular支持双向数据绑定,使得数据的变化可以自动反映在视图中,同时用户的输入也可以自动更新数据模型。 3. 依赖注入:Angular使用依赖注入来管理组件之间的依赖关系。这样可以更好地组织和测试代码,并提高代码的可维护性和可扩展性。 4. 路由和导航:Angular提供了强大的路由和导航功能,可以实现页面之间的无缝切换和导航。 5. 响应式表单:Angular提供了响应式表单功能,可以轻松地处理表单的验证、状态管理和用户输入。 6. 强大的工具集:Angular配备了丰富的工具集,包括开发者工具、测试工具和构建工具,可以提高开发效率和代码质量。 下面是一个简单的Angular组件的示例: ```typescript import { Component } from '@angular/core'; @Component({ selector: 'app-hello', template: ` <h1>Hello, {{ name }}!</h1> <button (click)="changeName()">Change Name</button> `, }) export class HelloComponent { name = 'Angular'; changeName() { this.name = 'World'; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值