标签: ts-jest

Jest 在 CI 中运行所有测试(仅包含/跳过)

在开发过程中,我们偶尔会使用skiponly调试特定的测试或测试套件。一不小心,我们可能会忘记恢复案例并推送 PR 代码。我正在寻找一种方法来检测或自动运行所有测试,甚至是skip我们only的 CI 管道中的测试(使用 Github 操作)。无论是哪种情况都可以,如下所示。

  1. skip当有或测试时测试失败only
  2. skip甚至对和运行所有测试only

非常感谢任何帮助。

jestjs ts-jest

5
推荐指数
1
解决办法
1356
查看次数

类型 X 是 2 个模块声明的一部分:Y 和 Y!...在 Angular Jest 测试中

运行 Angular Jest 测试时,出现以下错误:

"Type InButtonComponent is part of the declarations of 2 modules: InButtonModule and InButtonModule! Please consider moving InButtonComponent to a higher module that imports InButtonModule and InButtonModule. You can also create a new NgModule that exports and includes InButtonComponent then import that NgModule in InButtonModule and InButtonModule."
Run Code Online (Sandbox Code Playgroud)

我以前遇到过“2 个模块的声明”问题,通常很容易解决;不要在两个不同的模块中声明相同的组件。但是,在这种情况下,我不会InButtonComponent在两个不同的模块中声明。就像 Angular 抱怨我在同一个模块中声明了两次组件?该错误消息对我来说没有任何意义。

无论我是为产品构建还是运行开发服务器,我都不会在应用程序中收到此错误。然而,当我运行 Jest 测试时,它们失败了。

如果有帮助的话,这里是一些可能涉及的文件:

// button.component.ts  single file SCAM
// ...
@NgModule({
  declarations: [InButtonComponent],
  imports: [CommonModule],
  exports: [InButtonComponent],
})
export class InButtonModule {}
Run Code Online (Sandbox Code Playgroud)
// …
Run Code Online (Sandbox Code Playgroud)

typescript jestjs angular ts-jest

5
推荐指数
0
解决办法
123
查看次数

NX 工作区 - 角度 14 - 玩笑不工作

我正在尝试将我的 Angular 13 NX 工作区升级到 Angular 14,但不知何故我无法让我的笑话单元测试正常工作。我从零开始尝试了以下方法:

\n

安装@angular/clinx全局:

\n
npm install --global @angular/cli@latest\nnpm install --global nx@latest\n
Run Code Online (Sandbox Code Playgroud)\n

在不带空格的路径中打开并cmd

\n
npx create-nx-workspace\n> test\n> angular\n> demo\n> scss\n
Run Code Online (Sandbox Code Playgroud)\n

结果:

\n
\n

Nx 正在创建您的 v13.10.5 工作区

\n
\n

所以现在我可以迁移到最新的 NX 工作区版本

\n
cd test\nnx migrate latest\n
Run Code Online (Sandbox Code Playgroud)\n

版本@angular/cli仍然是13,所以我在根中将其更改package.json14.0.1(nx删除版本注释~^...)

\n

npm install不成功。在工作区中jest-preset-angular@11.1.2需要这是一个固定的依赖项:@angular-devkit/build-angular@">=0.1002.4"@angular-devkit/build-angular@"14.0.1"

\n
npm ERR! Found: @angular-devkit/build-angular@13.3.7\nnpm ERR! node_modules/@angular-devkit/build-angular\nnpm ERR!   dev @angular-devkit/build-angular@"14.0.1" from the …
Run Code Online (Sandbox Code Playgroud)

ts-jest nrwl-nx angular14

5
推荐指数
1
解决办法
7959
查看次数

如何将 Node-fetch 与 Jest 和 Typescript 结合使用?

我正在尝试设置一个非常简单的 NPM 代码库,我可以用它来驱动 API 来进行一些测试。

\n

导致错误的代码行只是import fetch from "node-fetch";,请参阅代码

\n

对于设置/配置,我正在关注此存储库(建议使用更好的模板将不胜感激):https://github.com/bromix/typescript-jest-example

\n

node-fetch当我尝试在代码中使用该库(在目录test.ts中的文件/test或目录.ts中的文件中/src)时,出现以下错误:

\n
FAIL  test/Api.test.ts\n  \xe2\x97\x8f Test suite failed to run\n\n    Jest encountered an unexpected token\n\n    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.\n\n...\n\n    Details:\n\n    C:\\ardc\\rats\\node_modules\\node-fetch\\src\\index.js:9\n    import http from \'node:http\';\n    ^^^^^^\n\n    SyntaxError: Cannot …
Run Code Online (Sandbox Code Playgroud)

typescript jestjs es6-modules node-fetch ts-jest

