Skip to content

🧩 A TypeScript starter for solving LeetCode problems with test-driven development

License

Notifications You must be signed in to change notification settings

shetharp/ts-prep

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ts-prep

🧩 A TypeScript starter for solving LeetCode problems with test-driven development

Usage

Prerequisites
  • Ensure you have Node installed on your machine before proceeding.

  • Clone this repo locally and cd into it

  1. Install dependencies

    npm install
  2. Start test-driven-development (TDD)

    npm start

    This script runs Jest in watch mode.

  3. Start making changes!

Add a new problem

In the root directory of the repo, run

npm run new-problem '<title>'
Where <title> is the LeetCode problem's title.
  • The format of the title should be: {number}. {name}
  • The title must start with a number, followed by a period. Otherwise, the script won't work!
  • The title should be wrapped in quotes for the argument to get passed into the script

For example, if you want to set up starter files for problem 202. Happy Number, you would run

npm run new-problem '202. Happy Number'
About this script

This repo comes with a script to quickly set up starter files for new LeetCode problems—letting you focus on the real problem solving!

This script is defined in the package.json. It uses your local Node runtime environment to execute the scripts/new-problem.js file. The script parses the inputted title and uses the templates to auto-generate the boilerplate .md and .ts files.

You could run this script directly from the repo root:

node ./scripts/new-problem.js -t '<title>'

Alternatively, you can add a new problem manually by following the repo structure.

Jest Tips for Test-Driven Development

If you are running Jest (npm start), you can press the w key to get a list of watch usage commands. For example, you can press p to filter by a filename pattern and run tests only in those files.

Unit Test

Unit tests are typically denoted using the it() function, which is an alias for the test() function.

Focus Test

To run only specific tests and skip all others, rename the desired test(s) to fit() instead of it().

Exclude Test

To run all tests except specific tests, rename the desire test(s) to xit() instead of it().

Structure

src/ – In general, you should do all your work here
  • Each LeetCode problem should have its own directory
  • Add a .md file in the problem directory for the problem description and discussion
  • Add a .ts file in the problem directory to solve and test
scripts/ – Contains custom scripts and templates
  • The new-problem.js script is used to set up starter files for new LeetCode problems
  • The templates directory contains files that define the boilerplate content for auto-generating .md and .ts files
  • Feel free to add or modify scripts or templates to fit your needs
jest.config.js – Defines our custom Jest configuration
  • We match test cases defined in regular .ts files
    • By default, Jest matches .ts files in a __tests__ dir or if the filename ends with .spec.ts or .test.ts
    • Our custom testMatch rule lets write code to solve and test in one file
  • We use ts-jest as a dependency to transform .ts files for Jest
    • By default, Jest only works with JavaScript
  • Learn how to configure Jest for TypeScript

How to Contribute

Please note that this repo is meant to be a starter, so we don't need contributors to submit LeetCode problems solved with TypeScript. However, any tooling or documentation that you would like to contribute to make the developer experience better is welcomed!

  1. Fork this repo and set it up locally
  2. Make your desired changes and commit them to a feature branch in your forked repo
  3. Open up a PR from your forked repo branch against this repository

Thanks for making this project better! 💪

About

🧩 A TypeScript starter for solving LeetCode problems with test-driven development

Topics

Resources

License

Stars

Watchers

Forks