jest-community / jest-extended

Additional Jest matchers 🃏💪
https://jest-extended.jestcommunity.dev/
MIT License
2.32k stars 223 forks source link

Vscode only recognize matcher once opening global.d.ts #307

Open hungdao-testing opened 3 years ago

hungdao-testing commented 3 years ago

Bug

Relevant code or config

jest.config.js
/*
 * For a detailed explanation regarding each configuration property, visit:
 * https://jestjs.io/docs/en/configuration.html
 */

module.exports = {
  coverageProvider: 'v8',
  moduleFileExtensions: ['js', 'json', 'ts'],
  preset: 'ts-jest',
  testEnvironment: 'node',
  testTimeout: 20000,
  setupFiles: ['./configs/envHandle.ts'],
  setupFilesAfterEnv: ['jest-extended'],
  testMatch: [
    '**/__tests__/**/*.[jt]s?(x)',
    '**/?(*.)+(spec|test).[tj]s?(x)',
  ],

  testPathIgnorePatterns: ['/node_modules/'],
};

======
babel.config.js

module.exports = {
  presets: [["@babel/preset-env", { targets: { node: "current" } }]],
};

Pre-condition:

What you did:

  1. enter the line in global.d.ts: import 'jest-extended';
  2. create a spec file
    test('Check auto-checking', () => {
    expect(1).toBeOneOf([1, 2, 3]);
    });
  3. Observe the checking-error : NO issue at this step
  4. Close vscode and reopen the spec file => FAILED. There is error Screen Shot 2021-02-23 at 23 11 26

This issue would happen when re-opening the file or vscode.

Work-around: open global.d.ts hit Save, the error in spec file is disappeared ,or enter import 'jest-extended'; in spec file

Bazze commented 3 years ago

I think this should resolve your issue: https://github.com/jest-community/jest-extended/issues/282

To your tsconfig.json, add:


{
  ...
  "files": ["path/to/global.d.ts"]
}
hungdao-testing commented 3 years ago

The problem is fixed, I thought maybe I didn't create tsconfig.json in my project. After created this file, and added few basic options. This issue is gone.

{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */

    "target": "ES2020" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
    "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
    "downlevelIteration": true /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */,
    "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
    "skipLibCheck": true /* Skip type checking of declaration files. */,
    "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
  }
}

One note that, currently I removed babel out of the project

DanKaplanSES commented 3 years ago

Isn't this a better solution to a global.d.ts file:

tsconfig.json
...
"types": ["node", "jest", "jest-extended"],
...