Skip to content

Use Jest as test framework #18

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

Closed
codeandcats opened this issue Apr 25, 2019 · 3 comments · Fixed by #26
Closed

Use Jest as test framework #18

codeandcats opened this issue Apr 25, 2019 · 3 comments · Fixed by #26

Comments

@codeandcats
Copy link
Member

codeandcats commented Apr 25, 2019

Another enhancement request: I was wondering how you'd feel about replacing Mocha with Jest?

Motivation

  • Chai's API can be a little inconsistent with whether you should use brackets or not after assertions. Jest's API is straight-forward, no having to guess the correct combination of nested property names, and every assertion is a function, not a property getter, so you always use brackets. e.g. expect(thing).toBeTruthy().

  • Support for spying on mock functions comes out of box. No need to use a separate library like Sinon. Naive example:

      describe('AdvancedMath', () => {
        it('should square numbers', () => {
          const basicMath: Partial<BasicMath> = {
            multiply: jest.fn().mockReturnValue(3);
          };
    
          const advancedMath = new AdvancedMath(basicMath);
          const result = advancedMath.square(5);
          expect(result).toBe(25);
          expect(basicMath.multiply).toHaveBeenCalledOnce();
          expect(basicMath.multiply).toHaveBeenCalledWith(5, 5);
        });
      });
  • Support for mocking modules out of the box. No need to use a separate library like mockery. Unlike mockery you don't need to ensure your mock is registered before the library is required in the code that you're testing.

  • Comes with Instanbul support built-in, allowing you to easily track code-coverage and even fail when minimum thresholds for functions, statements, branches and lines are not met.

  • One test library to rule them all! 💍

Indirectly related: Also wanted to ask how you'd feel about putting the tests in the same directory as the files being tested.

This has the following advantages:

  • Makes it clear that foo.ts is not tested when it doesn't have an accompanying foo.test.ts file right next to it.

  • No need to maintain a mirrored directory structure of your tests.

@laurence-myers
Copy link
Member

Sure, feel free to raise a PR. 👍

(I agree re: Chai's BDD API, but they also offer a "TDD" API which uses classic single-function assertions.)

I'd prefer to keep the tests in a separate directory for now, it makes it easier to exclude test files from the TSC config and published artifacts. The code coverage report could serve to tell us if a file is tested.

@codeandcats
Copy link
Member Author

codeandcats commented Apr 26, 2019

Okay, cool. I might convert it and shoot through a PR when I have some time.

No worries at test location, though I will say...

  1. Don't forget that tsconfig has an exclude glob option.

  2. Also, the ts-jest plugin for jest runs directly against the .ts files without generating .js files so test output files never pollute your artefacts directory.

Imho, it's super nice when creating a component to have a folder for the component, with code, the view, styles and tests all in same location. Much less jumping around the project to find related files.

@laurence-myers
Copy link
Member

This has been released in v.0.0.13

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

Successfully merging a pull request may close this issue.

2 participants