Skip to content

Instantly share code, notes, and snippets.

@Windrill
Created January 22, 2023 21:30
Show Gist options
  • Select an option

  • Save Windrill/8ea4b2caae77537007b980317e8e3d58 to your computer and use it in GitHub Desktop.

Select an option

Save Windrill/8ea4b2caae77537007b980317e8e3d58 to your computer and use it in GitHub Desktop.
How to setup Jest with TypeScript
1. npm install Jest
- This command is the Jest package
2. npm install ts-jest
- This enables jest with TypeScript annotations
3. npm i --save-dev @types/jest
- Use with step 4
4. Addd "@types/jest" to your tsconfig.json:
{ "compilerOptions": { "types": [""@types/jest"] } }
- This fixes your 'test' and 'expect' not found errors
5. Create some files named xxx.test.ts anywhere in your project. Usually these files will be auto-detected by jest for testing without needing to configure a jest config file.
6. Add this to package.json:
"jest": {
"preset": "ts-jest"
}
in package.json
(eg. add it after the 'dependencies' section, 'devDependencies' section....
7. Add:
describe('Description for group of tests', () => {
test('A test', () => {
});
test('A second test', () => {
});
});
You don't need to import anything for these keywords, because you have jest configured already
8. Run 'jest' in your terminal somewhere in your repository, Jest will automatically detect all xx.test.ts files and run them
9. For isolatedModules: true typescript libraries, add export {} to the end of your file.
10. Versions used:
// Caetgorized under dependencies
"ts-jest": "^29.0.5"
"jest": "^29.3.1",
// Categorized under dev dependencies
"@types/jest": "^29.2.6",
"typescript": "^4.5.4",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment