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

feat: added tests/typescript to test supported TS examples #6775

Merged
merged 16 commits into from
Feb 10, 2023

Conversation

btw17
Copy link
Member

@btw17 btw17 commented Jan 13, 2023

The basics

  • I branched from develop
  • My pull request is against develop
  • My code follows the style guide
  • I ran npm run format and npm run lint

The details

Resolves

This adds support for testing theoretical Blockly use cases, as mentioned in #6639 (review)

Proposed Changes

Updated npm run test to include a new suite of tests connected to tests/typescript:

  • The test passes if everything there compiles.
  • The test fails if the files fail to compile.
    • The error output from the tsc command displays in the console output.

Behavior Before Change

One less test suite to run.

Behavior After Change

One more test suite to run.

Reason for Changes

To enable testing fake custom fields. This allows us to see how core updates impact use cases we'd like to support.

Test Coverage

N/A

Documentation

I don't believe so.

Additional Information

  • I added one example test case that's related to a discussion in fix: replace 'AnyDuringMigration' for core/field.ts value functions #6639 about allowing Field subclasses to have different type information for what's passed into setValue than is stored on the Field.
  • I've tested the console output with npm run test, verifying it passes when the command passes, fails when the command fails, and properly outputs the log info needed to debug the failure.

@btw17 btw17 requested a review from a team as a code owner January 13, 2023 23:30
@btw17 btw17 requested a review from cpcallen January 13, 2023 23:30
@github-actions github-actions bot added the PR: feature Adds a feature label Jan 13, 2023
@btw17 btw17 force-pushed the feat/add-ts-tests branch from cdd9888 to 464f700 Compare January 13, 2023 23:31
@github-actions github-actions bot added PR: feature Adds a feature and removed PR: feature Adds a feature labels Jan 13, 2023
Copy link
Contributor

@cpcallen cpcallen left a comment

Choose a reason for hiding this comment

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

Yay for more tests!

