Comments (2)
This would be good to have given that ts-jest
works "out of the box".
If you're running into problems, here's what worked for me starting from an empty project folder:
(path /
is project root)
1. Initialize a new NodeJS project
Run npm init -y
Creates file /package.json
with the following contents. I've removed a few extra keys for brevity and added a test
script that we'll need later.
{
"name": "ts-testing",
"scripts": {
"test": "jest"
},
"devDependencies": {
"@types/jest": "^29.5.12",
"jest": "^29.7.0",
"ts-jest": "^29.1.2",
"typescript": "^5.4.3"
}
}
2. Install dependencies step from ts-jest docs
Run npm install --save-dev jest typescript ts-jest @types/jest
3. From the next step in the page, create a ts-jest config
Run npx ts-jest config:init
Creates file /jest.config.js
with the following contents.
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
4. Create a new default typescript config
Run npx tsc -init
(creates file /tsconfig.json
)
Creates file /tsconfig.json
with the following contents. I've removed the generate comments.
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
5. Create your typescript file
For example /calc.ts
with the following contents.
export function add(a: number, b: number): number {
return a + b;
}
6. Create your unit test file.
It should end in spec.ts
. For example /calc.spec.ts
with the following contents.
import { add } from "./calc";
describe("Addition", () => {
test("Two integers", () => {
expect(add(1, 2)).toBe(3);
});
});
7. Start the tests
Run npm run test
Should get the following if you used the examples above
PASS ./calc.spec.ts
Addition
√ Two integers (2 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
from ts-jest.
We have example repos here https://github.com/kulshekhar/ts-jest/tree/main/examples
Would you pls check?
from ts-jest.
Related Issues (20)
- [Security] Lodash vulnerability
- [Bug]: Cannot read properties of undefined while using NestJS and Enum HOT 1
- [Bug]: error TS2307: Cannot find module '...' or its corresponding type declarations. HOT 2
- [Feature]: Bump JEST to 29.7.0 HOT 3
- [Bug]: docs confusion - deprecated presets HOT 4
- [Bug]: setupFilesAfterEnv with a path to a TS file leads to the error "SyntaxError: Cannot use import statement outside a module" HOT 4
- [Bug]: ESM Support "Use ESM Presets" documentation has confusing redundancy HOT 1
- [Bug]: dynamic imports create a double default-wrapped export -- `{ default: { default: MyDefaultExport } }` HOT 2
- version 29.2.1 and 29.2.2 have new dependency on ejs which is flagged with critical CVE-2023-29827. Can this be remedied? HOT 2
- [Bug]: Using "module": "NodeNext" causes the error "SyntaxError: Cannot use import statement outside a module" HOT 5
- [Bug]: Version `29.2.1` breaks default imports HOT 2
- [Bug]: ts-jest 29.2 breaks dependency transform HOT 18
- [Please read the message below]
- [Bug]: ts-jest[cli] (FATAL) Configuration file jest.config.cjs must be a JavaScript or JSON file. with config.migrate in vite react project HOT 2
- [Please read the message below]
- [Bug]: "ReferenceError: exports is not defined" with ts-jest 29.2 HOT 2
- [Bug]: broken link in installation page in getting started
- [Bug]: When import ESModule from CommonJS, Error: Must use import to load ES Module HOT 12
- [Bug]: Undefined moduleResolution HOT 1
- [Feature]: Support tc39 import attributes
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ts-jest.