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 :-(
This commit is contained in:
Damien Elmes 2021-04-17 16:38:45 +10:00
parent 8cf8c901fe
commit 377ba1471e
5 changed files with 130 additions and 14 deletions

View file

@ -126,11 +126,9 @@ svelte_check(
) )
jest_test( jest_test(
data = [
"@npm//protobufjs",
],
deps = [ deps = [
":lib", ":lib",
"//ts/lib:backend_proto", "//ts/lib:backend_proto",
"@npm//protobufjs",
], ],
) )

93
ts/deckconfig/lib.test.ts Normal file
View file

@ -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");
});

View file

@ -62,9 +62,11 @@ export class DeckConfigState {
useCount: config.useCount!, useCount: config.useCount!,
}; };
}); });
this.selectedIdx = this.selectedIdx = Math.max(
this.configs.findIndex((c) => c.config.id === this.currentDeck.configId) ?? 0,
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 // decrement the use count of the starting item, as we'll apply +1 to currently
// selected one at display time // selected one at display time
this.configs[this.selectedIdx].useCount -= 1; this.configs[this.selectedIdx].useCount -= 1;

View file

@ -1,19 +1,46 @@
load("@npm//@bazel/typescript:index.bzl", "ts_library") 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") 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." "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( ts_library(
name = name + "_lib", name = name + "_lib",
srcs = native.glob(["*.test.ts"]), srcs = ts_sources,
tsconfig = "//ts:tsconfig.json", tsconfig = "//ts:tsconfig.json",
deps = deps + [ deps = deps + [
"@npm//@types/jest", "@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 # then test them
_jest_test( _jest_test(
name = name, name = name,
@ -25,8 +52,7 @@ def jest_test(deps, data = [], name = "jest"):
"--config", "--config",
"$(location //ts:jest.config.js)", "$(location //ts:jest.config.js)",
], ],
data = data + [ data = bundled_srcs + [
name + "_lib",
"//ts:jest.config.js", "//ts:jest.config.js",
], ],
target_compatible_with = select({ target_compatible_with = select({

View file

@ -78,9 +78,6 @@ eslint_test(
) )
jest_test( jest_test(
data = [
"@npm//protobufjs",
],
deps = [ deps = [
":lib", ":lib",
], ],