Skip to content Skip to sidebar Skip to footer

Vs Code Imports Using The Require Syntax In React Test Cases

Little lately, I have found that my react project in the VS Code IDE is using require syntax in the test cases when I'm doing auto import from the suggestions. Could anyone please

Solution 1:

From the new feature CommonJS auto imports, we know:

If VS Code detects that you are working in a CommonJS style JavaScript module, auto imports will now use require instead of import.

You can set compilerOptions.module to es2015 for jsconfig.json. So that the js file will be detected as ESM, auto imports will use import instead of require.

jsconfig.json:

{"compilerOptions":{"module":"es2015"},}
// auto imports use `import` keyword.import { render } from'react-dom';

describe('Home', () => {
  it('should render Search page', () => {
    render
  });
});

For more info about when VS code uses require, see shouldUseRequire function.

P.S If it does not work, try to reload the window of VS code after adding this configuration.

Post a Comment for "Vs Code Imports Using The Require Syntax In React Test Cases"