🧩 A TypeScript starter for solving LeetCode problems with test-driven development
Prerequisites
-
Ensure you have Node installed on your machine before proceeding.
-
Clone this repo locally and
cd
into it
-
Install dependencies
npm install
-
Start test-driven-development (TDD)
npm start
This script runs Jest in watch mode.
-
Start making changes!
- You will see your changes be validated against your test cases.
- Add a new problem with the script (or manually)
- Test your solution with Jest using TDD
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.
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 tests are typically denoted using the it()
function, which is an alias for the test()
function.
To run only specific tests and skip all others, rename the desired test(s) to fit()
instead of it()
.
To run all tests except specific tests, rename the desire test(s) to xit()
instead of it()
.
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
- By default, Jest matches
- 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
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!
- Fork this repo and set it up locally
- Make your desired changes and commit them to a feature branch in your forked repo
- Open up a PR from your forked repo branch against this repository
Thanks for making this project better! 💪