5
推荐指数
1
解决办法
6117
查看次数

在玩笑单元测试中引用计算属性

我正在为我的 Vuejs 2 (Vuetify) 组件编写一个笑话单元测试。设置非常简单。

在组件内部,我有一个 prop,它是一个带有嵌套字段的对象:

props: ['testObject']

我有一个计算属性,它从该道具返回一个字段:

computed: {
    myField() {
      return this.testObject.testField;
    }
}
Run Code Online (Sandbox Code Playgroud)

我写了一个 Jest 测试:

wrapper = mount(MyComponent, {
            mocks: {
                propsData: {
                    testObject: { testField: 'My Test Value' }
                }
            },
        });

it("should return a test field", () => {
    const field = wrapper.vm.$options.computed.myField();
    expect(field).toBe('My Test Value');
});

Run Code Online (Sandbox Code Playgroud)

当我运行测试时,我得到以下信息:

TypeError: Cannot read property 'testField' of undefined
Run Code Online (Sandbox Code Playgroud)

所以它调用计算属性,但 testObject 未定义。

知道发生了什么事吗?

@vue/test-utils:1.3.0 @vue/vue2-jest:28.0.1 @types/jest:28.1.3 笑话:28.1.1

vue.js jestjs vuejs2 vue-test-utils ts-jest

5
推荐指数
1
解决办法
1370
查看次数

开玩笑:语法错误:无法在 React/Typescript 项目的模块外部使用 import 语句

在使用 Babel、Typescript、React 和 Jest 测试某些组件时,我遇到了 Jest 问题。

在测试 React/Typescript 组件时,我得到了SyntaxError: Cannot use import statement outside a module.

我尝试过的事情

  1. 使用 babel 插件(参见下面的 babel 配置)
  2. 使用ts-jest
  3. 尝试了文档ts-jest中的 ESM 支持
  4. 阅读这篇这篇这篇有类似问题的帖子

这里是jest.config.js

const ignores = ['/node_modules/'];

