Runtime error in jest - TypeError: Class extends value undefined is not a constructor or null
Runtime error in jest - TypeError: Class extends value undefined is not a constructor or null
我有一个这样定义的打字稿文件:
// myType.tsx
import { Foo } from 'foo';
[...]
export interface MyType extends IDisposable {
[...]
}
export abstract class MyTypeBase implements MyType {
innerFoo: Foo | null | undefined;
get foo(): Foo {
return this.innerFoo;
}
[...]
}
我正在尝试测试依赖于此 MyType
的另一个 class,我想模拟 foo()
getter.
在我的测试文件中,一旦我将以下内容添加到文件中:
[...]
import { MyType } from 'path/to/MyType';
[...]
jest.mock('path/to/MyType', () => {
return jest.fn().mockImplementation(() => {
return {foo: () => mockFoo}; //mockFoo is defined earlier in the file.
})
});
我在 运行 测试时出错:
Class extends value undefined is not a constructor or null
这仅在我包含 jest.mock(...)
行时发生。
从其他相关问题来看,我发现这可能是由循环依赖引起的,但我看不出这是如何通过调用 jest.mock
引入的?我可以看到当我注释掉这段代码时命中的其他断点没有被命中,所以看起来好像添加这个模拟导致我的测试因为这个原因提前失败。
有人遇到过这个吗?
问题在the documentation中解释:
A limitation with the factory parameter is that, since calls to jest.mock() are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'. It's up to you to guarantee that they will be initialized on time! For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration
使用 mockFoo
变量名允许在 jest.mock
工厂函数中使用它,风险自负。
mockFoo is defined earlier in the file
这是不正确的,因为 jest.mock
提升在各自的导入之上,但它引用的变量在评估模拟模块时未定义。对于急切评估的模拟模块,需要在工厂内部定义模拟。如果需要在测试中访问它,可以通过模块公开它。
foo
是 属性 访问器,但被模拟为函数。
模拟模块也是 CommonJS,这将阻止在大多数设置中正确映射命名导入,模拟模块中应该有 __esModule: true
。
我有一个这样定义的打字稿文件:
// myType.tsx
import { Foo } from 'foo';
[...]
export interface MyType extends IDisposable {
[...]
}
export abstract class MyTypeBase implements MyType {
innerFoo: Foo | null | undefined;
get foo(): Foo {
return this.innerFoo;
}
[...]
}
我正在尝试测试依赖于此 MyType
的另一个 class,我想模拟 foo()
getter.
在我的测试文件中,一旦我将以下内容添加到文件中:
[...]
import { MyType } from 'path/to/MyType';
[...]
jest.mock('path/to/MyType', () => {
return jest.fn().mockImplementation(() => {
return {foo: () => mockFoo}; //mockFoo is defined earlier in the file.
})
});
我在 运行 测试时出错:
Class extends value undefined is not a constructor or null
这仅在我包含 jest.mock(...)
行时发生。
从其他相关问题来看,我发现这可能是由循环依赖引起的,但我看不出这是如何通过调用 jest.mock
引入的?我可以看到当我注释掉这段代码时命中的其他断点没有被命中,所以看起来好像添加这个模拟导致我的测试因为这个原因提前失败。
有人遇到过这个吗?
问题在the documentation中解释:
A limitation with the factory parameter is that, since calls to jest.mock() are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'. It's up to you to guarantee that they will be initialized on time! For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration
使用 mockFoo
变量名允许在 jest.mock
工厂函数中使用它,风险自负。
mockFoo is defined earlier in the file
这是不正确的,因为 jest.mock
提升在各自的导入之上,但它引用的变量在评估模拟模块时未定义。对于急切评估的模拟模块,需要在工厂内部定义模拟。如果需要在测试中访问它,可以通过模块公开它。
foo
是 属性 访问器,但被模拟为函数。
模拟模块也是 CommonJS,这将阻止在大多数设置中正确映射命名导入,模拟模块中应该有 __esModule: true
。