jest-extended icon indicating copy to clipboard operation
jest-extended copied to clipboard

Not working typescript import suggestion

Open soullivaneuh opened this issue 4 years ago • 5 comments

Bug

  • package version: 0.11.2
  • node version: v12.13.0
  • npm (or yarn) version: 6.12.0

Relevant code or config

See steps.

What you did:

Following this guide: https://github.com/jest-community/jest-extended/issues/172

The global.d.ts file:

import 'jest-extended';

The tsconfig.json file:

{
  "compilerOptions": {
    "target": "es2018",
    "module": "commonjs",
    "outDir": "./lib",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true
  },
  "include": [
    "src/**/*",
    "./global.d.ts"
  ],
  "exclude": ["test"]
}

The jest.config.js file (using ts-jest):

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  globals: {
    'ts-jest': {
      diagnostics: false,
    },
  },
};

What happened (please provide anything you think will help):

The function is still not found:

 FAIL  test/services/rides.test.ts
  'rides' service
    ✓ registered the service (3ms)
    ✕ creates a ride (75ms)
    ✕ it complete a step (13ms)

  ● 'rides' service › creates a ride

    TypeError: expect(...).toSatisfyAll is not a function

      42 |     expect(ride.currentStep).toBe(0);
      43 |     expect(ride.steps).toHaveLength(4);
    > 44 |     expect(ride.steps).toSatisfyAll((step) => step.doneAt === null)
         |                        ^
      45 |     rideId = ride._id;
      46 |   });
      47 | 

Current workaround:

Adding import 'jest-extended'; directly on each test file I use the jest-extended solves the issue, but it's cumbersome.

soullivaneuh avatar Nov 17 '19 21:11 soullivaneuh

You can import jest-extended inside your test, without using global.d.ts

rafalpetryka avatar Nov 18 '19 11:11 rafalpetryka

@soullivaneuh Shouldn't jest.config.js be:

module.exports = {
  preset: 'ts-jest',
  // This line seems to be missing.
  setupFilesAfterEnv: ['jest-extended'],
  testEnvironment: 'node',
  globals: {
    'ts-jest': {
      diagnostics: false,
    },
  },
};

?

It's there under Setup in the readme.

Seally avatar Nov 21 '19 10:11 Seally

+1

uhm where exactly after setup?

Setup Jest >v24 Add jest-extended to your Jest setupFilesAfterEnv configuration. See for help

"jest": { "setupFilesAfterEnv": ["jest-extended"] } Jest <v23 "jest": { "setupTestFrameworkScriptFile": "jest-extended" } If you are already using another test framework, like jest-chain, then you should create a test setup file and require each of the frameworks you are using.

For example:

// ./testSetup.js require('jest-extended'); require('jest-chain'); require('any other test framework libraries you are using'); Then in your Jest config:

"jest": { "setupTestFrameworkScriptFile": "./testSetup.js" } Typescript If your editor does not recognise the custom jest-extended matchers, add a global.d.ts file to your project with:

import 'jest-extended';

xenoterracide avatar Jan 06 '20 17:01 xenoterracide

disabling diagnostics seems to work, albeit sketchy. however nothing suggested/tried seems to work in intellij except for the direct import

xenoterracide avatar Jan 06 '20 17:01 xenoterracide

I think you need to remove "exclude": ["test"]in your tsconfig.json, otherwise it won't compile your tests with this config and without including global.d.ts You also may have to specify the tsConfig key in your jest config, like so :

ts-jest': {
      diagnostics: true,
      tsConfig: './tsconfig.json',
    }

abalhier avatar Apr 02 '20 12:04 abalhier