From a3fb9e67e4c492dbfb69dbcd9a4e776099d5e277 Mon Sep 17 00:00:00 2001 From: adrianmroz <78143552+adrianmroz-allegro@users.noreply.github.com> Date: Wed, 30 Mar 2022 14:19:32 +0200 Subject: [PATCH] Typescript fixes in package.json (#887) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add cypress types globally for tsc * Bump @types/node * Add globalThis shim for cypress package. * Add es6 types and iterator option for more modern syntax in e2e tests * Fixed types * Real tests for TimeTag * Add .nvmrc * New package scripts. Move checking client types to lint stage. Add watch scripts for checking types in all sub-packages. * Update package.json Co-authored-by: Paweł Lesiecki * Fix lint:types arguments * Pin advised version of setup-gcloud action * remove local .nvmrc * Fix scripts in package.json Co-authored-by: Paweł Lesiecki --- .github/workflows/deploy-branch.yml | 2 +- .github/workflows/deploy-master.yml | 2 +- cypress/index.d.ts | 18 ++++++++++ cypress/integration/split-tile.spec.ts | 10 +++--- cypress/tsconfig.json | 15 +++++++-- package-lock.json | 6 ++-- package.json | 19 ++++++----- src/common/models/time-tag/time-tag.mocha.ts | 35 +++++++++++--------- src/common/models/time-tag/time-tag.ts | 10 +----- src/server/www.ts | 3 +- tsconfig.json | 2 +- 11 files changed, 72 insertions(+), 50 deletions(-) create mode 100644 cypress/index.d.ts diff --git a/.github/workflows/deploy-branch.yml b/.github/workflows/deploy-branch.yml index 1274151e0..acc9525f8 100644 --- a/.github/workflows/deploy-branch.yml +++ b/.github/workflows/deploy-branch.yml @@ -18,7 +18,7 @@ jobs: uses: actions/checkout@v2 - name: Authenticate on GCP - uses: google-github-actions/setup-gcloud@master + uses: google-github-actions/setup-gcloud@v0 with: project_id: ${{ secrets.GCP_PROJECT_ID }} service_account_key: ${{ secrets.GCP_SA_KEY }} diff --git a/.github/workflows/deploy-master.yml b/.github/workflows/deploy-master.yml index ea7d7c943..d9b4956e6 100644 --- a/.github/workflows/deploy-master.yml +++ b/.github/workflows/deploy-master.yml @@ -18,7 +18,7 @@ jobs: uses: actions/checkout@v2 - name: Authenticate on GCP - uses: google-github-actions/setup-gcloud@master + uses: google-github-actions/setup-gcloud@v0 with: project_id: ${{ secrets.GCP_PROJECT_ID }} service_account_key: ${{ secrets.GCP_SA_KEY }} diff --git a/cypress/index.d.ts b/cypress/index.d.ts new file mode 100644 index 000000000..ee681790b --- /dev/null +++ b/cypress/index.d.ts @@ -0,0 +1,18 @@ +/* + * Copyright 2017-2022 Allegro.pl + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Remove after updating to tsc 3.4 +declare const globalThis: any; diff --git a/cypress/integration/split-tile.spec.ts b/cypress/integration/split-tile.spec.ts index 5d03adb6c..6d78fcae9 100644 --- a/cypress/integration/split-tile.spec.ts +++ b/cypress/integration/split-tile.spec.ts @@ -224,11 +224,10 @@ context("Split Tile", () => { splitsContainer().trigger("dragenter"); - // @ts-ignore Fix types! splitTile("Channel").then(([channelSplit]) => { - const { x, width } = channelSplit.getBoundingClientRect(); + const { left, width } = channelSplit.getBoundingClientRect(); - dragMask().trigger("drop", { clientX: x + width / 2 }); + dragMask().trigger("drop", { clientX: left + width / 2 }); shouldHaveSplits("Page"); }); @@ -252,11 +251,10 @@ context("Split Tile", () => { splitsContainer().trigger("dragenter"); splitTile("Page") - // @ts-ignore Fix types! .then(([timeSplit]) => { - const { x, width } = timeSplit.getBoundingClientRect(); + const { left, width } = timeSplit.getBoundingClientRect(); - dragMask().trigger("drop", { clientX: x + width }); + dragMask().trigger("drop", { clientX: left + width }); shouldHaveSplits("Page", "Channel", "City Name"); }); diff --git a/cypress/tsconfig.json b/cypress/tsconfig.json index f87a578b6..cd446a3bb 100644 --- a/cypress/tsconfig.json +++ b/cypress/tsconfig.json @@ -1,8 +1,17 @@ { "compilerOptions": { + "downlevelIteration": true, "target": "es5", - "lib": ["es5", "dom"], - "types": ["cypress"] + "lib": [ + "es5", + "es6", + "dom" + ], + "types": [ + "cypress" + ] }, - "include": ["**/*.ts"] + "include": [ + "**/*.ts" + ] } diff --git a/package-lock.json b/package-lock.json index 02733978b..47e947980 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2847,9 +2847,9 @@ } }, "@types/node": { - "version": "8.5.2", - "resolved": "https://registry.npmjs.org/@types/node/-/node-8.5.2.tgz", - "integrity": "sha512-KA4GKOpgXnrqEH2eCVhiv2CsxgXGQJgV1X0vsGlh+WCnxbeAE1GT44ZsTU1IN5dEeV/gDupKa7gWo08V5IxWVQ==", + "version": "14.0.4", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.0.4.tgz", + "integrity": "sha512-k3NqigXWRzQZVBDS5D1U70A5E8Qk4Kh+Ha/x4M8Bt9pF0X05eggfnC9+63Usc9Q928hRUIpIhTQaXsZwZBl4Ew==", "dev": true }, "@types/nopt": { diff --git a/package.json b/package.json index 120576cdc..82961566a 100644 --- a/package.json +++ b/package.json @@ -41,26 +41,27 @@ "test:common": "mocha --exit --require ./test/setup/mocha.js 'src/common/**/*.mocha.{ts,tsx}'", "test:client": "mocha --exit --require ./test/setup/mocha.js 'src/client/**/*.mocha.{ts,tsx}' --file 'src/client/utils/test-utils/jsdom-setup.ts'", "test:server": "mocha --exit --require ./test/setup/mocha.js 'src/server/**/*.mocha.ts'", - "test:client-types": "npm run tsc -- --noEmit -p src/client/tsconfig.json", - "test:size": "size-limit", "watch:test:common": "npm run test:common -- --watch --watch-files 'src/common/**/*.mocha.ts'", "watch:test:client": "npm run test:client -- --watch --watch-files 'src/client/**/*.mocha.tsx' --watch-files 'src/client/**/*.mocha.ts'", "watch:test:server": "npm run test:server -- --watch --watch-files 'src/server/**/*.mocha.ts'", - "test-watcher": "mocha --require ./test/setup/mocha.js --watch", "start": "./bin/turnilo", - "start:dev": "NODE_ENV=dev-hmr ./bin/turnilo", "start:examples": "npm run start -- --examples", + "start:dev": "NODE_ENV=dev-hmr ./bin/turnilo", "start:dev:examples": "npm run start:dev -- --examples", "build:client": "webpack --config config/webpack.prod.js", - "build:server": "npm run tsc -- -p src/server/tsconfig.json", + "build:server": "tsc -p src/server/tsconfig.json", "build": "npm-run-all -s clean build:server build:client", "clean": "rimraf build/*", - "tsc": "tsc", - "tsc:watch": "tsc -p tsconfig.json -w --pretty", + "watch:types:server": "tsc -p src/server/tsconfig.json -w --pretty --noEmit", + "watch:types:client": "tsc -p src/client/tsconfig.json -w --pretty --noEmit", + "watch:types:e2e": "tsc -p cypress/tsconfig.json -w --pretty --noEmit", + "watch:types:common": "tsc -p src/common/tsconfig.json -w --pretty --noEmit", "lint": "npm-run-all -p lint:*", + "lint:types": "tsc --noEmit -p src/client/tsconfig.json", "lint:ts": "tslint -p . --format verbose", - "lint:ts:fix": "tslint -p . --fix", "lint:sass": "sass-lint -v", + "fix": "npm-run-all -s fix:*", + "fix:ts": "tslint -p . --fix", "e2e": "start-server-and-test start:examples http://localhost:9090/health/ready 'cypress run'", "e2e:dev": "cypress open", "check": "npm-run-all -c -p lint test build e2e" @@ -133,7 +134,7 @@ "@types/mocha": "2.2.46", "@types/moment-timezone": "0.5.12", "@types/nock": "9.1.3", - "@types/node": "8.5.2", + "@types/node": "14.0.4", "@types/nopt": "3.0.29", "@types/randomstring": "1.1.6", "@types/react": "16.14.24", diff --git a/src/common/models/time-tag/time-tag.mocha.ts b/src/common/models/time-tag/time-tag.mocha.ts index 177256416..553764684 100644 --- a/src/common/models/time-tag/time-tag.mocha.ts +++ b/src/common/models/time-tag/time-tag.mocha.ts @@ -15,24 +15,27 @@ * limitations under the License. */ -import { testImmutableClass } from "immutable-class-tester"; -import { TimeTag, TimeTagJS } from "./time-tag"; +import { expect } from "chai"; +import { TimeTag } from "./time-tag"; describe("TimeTag", () => { - // TODO: reimplement this test as simpler cases without immutable-class-tester - it checks too much - it.skip("is an immutable class", () => { - testImmutableClass(TimeTag, [ - { - name: "dodo", - time: new Date("2015-10-15T19:20:00Z"), - lastTimeChecked: new Date("2015-10-15T19:20:13Z") - }, - { - name: "wikipedia", - time: new Date("2015-10-15T19:21:00Z"), - lastTimeChecked: new Date("2015-10-15T19:21:13Z") - } - ]); + it("should read passed name values", () => { + const timeTag = TimeTag.fromJS({ name: "foobar" }); + expect(timeTag.name).to.be.equal("foobar"); }); + it("should parse passed time value", () => { + const timeTag = TimeTag.fromJS({ name: "foobar", time: "2020-01-01" }); + expect(timeTag.time).to.be.deep.equal(new Date("2020-01-01")); + }); + + it("should parse passed lastTimeChecked value", () => { + const timeTag = TimeTag.fromJS({ name: "foobar", lastTimeChecked: "2020-01-01" }); + expect(timeTag.lastTimeChecked).to.be.deep.equal(new Date("2020-01-01")); + }); + + it("should use time value if lastTimeChecked is missing", () => { + const timeTag = TimeTag.fromJS({ name: "foobar", time: "2020-01-01" }); + expect(timeTag.lastTimeChecked).to.be.deep.equal(new Date("2020-01-01")); + }); }); diff --git a/src/common/models/time-tag/time-tag.ts b/src/common/models/time-tag/time-tag.ts index e372ee9cc..4c4923e5e 100644 --- a/src/common/models/time-tag/time-tag.ts +++ b/src/common/models/time-tag/time-tag.ts @@ -37,10 +37,6 @@ const defaultTimeTag: TimeTagValue = { export class TimeTag extends Record(defaultTimeTag) { - static isTimeTag(candidate: any): candidate is TimeTag { - return candidate instanceof TimeTag; - } - static fromJS({ name, time: timeJS, lastTimeChecked: lastTimeCheckedJS }: TimeTagJS): TimeTag { const time = timeJS ? new Date(timeJS) : undefined; const lastTimeChecked = lastTimeCheckedJS ? new Date(lastTimeCheckedJS) : time; @@ -56,10 +52,6 @@ export class TimeTag extends Record(defaultTimeTag) { } public changeTime(time: Date, lastTimeChecked: Date): TimeTag { - return new TimeTag({ - name: this.name, - time, - lastTimeChecked - }); + return this.set("time", time).set("lastTimeChecked", lastTimeChecked); } } diff --git a/src/server/www.ts b/src/server/www.ts index 8a61d3578..3d712abc3 100755 --- a/src/server/www.ts +++ b/src/server/www.ts @@ -17,6 +17,7 @@ import * as http from "http"; +import { AddressInfo } from "net"; import app from "./app"; import { SERVER_SETTINGS, START_SERVER } from "./config"; @@ -46,7 +47,7 @@ if (START_SERVER) { }); server.on("listening", () => { - var address = server.address(); + const address = server.address() as AddressInfo; console.log(`Turnilo is listening on address ${address.address} port ${address.port}`); }); diff --git a/tsconfig.json b/tsconfig.json index 6e38b349d..3d0c9c535 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,6 +17,6 @@ "esModuleInterop": true, "jsx": "react", "lib": ["dom", "es6", "es5"], - "types": ["node", "express", "mocha", "webpack-env"] + "types": ["node", "express", "mocha", "cypress", "webpack-env"] } }