Code Monkey home page Code Monkey logo

Comments (2)

pjdon avatar pjdon commented on August 16, 2024

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.

ahnpnl avatar ahnpnl commented on August 16, 2024

We have example repos here https://github.com/kulshekhar/ts-jest/tree/main/examples

Would you pls check?

from ts-jest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.