module.exports = {
    preset: 'ts-jest',
    roots: ['<rootDir>'],
    modulePaths: [
        "<rootDir>/src"
    ],
    moduleDirectories: [
        "node_modules",
    ],
    transformIgnorePatterns: [...ignores],
    transform: {
        '^.+\\.(ts|tsx)?$': 'ts-jest',
        '^.+\\.(gif|svg|ico)$': '<rootDir>/svgTransform.js',
    },
    testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.js?$',
    moduleFileExtensions: ['tsx', 'js', 'ts'],
    moduleNameMapper: { …
Run Code Online (Sandbox Code Playgroud)

unit-testing node.js jestjs babeljs ts-jest

5
推荐指数
1
解决办法
1847
查看次数

在 Angular 14 + Ionic 6 项目中设置 Jest

我正在尝试将 Jest 测试框架设置到我的项目中,该项目使用 Angular 14 和 Ionic 6,以及其他可能存在冲突的插件,例如 firebase 和 ngrx。

\n

我一直主要关注Tim Deschryver 教程和其他一些教程,甚至从堆栈溢出中复制和粘贴一些关于我的 Jest 测试错误的代码,但没有任何效果。即使我尝试重新开始删除所有软件包和修改并重新开始,但没有运气。

\n

我在这里有更新的存储库https://github.com/neil89/igloo(它非常易于管理)。但总的来说,我的主要修改是这些:

\n

包.json

\n
{\n  "name": "igloo",\n  "version": "0.0.1",\n  "author": "Ionic Framework",\n  "homepage": "https://ionicframework.com/",\n  "scripts": {\n    "ng": "ng",\n    "start": "ng serve",\n    "build": "ng build",\n    "test": "jest",\n    "lint": "ng lint",\n    "e2e": "ng e2e"\n  },\n  "private": true,\n  "dependencies": {\n    "@angular/common": "^14.0.0",\n    "@angular/core": "^14.0.0",\n    "@angular/fire": "^7.4.1",\n    "@angular/forms": "^14.0.0",\n    "@angular/platform-browser": "^14.0.0",\n    "@angular/platform-browser-dynamic": "^14.0.0",\n    "@angular/router": "^14.0.0",\n    "@briebug/jest": "^1.3.1",\n    "@ionic/angular": "^6.2.6",\n    "@ngrx/effects": "^14.3.1",\n    "@ngrx/store": …
Run Code Online (Sandbox Code Playgroud)

unit-testing typescript ionic-framework angular ts-jest

5
推荐指数
1
解决办法
1869
查看次数

带有 .js 扩展名的 TypeScript Jest 导入导致错误:找不到模块

我试图解决这个问题已经走进了死胡同。我正在使用以下 TypeScript 配置:

{
    "compilerOptions": {
        "module": "es2022",
        "moduleResolution": "nodenext",
        "target": "es2017",
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,
        "skipLibCheck": true,
        "noImplicitAny": true,
        "outDir": "./dist",
        "rootDir": "./src",
        "typeRoots": [
            "./src/types", "./node_modules/@types"],
        "allowJs": true,
        "strictFunctionTypes": true,
        "noImplicitReturns": true
    },
    "include": ["./src/**/*"],
    "exclude": ["node_modules"],
    "ts-node": {
        "esm": true,
        "experimentalSpecifierResolution": true
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的, moduleResolution 设置为nodenext,因此我必须在导入时显式添加文件扩展名,如下所示: import ServerError from '../models/Errors/ServerError.js';。否则,我会收到一条错误消息,指出找不到该模块。

一切工作正常,但是当我启动测试时出现错误: Cannot find module '../models/Errors/ServerError.js' from '../src/services/usersService.ts'。所以基本上 jest 试图找到文件 ServerError.js,但它不存在,因为所有文件都有 .ts 扩展名,所以它应该是 ServerError.ts。如果我尝试将文件中的 .js 更改为 .ts,我也会收到错误。

由于这个问题,我无法完成我的任务,因此我将不胜感激。

typescript jestjs ts-jest

5
推荐指数
2
解决办法
3941
查看次数

在同一个项目中使用@jest-environment jsdom和@jest-environment节点[Jest-ReactJS]

我们在项目中使用“Jest with React Testing Library”进行单元测试。在同一个项目中,我们有一些情况需要“@jest-environment jsdom”,有些情况需要@jest-environment节点。我们如何在测试用例文件中切换它。

我们有以下 jest.config.js 文件:

 /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
    module.exports = {
      preset: 'ts-jest',
      testEnvironment: 'node'
    };
Run Code Online (Sandbox Code Playgroud)

我尝试过以下方法,但根据我的经验,它不起作用。

/**
 * @jest-environment jsdom
 */

test('use jsdom in this test file', () => {
  const element = document.createElement('div');
  expect(element).not.toBeNull();
});

/**
 * @jest-environment node
 */

test('do not use jsdom in this test file', () => {
  console.log(window); // this will fail as node doesn't have a window object
});
Run Code Online (Sandbox Code Playgroud)

javascript reactjs jestjs ts-jest

5
推荐指数
0
解决办法
810
查看次数

每次测试都使用 EBUSY 进行 Jest 锁定

总结一下问题

每当在 Jest 中为 Angular 项目运行测试时,我都会遇到以下问题:

EBUSY: resource busy or locked, open 'C:\Users\<username>\AppData\Local\Temp\jest\ts-jest\1b\56fbb16412d9bba7787f9ba7186e4b9972adfb\e9a8ee471bc676fe755c57379268f21a1390a7a4

    > 1 | import 'jest-preset-angular/setup-jest';
        | ^
      2 |

      at NgJestTransformer.TsJestTransformer.getCacheKey (../../node_modules/ts-jest/dist/legacy/ts-jest-transformer.js:341:40)
      at Object.<anonymous> (../../node_modules/rxjs/dist/cjs/index.js:33:14)
      at Object.<anonymous> (../../node_modules/@angular/core/fesm2015/core.mjs:7:69)
      at Object.<anonymous> (../../node_modules/@angular/core/fesm2015/testing.mjs:7:1159)
      at Object.<anonymous> (../../node_modules/jest-preset-angular/setup-jest.mjs:2:28)
      at Object.<anonymous> (src/setup-jest.ts:1:1)
Run Code Online (Sandbox Code Playgroud)

...通过命令运行:nx run <project>:test

这些工作一直持续到周一。

如果我运行以下任一命令,测试就会运行:

  • 骑手中的单独测试/测试套件
  • 终端中的单个测试套件通过:nx run <project>:test --project=<project> --testFile=src/app/services/plotly-layout.service.spec.ts

描述一下你已经尝试过的事情

我发现这些 EBUSY 问题相当多,但通常是以下其中一项有效:

  • 关闭我正在使用的终端
  • 结束骑手
  • 重新启动我的电脑

这些都不起作用。

鉴于锁定在Temp文件夹上,我尝试删除整个Temp/jest文件夹,但它只是重新生成56fbb16412d9bba7787f9ba7186e4b9972adfb\e9a8ee471bc676fe755c57379268f21a1390a7a4文件并在下一次测试运行时立即锁定。

以前有人遇到过这个吗?我的假设是,这jest是在不应该的地方并行化测试套件,但我不知道为什么它会突然开始。

npm typescript angular ts-jest

5
推荐指数
0
解决办法
477
查看次数