Skip to content
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

chore(tests): Convert to typescript and use Jest #188

Merged
merged 33 commits into from
Aug 25, 2020
Merged

Conversation

wolfy1339
Copy link
Member

Fixes #187

@wolfy1339 wolfy1339 added typescript Relevant to TypeScript users only Type: Maintenance Any dependency, housekeeping, and clean up Issue or PR labels Aug 6, 2020
@oscard0m
Copy link
Member

oscard0m commented Aug 6, 2020

Would you agree on putting in place ESlint rules for jest? What about the recommended preset?

@oscard0m
Copy link
Member

oscard0m commented Aug 6, 2020

@wolfy1339 if you need help or want to split some tests together (as branch from your branch) let me know :)

@wolfy1339
Copy link
Member Author

Would you agree on putting in place ESlint rules for jest? What about the recommended preset?

ESLint isn't used (at least exposed to the user), since Prettier is used

@wolfy1339 wolfy1339 force-pushed the wolfy1339-patch-1 branch 2 times, most recently from e48290c to 7a6ce05 Compare August 7, 2020 04:40
wolfy1339 added a commit that referenced this pull request Aug 7, 2020
oscard0m added a commit that referenced this pull request Aug 7, 2020
Found this little bug when doing #188

Co-authored-by: OscarDOM <dominguez.celada@gmail.com>
@wolfy1339
Copy link
Member Author

I need some help. I've got things mostly done here.

The tests in test/integration/event-handler-test.ts are failing due to async timeout.
There's also test/integration/middleware-test.ts, test/unit/middleware-test.ts, test/integration/server-test.ts that need to be looked at, it's mostly just type problems.

