From 377ba1471e56c92ce9af049ad2df358e1c439686 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sat, 17 Apr 2021 16:38:45 +1000 Subject: [PATCH] support es6 imports in Jest tests Switching to lodash-es caused Jest to fail. The standard Jest workflow would be to transpile things with Bazel, but we can do it faster by bundling with esbuild. the log in lib.test.ts has revealed numbers are being set as Long instead of JS numbers, and the published workaround for it is not working :-( --- ts/deckconfig/BUILD.bazel | 4 +- ts/deckconfig/lib.test.ts | 93 +++++++++++++++++++++++++++++++++++++++ ts/deckconfig/lib.ts | 8 ++-- ts/jest.bzl | 36 ++++++++++++--- ts/lib/BUILD.bazel | 3 -- 5 files changed, 130 insertions(+), 14 deletions(-) create mode 100644 ts/deckconfig/lib.test.ts diff --git a/ts/deckconfig/BUILD.bazel b/ts/deckconfig/BUILD.bazel index 906fb074b..7b8721451 100644 --- a/ts/deckconfig/BUILD.bazel +++ b/ts/deckconfig/BUILD.bazel @@ -126,11 +126,9 @@ svelte_check( ) jest_test( - data = [ - "@npm//protobufjs", - ], deps = [ ":lib", "//ts/lib:backend_proto", + "@npm//protobufjs", ], ) diff --git a/ts/deckconfig/lib.test.ts b/ts/deckconfig/lib.test.ts new file mode 100644 index 000000000..5ee341770 --- /dev/null +++ b/ts/deckconfig/lib.test.ts @@ -0,0 +1,93 @@ +// Copyright: Ankitects Pty Ltd and contributors +// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html + +import * as pb from "anki/backend_proto"; +import { DeckConfigState } from "./lib"; + +const exampleData = { + allConfig: [ + { + config: { + id: "1", + name: "Default", + mtimeSecs: "1618570764", + usn: -1, + config: { + learnSteps: [1, 10], + relearnSteps: [10], + newPerDay: 20, + reviewsPerDay: 200, + initialEase: 2.5, + easyMultiplier: 1.2999999523162842, + hardMultiplier: 1.2000000476837158, + intervalMultiplier: 1, + maximumReviewInterval: 36500, + minimumLapseInterval: 1, + graduatingIntervalGood: 1, + graduatingIntervalEasy: 4, + leechAction: "LEECH_ACTION_TAG_ONLY", + leechThreshold: 8, + capAnswerTimeToSecs: 60, + }, + }, + useCount: 1, + }, + { + config: { + id: "1618570764780", + name: "another one", + mtimeSecs: "1618570781", + usn: -1, + config: { + learnSteps: [1, 10, 20, 30], + relearnSteps: [10], + newPerDay: 40, + reviewsPerDay: 200, + initialEase: 2.5, + easyMultiplier: 1.2999999523162842, + hardMultiplier: 1.2000000476837158, + intervalMultiplier: 1, + maximumReviewInterval: 36500, + minimumLapseInterval: 1, + graduatingIntervalGood: 1, + graduatingIntervalEasy: 4, + leechAction: "LEECH_ACTION_TAG_ONLY", + leechThreshold: 8, + capAnswerTimeToSecs: 60, + }, + }, + useCount: 1, + }, + ], + currentDeck: { + name: "Default::child", + configId: "1618570764780", + parentConfigIds: [1], + }, + defaults: { + config: { + learnSteps: [1, 10], + relearnSteps: [10], + newPerDay: 20, + reviewsPerDay: 200, + initialEase: 2.5, + easyMultiplier: 1.2999999523162842, + hardMultiplier: 1.2000000476837158, + intervalMultiplier: 1, + maximumReviewInterval: 36500, + minimumLapseInterval: 1, + graduatingIntervalGood: 1, + graduatingIntervalEasy: 4, + leechAction: "LEECH_ACTION_TAG_ONLY", + leechThreshold: 8, + capAnswerTimeToSecs: 60, + }, + }, +}; + +test("create", () => { + const empty = pb.BackendProto.DeckConfigForUpdate.fromObject(exampleData); + console.log(empty); + const state = new DeckConfigState(empty); + expect(state.currentDeck.name).toBe("Default::child"); +}); diff --git a/ts/deckconfig/lib.ts b/ts/deckconfig/lib.ts index 232c92a98..b58b4e739 100644 --- a/ts/deckconfig/lib.ts +++ b/ts/deckconfig/lib.ts @@ -62,9 +62,11 @@ export class DeckConfigState { useCount: config.useCount!, }; }); - this.selectedIdx = - this.configs.findIndex((c) => c.config.id === this.currentDeck.configId) ?? - 0; + this.selectedIdx = Math.max( + 0, + this.configs.findIndex((c) => c.config.id === this.currentDeck.configId) + ); + // decrement the use count of the starting item, as we'll apply +1 to currently // selected one at display time this.configs[this.selectedIdx].useCount -= 1; diff --git a/ts/jest.bzl b/ts/jest.bzl index 5bb05b010..5932b017b 100644 --- a/ts/jest.bzl +++ b/ts/jest.bzl @@ -1,19 +1,46 @@ load("@npm//@bazel/typescript:index.bzl", "ts_library") +load("@esbuild_toolchain//:esbuild.bzl", esbuild = "esbuild_macro") load("@npm//jest-cli:index.bzl", _jest_test = "jest_test") -def jest_test(deps, data = [], name = "jest"): +def jest_test(deps, name = "jest"): "Build *.test.ts into a library, then test it with Jest." - # compile sources + ts_sources = native.glob(["*.test.ts"]) + + # compile sources for type checking ts_library( name = name + "_lib", - srcs = native.glob(["*.test.ts"]), + srcs = ts_sources, tsconfig = "//ts:tsconfig.json", deps = deps + [ "@npm//@types/jest", ], ) + # bundle each test file up with its dependencies for jest + bundled_srcs = [] + for ts_src in ts_sources: + base = ts_src.replace(".test.ts", "") + bundle_name = base + ".bundle.test" + bundled_srcs.append(bundle_name) + esbuild( + name = bundle_name, + args = [ + "--resolve-extensions=.mjs,.js", + "--log-level=warning", + "--platform=node", + "--external:protobufjs", + "--keep-names", + ], + entry_point = ts_src, + output = bundle_name + ".js", + deps = [ + name + "_lib", + ] + deps, + # the code shaking saves close to a second off the deckconfig/lib.test.ts test + minify = True, + ) + # then test them _jest_test( name = name, @@ -25,8 +52,7 @@ def jest_test(deps, data = [], name = "jest"): "--config", "$(location //ts:jest.config.js)", ], - data = data + [ - name + "_lib", + data = bundled_srcs + [ "//ts:jest.config.js", ], target_compatible_with = select({ diff --git a/ts/lib/BUILD.bazel b/ts/lib/BUILD.bazel index 01d538429..d67b4f870 100644 --- a/ts/lib/BUILD.bazel +++ b/ts/lib/BUILD.bazel @@ -78,9 +78,6 @@ eslint_test( ) jest_test( - data = [ - "@npm//protobufjs", - ], deps = [ ":lib", ],