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: migrate some tests to TS #10073

Merged
merged 4 commits into from
Jul 30, 2020
Merged

Conversation

ghostd
Copy link
Contributor

@ghostd ghostd commented May 21, 2020

Migrate some tests

Copy link
Contributor

@jeysal jeysal left a comment

Choose a reason for hiding this comment

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

Cool thanks!

@@ -15,21 +15,22 @@ const jestExpect = require('../');
expect.addSnapshotSerializer(alignedAnsiStyleSerializer);

jestExpect.extend({
toBeDivisibleBy(actual, expected) {
toBeDivisibleBy(actual: number, expected: number) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose in a real matcher actual would always be unknown, but here for simplicity we can ignore that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If i use unknown, TS does not allow the %operation on the next line (i guess i should add a if typeof before)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes I understand, it was more of a loud thought. Could introduce a type check so that this would be more of a 'proper' matcher implementation. In a test where it's just supposed to be some example matcher, I'd be fine with keeping it as is. Your call 🙃

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, i didn't understand your comment. You mean something like that:

  toBeDivisibleBy(actual: unknown, expected: number) {
    let pass = false;
    if (typeof actual === 'number') {
      pass = actual % expected === 0;
    }
    const message = pass
      ? () => `expected ${actual} not to be divisible by ${expected}`
      : () => `expected ${actual} to be divisible by ${expected}`;

    return {message, pass};
  },

@@ -34,11 +36,11 @@ describe('isError', () => {
});

it('should detect errors from another context', () => {
testErrorFromDifferentContext(win => new win.Error());
testErrorFromDifferentContext((win: any) => new win.Error());
Copy link
Contributor

Choose a reason for hiding this comment

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

Window or typeof window or something could be a good type here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Window (as defined in lib.dom.ts) does not have Error property:
TS2339: Property 'Error' does not exist on type 'Window'.
We could extend the Window declaration, or use the NodeJS.Global type. Any preferences?

Copy link
Contributor

Choose a reason for hiding this comment

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

That is weird. It works on the playground Playground Link

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right. The TS error only shows up in my IDE... so i guess my IDE loads the TS settings badly :-/

packages/expect/src/__tests__/isError.test.ts Outdated Show resolved Hide resolved
@@ -6,8 +6,6 @@
*
*/

'use strict';

const {alignedAnsiStyleSerializer} = require('@jest/test-utils');
Copy link
Member

Choose a reason for hiding this comment

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

can we use import in these tests instead of require?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@SimenB SimenB requested a review from jeysal June 1, 2020 08:09
@SimenB
Copy link
Member

SimenB commented Jun 4, 2020

@jeysal merge if good to go?

@@ -13,14 +13,14 @@ import {isError} from '../utils';

// Copied from https://github.com/graingert/angular.js/blob/a43574052e9775cbc1d7dd8a086752c979b0f020/test/AngularSpec.js#L1883
describe('isError', () => {
function testErrorFromDifferentContext(createError) {
function testErrorFromDifferentContext(createError: Function) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The Function type should be avoided, because it doesn't do any checking at call time, and accepts "functions" like constructors (which will error if not called with new).

It's not blocking yet, but @typescript-eslint added Function to the recommended types for ban-types in v3, so the less new usages of the Function type are added the codebase, the less work I have to do for #10177 :)

You should at worse be able to use (...unknown[]) => any as a quick way to make linting happy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Let me know if you have other blockers for the eslint migration.

Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome, thanks will do!

@SimenB
Copy link
Member

SimenB commented Jun 23, 2020

@jeysal ping

@SimenB
Copy link
Member

SimenB commented Jul 28, 2020

@jeysal hiya 😀

@SimenB SimenB changed the title Migrate some tests to TS chore: migrate some tests to TS Jul 30, 2020
@SimenB SimenB merged commit 01b93d4 into jestjs:master Jul 30, 2020
@SimenB
Copy link
Member

SimenB commented Jul 30, 2020

Thanks @ghostd!

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
@ghostd ghostd deleted the migrate-some-tests branch May 11, 2021 18:24
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants