小编hyp*_*ipe的帖子

如何将Mocha和Jest与TypeScript一起使用而不会发生冲突?

我正在尝试在一个项目中使用类型安装Mocha和Jest。我们使用严格的类型检查,所以我得到与冲突的全局类型有关的错误。

我尝试创建模糊的模块声明,仅在types中定义Mocha tsconfig。我一直在尝试删除Jest的声明-但这会有所帮助。禁用严格的类型检查或库检查不是一种选择。

我希望它能正常工作,但出现下一个错误。

node_modules/@types/jest/index.d.ts(29,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'beforeEach' must be of type 'HookFunction', but here has type 'Lifecycle'.
node_modules/@types/jest/index.d.ts(31,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'afterEach' must be of type 'HookFunction', but here has type 'Lifecycle'.
node_modules/@types/jest/index.d.ts(32,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'describe' must be of type 'SuiteFunction', but here has type 'Describe'.
node_modules/@types/jest/index.d.ts(34,13): error TS2403: Subsequent …
Run Code Online (Sandbox Code Playgroud)

mocha.js typescript jstestrunner jestjs

7
推荐指数
1
解决办法
396
查看次数

如何使用 TypeScript 在 Jest 的 globalSetup 函数中使用模块导入 (@)?

使用 JS 挂钩调用 TS setupGlobal 函数(例如在模块导入中使用@app/blablabla.ts),我收到错误Cannot find module '@config/config'。我已经描述过moduleNameMapper并且它在测试中工作,但似乎不起作用setupGlobal。我该如何解决这个问题?

正如我所写,moduleNameMapper已描述并且 Jest 可以理解测试中的那些(在模块中)导入。

笑话配置.js

module.exports = {
  roots: ['<rootDir>/src'],
  transform: {
    '^.+\\.tsx?$': 'ts-jest',
  },
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  moduleDirectories: ['node_modules', 'src'],
  moduleNameMapper: {
    '@tests-suite/(.*)': '<rootDir>/src/tests/tests-suite/$1',
    '@config/(.*)': '<rootDir>/config/$1',
    '@tests/(.*)': '<rootDir>/src/tests/$1',
    '@src/(.*)': '<rootDir>/src/$1',
  },
  globalSetup: "<rootDir>/src/tests/unit/jestGlobalSetup.js"
}

Run Code Online (Sandbox Code Playgroud)

jestGlobalSetup.js

require("ts-node/register");
module.exports = require('./setupTestEnvironment').default;
Run Code Online (Sandbox Code Playgroud)

setupTestEnvironment.ts

module.exports = {
  roots: ['<rootDir>/src'],
  transform: {
    '^.+\\.tsx?$': 'ts-jest',
  },
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
  moduleFileExtensions: ['ts', 'tsx', …
Run Code Online (Sandbox Code Playgroud)

typescript jestjs ts-jest

6
推荐指数
1
解决办法
3206
查看次数

如何使 4.X Typescript 项目与 3.X 等旧版本的 Typescript 兼容?

如何使基于 TS 4.X 构建的软件包与 3.X 兼容?例如,如果我有较新的版本,请使用新功能,否则使用anyunknown或旧版本中支持的任何内容。

是否有可能为此目的使用指令?

javascript compatibility typescript

3
推荐指数
1
解决办法
1657
查看次数