我正在将我们的业力测试迁移到旧的 Angular 9 应用程序中的 Jest,并且面临一些测试失败的问题,并出现以下类型的异常:
类型错误:类构造函数 SomeComponentClass 无法在没有“new”的情况下调用
我遵循 Jest 设置指南并参考了其他一些独立指南,我的设置如下:
笑话配置.js
module.exports = {
preset: "jest-preset-angular",
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
globals: {
'ts-jest': {
tsconfig: '<rootDir>/src/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$',
},
},
coverageDirectory: 'coverage',
coverageReporters: ["html", "text-summary", "json-summary", "cobertura"],
transform: {
'^.+\\.(ts|js|html)$': 'jest-preset-angular',
},
snapshotSerializers: [
'jest-preset-angular/build/serializers/no-ng-attributes',
'jest-preset-angular/build/serializers/ng-snapshot',
'jest-preset-angular/build/serializers/html-comment',
],
testEnvironment: "jsdom",
transformIgnorePatterns: [
"node_modules/(?!@o3|@al|ui-metadata)"
],
testPathIgnorePatterns: [
"<rootDir>/node_modules/",
"<rootDir>/dist/",
"<rootDir>/cypress/",
"<rootDir>/src/test.ts/"
],
reporters: [ "default" ],
testMatch: [
"<rootDir>/src/exposures/**/*.spec.ts"
]
};
Run Code Online (Sandbox Code Playgroud)
测试设置.ts
import 'jest-preset-angular/setup-jest';
import '@angular/localize/init';
Object.defineProperty(window, 'CSS', { value: null }); …Run Code Online (Sandbox Code Playgroud) 尝试使用这些说明将此项目转换为玩笑。除了使用配置的文件外,我一切正常:paths
"paths": {
"@fs/*": ["./src/*"],
"@test/*": ["./test/*"]
}
Run Code Online (Sandbox Code Playgroud)
看起来好像在运行测试时导入语句没有解析并记录下来:
Cannot find module '@fs/container/validation/ValidationContext' from 'Core.spec.ts'
1 | import { ValidationOptions } from "@fs/container/validation/ValidationOptions";
> 2 | import { ValidationContext } from "@fs/container/validation/ValidationContext";
| ^
3 | import { ValidationContainer } from "@fs/container/validation/ValidationContainer";
4 |
5 | import { Core1 } from "@test/core/Core1";
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17)
at Object.<anonymous> (test/core/Core.spec.ts:2:1)
Run Code Online (Sandbox Code Playgroud)
有没有办法让 jest/ts-jest 包含@paths解析导入的同时?
在 Gitlab-CI 环境 2 中,我们的 Jest 测试以Cannot find module.
奇怪的是它可以在我本地的 Win10 机器上运行——即使我在类似的 docker-container ( node 12.12.0) 中运行测试。
这是控制台输出:
FAIL apps/server/src/domain/dashboard/permission-group.service.spec.ts
Test suite failed to run
Cannot find module '@cm/utils-server' from 'license.service.ts'
9 | isLicenseFileContent,
10 | LicenseStatus,
> 11 | parseLicenseInfo
| ^
12 | } from '@cm/license-shared';
13 | import { ExitCode } from '../../util/exit-codes';
14 | import { readFile } from '@cm/utils-server';
at Resolver.resolveModule (../../node_modules/jest-resolve/build/index.js:259:17)
at Object.<anonymous> (src/domain/license/license.service.ts:11:24)
Run Code Online (Sandbox Code Playgroud)
我不确定如何正确解释此输出:
permission-group.service.spec.ts: 这是失败的测试Cannot find module …我有一个对象配置,我在其中向函数发送函数回调。
test('should create a ComponentMockwith the needed config', () => {
const config: MyConfig = {
// more options
myCallback: jasmine.anything()
};
sut.doYourThing(config);
expect(ComponentMock.create).toHaveBeenCalledWith(config);
});
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,更新到最新的 jest/jasmine 后,我收到以下错误myCallback: jasmine.anything():
如何修复类型“AmetryMatcher”不可分配给类型“() => void”。在茉莉花/笑话中
如果我忽略该行,@ts-ignore我的测试将继续工作,但我想知道 linter 为什么这么说以及如何修复它,因为我真的不明白它。
我在 Node.Js Typescript 项目中使用 Jest 和 Visual Studio 2019,并且发生了一些我不明白的事情,我将不胜感激您提供一些指导,以便我了解发生了什么。
我在 Visual Studio 的 Node.js 项目中设置了 Jest。npm 软件包 jest、ts-jest 和 jest-editor-support 已安装。Typescript 文件在 VS 2019 中成功编译。
当我从命令行使用 Jest 时,所有测试都运行良好,无论是 .ts 文件中的测试还是编译后的 .js 文件中的测试,与我在 .ts 文件中设置的目标版本无关tsconfig.json。没问题。
然而,当我使用 Visual Studio 2019 测试资源管理器时,有 2 件奇怪的事情:
如果目标版本低于 中的“ES6” tsconfig.json,则构建失败,并且我看到babylon 解析器(由
)在和语句jest-editor-support上引发解析器错误
。foreachmap
即使使用“ES6”或更高版本的目标版本,当我使用 Jest 的 .each 函数时,包含这些函数的测试也无法正确运行。它们显示为“失败”,但在“测试详细信息摘要”窗口中,没有指向其他输出的链接。
由于 Jest 从命令行运行没有问题,这意味着通过 Visual Studio 2019 的测试资源管理器重新运行测试时会出现一些不同的情况。如何在测试资源管理器中以相同的方式成功运行测试?(没有明显发生的绕道巴比伦的情况)。我正在寻找测试资源管理器中失败的测试运行的解决方案,以及了解正在发生的情况。我对 Jest 还很陌生。
(顺便说一句,安装了 Jest / Typescript 的其他项目类型也会出现同样的问题)
我正在尝试编写一个测试异步方法的 jest 测试用例,我想传入done()参数,以便 jest 在结束测试之前等待它被触发,但是,我不确定把它放在哪里。
有任何想法吗?
const testcases = [
[
'Crew',
[1,2,3],
Enum.Level1
],
[
'Staff',
[4,5,6],
Enum.Level2
]
];
test.each(testcases )(
'Should be able to load differing cases %p',
(
typeName: string,
initalVals: string[],
type: LevelType
) => {
// some call that updates mobx store state
when(
() => mobxstoreProperty.length == initalVals.length,
() => {
// my assertions
done();
}
);
}
);
Run Code Online (Sandbox Code Playgroud)
对于单个笑话测试,我可以这样做:
test('my single test', done => {
// some call that updates …Run Code Online (Sandbox Code Playgroud) 我正在使用react+relay+jest+typescript,但我的测试失败了,因为它们无法理解graphql。抛出的错误是:
Error: Uncaught [Invariant violation: graphql: Unexpected invocation at runtime.
Either the babel transform was not set up, or it failed to identify this call
site. Make sure it is being used verbatim as `graphql`.]
Run Code Online (Sandbox Code Playgroud)
我在 jest.config 中使用 ts-jest。并且它在运行应用程序时按预期工作。在运行测试时它失败了。
考虑到这个相对人为的类从 Elasticsearch(或任何与此相关的数据存储)获取一条记录:
export default class UserRepoImpl implements UserRepo {
constructor(private readonly esClient: ElasticsearchClient) {}
public async getUser(id: string): Promise<User> {
const response = await this.esClient.request('GET', `/users/_doc/${id}`);
return response._source;
}
}
Run Code Online (Sandbox Code Playgroud)
如何编写一个测试来UserRepoImpl为我实例化 a 但插入一个模拟的 ElasticsearchClient作为构造函数的参数?
我正在尝试将 jest.mock 包装到一个函数中,以便我可以在多个文件中重用它。我的用例是反应钩子,我正在使用打字稿。目前我的测试文件看起来像这样:
测试.tsx
//some imports
jest.mock('../path/to/hoook/whatever', () => ({
useWhatever: jest.fn()
}))
const mockResult = () => {
return ({
some stuff
};
};
describe('Component', () => {
let wrapper;
beforeEach(() => {
mocked(useWhatever).mockClear();
mocked(useWhatever).mockImplementation(() => mockResult);
wrapper = shallow(
<Component />
);
});
//tests below
});
Run Code Online (Sandbox Code Playgroud)
这工作得很好,但是这个无论什么钩子都会被其他组件使用,我正在尝试包装 jest.mock 以在其他测试中重用。我想做的事情看起来像这样:
模拟.ts
export const mockWhatever = () => jest.mock('../path/to/hoook/whatever', () => ({
useWhatever: jest.fn()
}))
export const mockResult = () => {
return ({
some stuff
};
};
Run Code Online (Sandbox Code Playgroud)
测试.tsx
//some …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用打字稿运行笑话,但出现以下错误。该项目在 webpack 和 ts-node 中运行良好。由于某种原因,我无法让它与笑话一起工作。
\nFAIL src/__tests__/classes/Utils.spec.ts\n\xe2\x97\x8f Test suite failed to run\n\nCannot find module \'typescript\'\n\nRequire stack:\n- /Users/myuser/repos/project/node_modules/ts-jest/dist/config/config-set.js\n- /Users/myuser/repos/project/node_modules/ts-jest/dist/ts-jest-transformer.js\n- /Users/myuser/repos/project/node_modules/ts-jest/dist/index.js\n- /Users/myuser/repos/project/node_modules/@jest/transform/build/ScriptTransformer.js\n- /Users/myuser/repos/project/node_modules/@jest/transform/build/index.js\n- /Users/myuser/repos/project/node_modules/jest-runtime/build/index.js\n- /Users/myuser/repos/project/node_modules/@jest/core/build/cli/index.js\n- /Users/myuser/repos/project/node_modules/@jest/core/build/jest.js\n- /Users/myuser/repos/project/node_modules/jest/node_modules/jest-cli/build/cli/index.js\n- /Users/myuser/repos/project/node_modules/jest/node_modules/jest-cli/bin/jest.js\n- /Users/myuser/repos/project/node_modules/jest/bin/jest.js\n- /usr/local/lib/node_modules/jest/node_modules/import-local/index.js\n- /usr/local/lib/node_modules/jest/bin/jest.js\nRun Code Online (Sandbox Code Playgroud)\n这是配置。我已经尝试了 root 属性和 moduleNameMapper 的许多变体,但无论我在配置中进行什么更改,错误消息都是完全相同的。
\nFAIL src/__tests__/classes/Utils.spec.ts\n\xe2\x97\x8f Test suite failed to run\n\nCannot find module \'typescript\'\n\nRequire stack:\n- /Users/myuser/repos/project/node_modules/ts-jest/dist/config/config-set.js\n- /Users/myuser/repos/project/node_modules/ts-jest/dist/ts-jest-transformer.js\n- /Users/myuser/repos/project/node_modules/ts-jest/dist/index.js\n- /Users/myuser/repos/project/node_modules/@jest/transform/build/ScriptTransformer.js\n- /Users/myuser/repos/project/node_modules/@jest/transform/build/index.js\n- /Users/myuser/repos/project/node_modules/jest-runtime/build/index.js\n- /Users/myuser/repos/project/node_modules/@jest/core/build/cli/index.js\n- /Users/myuser/repos/project/node_modules/@jest/core/build/jest.js\n- /Users/myuser/repos/project/node_modules/jest/node_modules/jest-cli/build/cli/index.js\n- /Users/myuser/repos/project/node_modules/jest/node_modules/jest-cli/bin/jest.js\n- /Users/myuser/repos/project/node_modules/jest/bin/jest.js\n- /usr/local/lib/node_modules/jest/node_modules/import-local/index.js\n- /usr/local/lib/node_modules/jest/bin/jest.js\nRun Code Online (Sandbox Code Playgroud)\nmodule.exports = {\n preset: \'ts-jest\',\n testEnvironment: \'node\',\n roots: [\'<rootDir>/src\'],\n transform: {\n \'^.+\\\\.tsx?$\': \'ts-jest\',\n },\n …Run Code Online (Sandbox Code Playgroud) jestjs ×10
ts-jest ×10
typescript ×5
tsconfig ×2
angular ×1
babel-jest ×1
jasmine ×1
node.js ×1
nrwl-nx ×1
reactjs ×1
relayjs ×1
ts-node ×1
unit-testing ×1