Relevant logs:
FAIL  test/unit/middleware-test.ts
  ● Test suite failed to run

    test/unit/middleware-test.ts:12:21 - error TS2345: Argument of type '{ method: string; url: string; }' is not assignable to parameter of type 'IncomingMessage'.
      Type '{ method: string; url: string; }' is missing the following properties from type 'IncomingMessage': aborted, httpVersion, httpVersionMajor, httpVersionMinor, and 45 more.

    12   middleware(state, { method: "POST", url: "/foo" }, {}, next);
                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 FAIL  test/integration/middleware-test.ts
  ● Test suite failed to run

    test/integration/middleware-test.ts:32:14 - error TS2345: Argument of type '{ method: string; headers: { "x-github-delivery": string; "x-github-event": string; "x-hub-signature": string; }; url: string; addListener(event: string | symbol, listener: (...args: any[]) => void): EventEmitter; ... 13 more ...; eventNames(): (string | symbol)[]; }' is not assignable to parameter of type 'IncomingMessage'.
      Type '{ method: string; headers: { "x-github-delivery": string; "x-github-event": string; "x-hub-signature": string; }; url: string; addListener(event: string | symbol, listener: (...args: any[]) => void): EventEmitter; ... 13 more ...; eventNames(): (string | symbol)[]; }' is missing the following properties from type 'IncomingMessage': aborted, httpVersion, httpVersionMajor, httpVersionMinor, and 29 more.

    32   middleware(requestMock, responseMock).then(() => {
                    ~~~~~~~~~~~
    test/integration/middleware-test.ts:50:17 - error TS2554: Expected 1 arguments, but got 0.

    50     end: simple.spy(),
                       ~~~~~

      node_modules/@types/simple-mock/index.d.ts:42:12
        42     spy<T>(fn: Fn<T>): Spy<T>;
                      ~~~~~~~~~
        An argument for 'fn' was not provided.
    test/integration/middleware-test.ts:55:14 - error TS2345: Argument of type '{ method: string; headers: { "x-github-delivery": string; "x-github-event": string; "x-hub-signature": string; }; url: string; addListener(event: string | symbol, listener: (...args: any[]) => void): EventEmitter; ... 13 more ...; eventNames(): (string | symbol)[]; }' is not assignable to parameter of type 'IncomingMessage'.
      Type '{ method: string; headers: { "x-github-delivery": string; "x-github-event": string; "x-hub-signature": string; }; url: string; addListener(event: string | symbol, listener: (...args: any[]) => void): EventEmitter; ... 13 more ...; eventNames(): (string | symbol)[]; }' is missing the following properties from type 'IncomingMessage': aborted, httpVersion, httpVersionMajor, httpVersionMinor, and 29 more.

    55   middleware(requestMock, responseMock).then(() => {
                    ~~~~~~~~~~~

 FAIL  test/integration/server-test.ts
  ● Test suite failed to run

    test/integration/server-test.ts:101:11 - error TS2339: Property 'body' does not exist on type 'IncomingMessage'.

    101       req.body = JSON.parse(Buffer.concat(dataChunks).toString());
                  ~~~~
    test/integration/server-test.ts:147:19 - error TS2345: Argument of type 'Spy<unknown>' is not assignable to parameter of type '(event: WebhookEventHandlerError) => void | Promise<void>'.
      Type 'unknown' is not assignable to type 'void | Promise<void>'.
        Type '{}' is missing the following properties from type 'Promise<void>': then, catch, [Symbol.toStringTag], finally

    147   api.on("error", errorHandler);
                          ~~~~~~~~~~~~
    test/integration/server-test.ts:181:31 - error TS2554: Expected 1 arguments, but got 0.

    181   const errorHandler = simple.spy();
                                      ~~~~~

      node_modules/@types/simple-mock/index.d.ts:42:12
        42     spy<T>(fn: Fn<T>): Spy<T>;
                      ~~~~~~~~~
        An argument for 'fn' was not provided.
    test/integration/server-test.ts:182:19 - error TS2345: Argument of type 'Spy<unknown>' is not assignable to parameter of type '(event: WebhookEventHandlerError) => void | Promise<void>'.

    182   api.on("error", errorHandler);
                          ~~~~~~~~~~~~

 FAIL  test/integration/event-handler-test.ts (19.016 s)
  ● events

    : Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error:

      3 | import installationCreatedPayload from "../fixtures/installation-created-payload.json";
      4 | 
    > 5 | test("events", (t) => {
        | ^
      6 |   // expect.assertions(7);
      7 | 
      8 |   const eventHandler = createEventHandler({});

      at new Spec (node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
      at Object.<anonymous> (test/integration/event-handler-test.ts:5:1)

  ● options.transform

    : Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error:

      101 | });
      102 | 
    > 103 | test("options.transform", (t) => {
          | ^
      104 |   expect.assertions(2);
      105 | 
      106 |   const eventHandler = createEventHandler({

      at new Spec (node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
      at Object.<anonymous> (test/integration/event-handler-test.ts:103:1)

  ● multiple errors in same event handler

    : Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error:

      141 | });
      142 | 
    > 143 | test("multiple errors in same event handler", (t) => {
          | ^
      144 |   expect.assertions(2);
      145 | 
      146 |   const eventHandler = createEventHandler({});

      at new Spec (node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
      at Object.<anonymous> (test/integration/event-handler-test.ts:143:1)

@wolfy1339
Copy link
Member Author

@dominguezcelada Care to help me on this? I noted the errors I'm getting in my previous comment

@oscard0m
Copy link
Member

oscard0m commented Aug 8, 2020

The tests in test/integration/event-handler-test.ts are failing due to async timeout.

I opened a PR to this branch which I think is solving the issue with timeouts: #195

If you think this is the solution, feel free to merge it in your branch :)

@oscard0m
Copy link
Member

oscard0m commented Aug 8, 2020

There's also test/integration/middleware-test.ts,

#196

test/unit/middleware-test.ts,

#197

test/integration/server-test.ts that need to be looked at, it's mostly just type problems.

#198 WIP


Once we finish this, if this is the right approach to follow, what do you think about getting rid of simple-mock library? I think the usage we are giving to this library is already covered by jest itself

@oscard0m
Copy link
Member

oscard0m commented Aug 8, 2020

btw, before doing merge, I think we should make the first commit (or the resulting squashed one to be test(<scope>): commit message as type instead of chore(<scope>): commit message 😎

@oscard0m
Copy link
Member

oscard0m commented Aug 9, 2020

test/integration/server-test.ts that need to be looked at, it's mostly just type problems.

#198 WIP

#198@wolfy1339

After tsconfig.json changes in master with strict: true new TypeScript errors were introduced 😢 . Once we validate my PR in here I can jump to fix them if you want

@oscard0m
Copy link
Member

Hey @wolfy1339 it seems we were overlaping on fixing the changes. If there's something I can help or we can split / pair-program let me know! :)

@wolfy1339
Copy link
Member Author

wolfy1339 commented Aug 22, 2020 via email

@oscard0m
Copy link
Member

oscard0m commented Aug 22, 2020

Just leave a comment as to what file/what issue you're working on, that should be just fine

Getting event-handler-test.ts errors @wolfy1339

@oscard0m
Copy link
Member

oscard0m commented Aug 22, 2020

Just leave a comment as to what file/what issue you're working on, that should be just fine

Getting event-handler-test.ts errors

Getting sign-test.ts errors @wolfy1339

@oscard0m
Copy link
Member

Just leave a comment as to what file/what issue you're working on, that should be just fine

Getting event-handler-test.ts errors

Getting sign-test.ts errors @wolfy1339

Getting server-test.ts errors @wolfy1339

@oscard0m
Copy link
Member

We only need to fix the coverage for middleware-test.ts and we will be ready to go 👏 @wolfy1339

@wolfy1339
Copy link
Member Author

wolfy1339 commented Aug 23, 2020

@dominguezcelada

According to the coverage report, it seems that the lines that aren't being used are related to timeouts, which was added in dfdfc46

From what I can understand of the code, a timeout needs to be simulated and doing so using @sinonjs/fake-timers.
One is request with no errors and just a timeout, the other an error is thrown and it times out.

@gr2m Any ideas how to test that

@oscard0m
Copy link
Member

@dominguezcelada

According to the coverage report, it seems that the lines that aren't being used are related to timeouts, which was added in dfdfc46

From what I can understand of the code, a timeout needs to be simulated and doing so using @sinonjs/fake-timers.
One is request with no errors and just a timeout, the other an error is thrown and it times out.

@gr2m Any ideas how to test that

I just applied the test coverage. I'm using Jest's Timer Mocks utility. Is that ok @gr2m @wolfy1339 ?

wolfy1339 and others added 15 commits August 25, 2020 02:49
Fix some types
Replace some remaining tap functions
Ignore some TypeScript type errors, they are there on purpose to test if the function throws an error
- Pull in the relevant types package for `@sinonjs/fake-timers`
- Convert those tests from tap to Jest
- Add some Typescript types to those tests
Adds back the existing error message that was removed
@gr2m
Copy link
Contributor

gr2m commented Aug 25, 2020

As sorry, I thought you meant the error was on master.

I got a pull request merged into aggregate-error: sindresorhus/aggregate-error#13. But before this was merged, I added new the type definitions directly into @octokit/webhooks. With the latest change, I upgraded to latest aggregate-error and removed to temporary types that I added, as they are now obsolete.

I can look into the error on wolfy1339-patch-1 tomorrow

@oscard0m
Copy link
Member

As sorry, I thought you meant the error was on master.

I got a pull request merged into aggregate-error: sindresorhus/aggregate-error#13. But before this was merged, I added new the type definitions directly into @octokit/webhooks. With the latest change, I upgraded to latest aggregate-error and removed to temporary types that I added, as they are now obsolete.

I can look into the error on wolfy1339-patch-1 tomorrow

@wolfy1339 @gr2m Solved 10386fa (#188) by upgrading aggregate-error package to latest version with @gr2m 's fix (sindresorhus/aggregate-error#13)

@oscard0m
Copy link
Member

oscard0m commented Aug 25, 2020

I think we are ready to go now 💪 🍾

@@ -1,6 +1,7 @@
import { createHmac } from "crypto";

export function sign(secret: string, payload: string | object): string {
export function sign(secret?: string, payload?: string | object): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert this change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert this change?

@gr2m without this change we are not able to give coverage to this

if (!secret || !payload) {
throw new TypeError("secret & payload required");
}

Maybe you prefer this approach on for tests so we give coverage but we bypass the TypeScript check? #237

@oscard0m oscard0m requested a review from gr2m August 25, 2020 19:18
Copy link
Contributor

@gr2m gr2m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outstanding work @wolfy1339 & @dominguezcelada 🥇🥇🎊

@gr2m gr2m merged commit 7e8defa into master Aug 25, 2020
@gr2m gr2m deleted the wolfy1339-patch-1 branch August 25, 2020 19:43
@github-actions
Copy link
Contributor

🎉 This PR is included in version 7.11.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Maintenance Any dependency, housekeeping, and clean up Issue or PR typescript Relevant to TypeScript users only
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rewrite tests in TypeScript and Jest
3 participants