Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs]: Include a GitHub repo with a minimal working demo #4286

Open
martinsik opened this issue Mar 29, 2024 · 1 comment
Open

[Docs]: Include a GitHub repo with a minimal working demo #4286

martinsik opened this issue Mar 29, 2024 · 1 comment

Comments

@martinsik
Copy link

🚀 Feature Proposal

It would be helpful if I could experiment with ts-jest in a independent project that is guaranteed to work and that I can just pull and run.

Motivation

I'm trying to implement ts-jest into an existing project but I'm not able to run a single test. Using just jest works but with ts-jest it looks like it doesn't even read my jest.config.ts. I suppose there's something with babel config or tsconfigs that are already in the project. Maybe TypeScript version or I don't know what is the issue.

When I run npx ts-jest I only get this response and it doesn't run any test:

Usage:
  ts-jest command [options] [...args]

Commands:
  config:init           Creates initial Jest configuration
  config:migrate        Migrates a given Jest configuration
  help [command]        Show this help, or help about a command

Example:
  ts-jest help config:migrate

Example

No response

@pjdon
Copy link

pjdon commented Apr 4, 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants