No testing solution out there is perfect. That said, jest is an excellent unit testing option which provides great TypeScript support.

Install the following using npm:

Explanation:

  • Install jest framwork (jest)
  • Install the types for jest (@types/jest)
  • Install the TypeScript preprocessor for jest (ts-jest) which allows jest to transpile TypeScript on the fly and have source-map support built in.
  • Save all of these to your dev dependencies (testing is almost always a npm dev-dependency)
  1. module.exports = {
  2. "roots": [
  3. "<rootDir>/src"
  4. ],
  5. "transform": {
  6. "^.+\\.tsx?$": "ts-jest"
  7. },
  8. "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
  9. "moduleFileExtensions": [
  10. "ts",
  11. "tsx",
  12. "js",
  13. "jsx",
  14. "json",
  15. "node"
  16. ],
  17. }

Explanation:

  • We always recommend having all TypeScript files in a src folder in your project. We assume this is true and specify this using roots option.
  • The transform config just tells jest to use ts-jest for ts / tsx files.
  • The testRegex tells Jest to look for tests in any __tests__ folder AND also any files anywhere that use the (.test|.spec).(ts|tsx) extension e.g. asdf.test.tsx etc.
  • The moduleFileExtensions tells jest to recognize our file extensions. This is needed as we add ts/ into the defaults (js|jsx|json|node).

Run npx jest from your project root and jest will execute any tests you have.

Add package.json:

  1. "test": "jest"
  2. }
  • This allows you to run the tests with a simple npm t.
  • And even in watch mode with npm t -- --watch.
  • npx jest -w
  • For a file foo.ts:

  1. import { sum } from '../';
  2. test('basic', () => {
  3. expect(sum()).toBe(0);
  4. });
  5. test('basic again', () => {
  6. expect(sum(1, 2)).toBe(3);
  7. });

Notes:

  • Jest provides the global test function.
  • Jest comes prebuilt with assertions in the form of the global expect.

Jest has built-in async/await support. e.g.

  1. test('basic',async () => {
  2. expect(sum()).toBe(0);
  3. });
  4. test('basic again', async () => {
  5. expect(sum(1, 2)).toBe(3);
  6. }, 1000 /* optional timeout */);

Enzyme allows you to test react components with dom support. There are three steps to setting up enzyme:

  1. Install enzyme, types for enzyme, a better snapshot serializer for enzyme, enzyme-adapter-react for your react version npm i enzyme /enzyme enzyme-to-json enzyme-adapter-react-16 -D
  2. Add "snapshotSerializers" and "setupTestFrameworkScriptFile" to your jest.config.js:
  1. Create src/setupEnzyme.ts file.
  1. import * as EnzymeAdapter from 'enzyme-adapter-react-16';
  2. configure({ adapter: new EnzymeAdapter() });
  • checkboxWithLabel.tsx:
  1. import * as React from 'react';
  2. labelOn: string,
  3. labelOff: string
  4. }, {
  5. isChecked: boolean
  6. }> {
  7. constructor(props) {
  8. super(props);
  9. this.state = { isChecked: false };
  10. }
  11. onChange = () => {
  12. this.setState({ isChecked: !this.state.isChecked });
  13. }
  14. render() {
  15. return (
  16. <label>
  17. <input
  18. type="checkbox"
  19. checked={this.state.isChecked}
  20. onChange={this.onChange}
  21. />
  22. {this.state.isChecked ? this.props.labelOn : this.props.labelOff}
  23. </label>
  24. );
  25. }
  26. }
  • checkboxWithLabel.test.tsx:
  • Built-in assertion library.
  • Great TypeScript support.
  • Very reliable test watcher.
  • Snapshot testing.
  • Built-in coverage reports.
  • Built-in async/await support.