jest-extended
jest-extended copied to clipboard
Not working typescript import suggestion
Bug
-
package
version: 0.11.2 -
node
version: v12.13.0 -
npm
(oryarn
) 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.
You can import jest-extended inside your test, without using global.d.ts
@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.
+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';
disabling diagnostics seems to work, albeit sketchy. however nothing suggested/tried seems to work in intellij except for the direct import
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',
}