在开发过程中,我们偶尔会使用skip或only调试特定的测试或测试套件。一不小心,我们可能会忘记恢复案例并推送 PR 代码。我正在寻找一种方法来检测或自动运行所有测试,甚至是skip我们only的 CI 管道中的测试(使用 Github 操作)。无论是哪种情况都可以,如下所示。
skip当有或测试时测试失败only。skip甚至对和运行所有测试only。非常感谢任何帮助。
运行 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) 我正在尝试将我的 Angular 13 NX 工作区升级到 Angular 14,但不知何故我无法让我的笑话单元测试正常工作。我从零开始尝试了以下方法:
\n安装@angular/cli并nx全局:
npm install --global @angular/cli@latest\nnpm install --global nx@latest\nRun Code Online (Sandbox Code Playgroud)\n在不带空格的路径中打开并cmd
\nnpx create-nx-workspace\n> test\n> angular\n> demo\n> scss\nRun Code Online (Sandbox Code Playgroud)\n结果:
\n\n\nNx 正在创建您的 v13.10.5 工作区
\n
所以现在我可以迁移到最新的 NX 工作区版本
\ncd test\nnx migrate latest\nRun Code Online (Sandbox Code Playgroud)\n版本@angular/cli仍然是13,所以我在根中将其更改package.json为14.0.1(nx删除版本注释~并^...)
npm install不成功。在工作区中jest-preset-angular@11.1.2需要这是一个固定的依赖项:@angular-devkit/build-angular@">=0.1002.4"@angular-devkit/build-angular@"14.0.1"
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) 我正在尝试设置一个非常简单的 NPM 代码库,我可以用它来驱动 API 来进行一些测试。
\n导致错误的代码行只是import fetch from "node-fetch";,请参阅代码。
对于设置/配置,我正在关注此存储库(建议使用更好的模板将不胜感激):https://github.com/bromix/typescript-jest-example
\nnode-fetch当我尝试在代码中使用该库(在目录test.ts中的文件/test或目录.ts中的文件中/src)时,出现以下错误:
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) 我正在为我的 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
在使用 Babel、Typescript、React 和 Jest 测试某些组件时,我遇到了 Jest 问题。
在测试 React/Typescript 组件时,我得到了SyntaxError: Cannot use import statement outside a module.
我尝试过的事情:
这里是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) 我正在尝试将 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) 我试图解决这个问题已经走进了死胡同。我正在使用以下 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,我也会收到错误。
由于这个问题,我无法完成我的任务,因此我将不胜感激。
我们在项目中使用“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) 每当在 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是在不应该的地方并行化测试套件,但我不知道为什么它会突然开始。
ts-jest ×10
jestjs ×7
typescript ×5
angular ×3
unit-testing ×2
angular14 ×1
babeljs ×1
es6-modules ×1
javascript ×1
node-fetch ×1
node.js ×1
npm ×1
nrwl-nx ×1
reactjs ×1
vue.js ×1
vuejs2 ×1