javascript - 在 React 测试用例中使用 require 语法导入 VS Code
问题描述
最近,我发现当我从建议中自动导入时,我在 VS Code IDE 中的 react 项目在测试用例中使用了 require 语法。有人可以帮忙吗?
我正在使用 React 测试库,这就是它的样子。
const { render } = require("@testing-library/react");
const { default: Home } = require("../Home");
describe("Home", () => {
it("should render Search page", () => {
const { container } = render(<Home />);
});
});
我有一个jsconfig.json
文件如下
{
"compilerOptions": {
"baseUrl": "src"
},
"include": [
"src"
],
}
解决方案
从CommonJS 的新特性 auto imports中,我们知道:
如果 VS Code 检测到您正在使用 CommonJS 样式的 JavaScript 模块,自动导入现在将使用
require
而不是import
.
您可以设置compilerOptions.module
为es2015
jsconfig.json 。这样 js 文件将被检测为 ESM,自动导入将使用import
而不是require
.
jsconfig.json
:
{
"compilerOptions": {
"module": "es2015"
},
}
// auto imports use `import` keyword.
import { render } from 'react-dom';
describe('Home', () => {
it('should render Search page', () => {
render
});
});
有关 VS 代码何时使用的更多信息require
,请参阅shouldUseRequire函数。
PS如果还是不行,添加此配置后尝试重新加载VS code的窗口。
推荐阅读
- java - Java 打印票证
- validation - 自定义验证在 Laravel 5.4 中不起作用
- python - Pandas 自条件以来的行数
- javascript - 有没有办法在 webgl2 中的帧缓冲区之间交换数据?(尝试多级图像处理)
- nativescript - 如何在 NativeScript (Angular) 中为应用程序制作和编码启动画面?
- r - R中的并行编程和<<-赋值运算符
- c# - keydown 事件无法在 datagridview 中添加项目
- swift - RxSwift 观察模型的变化并提出请求
- php - 在没有配置的外部包中加载 routes.yaml。在项目中
- docker - 努力将 Nginx 代理 + LetsEncrypt Companion 与我的 Docker 组合 Web 应用程序集成