我正在使用Jest模拟一个React组件,但是TypeScript没有识别被模拟的方法并给出一个TS2339错误。
这是我的测试:
jest.mock('./features/News/News');
describe('<App />', () => {
it('should render the News page on default route', () => {
// The following line is giving an error:
// TS2339: Property
当我使用TypeScript编译器编译测试并使用Jest模拟时,我经常收到来自tsc的错误,例如:
error TS2339: Property 'mockImplementationOnce' does not exist on type
'typeof readFile'.
从这个最小的测试:
jest.mock('fs');
// Run before the imports but does not alter types :(
import { readFile } from 'fs';
i
我正在向一个使用TypeScript的compilerOptions.paths项目中添加单元测试,我需要模拟一个导入。
我遇到一个问题,在这个问题上,jest无法解决要模拟的模块
FAIL logic/index.test.ts
● Test suite failed to run
Cannot find module '@lib/foo' from 'logic/index.test.ts'
我使用的是ts-jest,它在导入中添加了对路径的支持,但看起来我需要为模拟执行额外的步骤
解决这条路的正确方法是什么?
简化案例
{
"
我想模拟一个模块进行测试。一切正常,但我必须将相同的代码复制/粘贴到每个测试文件中。我怎样才能使它更易于维护?
(这是在TypeScript项目中使用Babel和Next.js。)
生产代码如下所示:
import { useRouter } from 'next/router';
// Then call the hook in a React component:
const router = useRouter();
测试代码:
// I need to access these mocks in the tests
const mockRouterPush = j
我试图用Jest来模拟一个TypeScript类,而且我显然是在做一些事情,因为接收到以下错误:
error TS2743: No overload expects 1 type arguments, but overloads do exist that expect either 0 or 2 type arguments.
这是我的密码:
Foo.ts
export default class Foo {
bar(): number {
return Math.random()
}
}
Foo.test.ts
import Foo from './Foo'
我正在使用Jest和react-testing-library,并且在模拟推进计时器时遇到了这个警告: console.error
Warning: An update to TimerProvider inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events
我在TypeScript中使用Jest (使用ts-jest)。这是我的测试主题,它使用dotenv库:
import * as dotenv from 'dotenv';
export class TestSubject {
public function test() {
console.log(dotenv); // Here I debug if I'm using a mock or not
}
}
我只想为了一个特定的测试而模拟dotenv库,但是我被困住了。
这是我的测试套件:
import { TestSubject } from
我试图用typescript和jest正确地模拟我的i18n文件,但当我运行测试时,我总是得到这个错误:
TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
__tests__/__mock__/react-i18next.ts:1:1 - error TS1208: All files must be modules when the '--isolatedModules' flag is provided.
1 jest