title: How to unit test exception thrown in TypeScript class's constructor in JestJs
body: <p>I am building some application in <code>NestJs</code>, therefore the default unit test framework is <code>JestJs</code>. Assume I have following class My.ts</p>
  
  <pre><code>export My {
      constructor(private myValue: number) {
         if (myValue ==== null) {
             throw new Error('myValue is null');
         }
      }
  }
  </code></pre>
  
  <p>I have created my unit test class My.spec.ts</p>
  
  <pre><code>import { My } from './My';
  
  describe('My', () =&gt; {
      fit('Null my value throws', () =&gt; {
          expect(new My(null)).rejects.toThrowError('myValue is null');
      });
  });
  </code></pre>
  
  <p>I use the command <code>npm run test</code> to run the unit tests, instead of getting what I expected I had a failure to complain the code in my <code>My</code> class constructor that an exception is thrown.</p>
  
  <p>What is the proper way to write unit test code in Jest to test the exception logic in constructor?</p>