.eslintignore Outdated
@@ -11,6 +11,7 @@
/tests/screenshot/*
/tests/test_runner.js
/tests/workspace_svg/*
/tests/typescript/*
Copy link
Contributor

Choose a reason for hiding this comment

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

How much work would it be to get the new tests to lint cleanly?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure how straightforward it will be, though I'll look further into it this upcoming Friday. And get back to you when I've looked into it a bit more.

The configuration in .eslintrc.json points to the root ./tsconfig.json via parserOptions which I believe was the main issue, though there may be around that. These tests need their own tsconfig so they aren't built and packaged with the rest of the code.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated! The tsconfig is defined in an array, which I hadn't noticed at my quick glance earlier, so I just had to add the second override.

Of note, I updated .eslintrc.json to .eslintrc.js since we likely want the same settings for the overrides, and it's easier to keep them in sync via JS than in JSON.

tests/typescript/README.md Outdated Show resolved Hide resolved
scripts/gulpfiles/test_tasks.js Outdated Show resolved Hide resolved
scripts/gulpfiles/test_tasks.js Outdated Show resolved Hide resolved
scripts/gulpfiles/test_tasks.js Outdated Show resolved Hide resolved
tests/typescript/src/field/different_user_input.ts Outdated Show resolved Hide resolved
tests/typescript/src/field/different_user_input.ts Outdated Show resolved Hide resolved
tests/typescript/src/field/different_user_input.ts Outdated Show resolved Hide resolved
tests/typescript/tsconfig.json Show resolved Hide resolved
@btw17
Copy link
Member Author

btw17 commented Jan 20, 2023

Should be all set! Please check my changes which update .eslintrc.json to .eslintrc.js. Other than restructuring the file for JS, I did the following updates:

  • Added an ignore pattern for .eslint.js (which was being picked up otherwise and triggered an error due to using module.exports)
  • Modified the file pattern for the override connected to ./tsconfig.json to just refer to ./core TS files instead of all TS files
  • Added the override for ./tests/typescript/tsconfig.json

@btw17 btw17 force-pushed the feat/add-ts-tests branch 4 times, most recently from e4085f2 to 879bfd5 Compare January 27, 2023 16:49
Copy link
Contributor

@cpcallen cpcallen left a comment

Choose a reason for hiding this comment

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

In addition to changing the tsc output directory (see below), can you update .eslintrc.js to follow the JS style guide, please? E.g.:

  • Indentation by two spaces.
  • Use single quotes for strings, no quoting of property names that are valid identifieres,
  • Ends with \n.

You can probably just run clang-format on it. Better: modify the config so that this file is always formatted when npm run format is run.

*/
function typeDefinitions() {
return runTestCommand('type_definitions',
'tsc -p ./tests/typescript/tsconfig.json');
Copy link
Contributor

Choose a reason for hiding this comment

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

Please write output to somewhere in build/ rather than in tests/.

I think it would be preferable to do so using -outDir and path.join(BUILD_DIR, 'tests' /* optionally more */) as buildJavaScript (in build_tasks.js) does.

.eslintignore Outdated
@@ -11,6 +11,7 @@
/tests/screenshot/*
/tests/test_runner.js
/tests/workspace_svg/*
/tests/typescript/dist/*
Copy link
Contributor

Choose a reason for hiding this comment

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

This should not be necessary.

Copy link
Member Author

Choose a reason for hiding this comment

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

When this is not ignored, I get an error like below:

5:1  error  Parsing error: 'import' and 'export' may appear only with 'sourceType: module'

Basically it's trying to run lint on the JS files that are created in /tests/typescript/dist

.eslintrc.js Outdated Show resolved Hide resolved
@btw17 btw17 force-pushed the feat/add-ts-tests branch from 879bfd5 to 19f0a55 Compare February 3, 2023 19:00
.eslintrc.js Outdated
Comment on lines 188 to 192
'extends' :
[
'eslint:recommended',
'google',
],
Copy link
Contributor

Choose a reason for hiding this comment

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

Go home formatter, you're drunk:

Suggested change
'extends' :
[
'eslint:recommended',
'google',
],
extends: [
'eslint:recommended',
'google',
],

(and similarly for overrides.)

@@ -0,0 +1,215 @@
const rules = {
'curly': ['error'],
Copy link
Contributor

Choose a reason for hiding this comment

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

Single quotes not required for property names that are valid identifiers:

Suggested change
'curly': ['error'],
curly: ['error'],

(and many below).

Copy link
Member Author

Choose a reason for hiding this comment

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

done

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like the linter wants consistently quoted properties. In TypeScript, it's best practice to quote keys you don't know at compile time. For example:

interface Test {
  knownKey: string;
  [key: string]: number;
}
const test: Test = {
  knownKey: 'value',
  'unknownOne': 1,
  'unknownTwo': 2
};

With that principle in mind, maybe it's valid to keep the keys that are defined by eslint outside of this file quoted.

Copy link
Contributor

Choose a reason for hiding this comment

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

In TypeScript, it's best practice to quote keys you don't know at compile time.

TIL. Though I'm not sure that argument is exactly applicable here, since I'd guess that the format of the tsconfig data structure is probably fully defined somewhere in the tsc source code.

scripts/gulpfiles/build_tasks.js Show resolved Hide resolved
Comment on lines 41 to 42
// Directory where typescript compiler can be found for `./tests/typescript`
// files.
Copy link
Contributor

Choose a reason for hiding this comment

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

This description seems confusing to me. How about:

Suggested change
// Directory where typescript compiler can be found for `./tests/typescript`
// files.
// Directory for files generated by compiling test code.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@btw17 btw17 force-pushed the feat/add-ts-tests branch from 0e93012 to 92d4dd8 Compare February 10, 2023 15:51
@@ -0,0 +1,215 @@
const rules = {
'curly': ['error'],
Copy link
Contributor

Choose a reason for hiding this comment

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

In TypeScript, it's best practice to quote keys you don't know at compile time.

TIL. Though I'm not sure that argument is exactly applicable here, since I'd guess that the format of the tsconfig data structure is probably fully defined somewhere in the tsc source code.

@cpcallen cpcallen merged commit 13fe6ee into google:develop Feb 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: feature Adds a feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants