Skip to content

Commit

Permalink
Add Jest test framework (#43)
Browse files Browse the repository at this point in the history
* Add Jest setup and write a test

* Code coverage?

* Update calculateOverriddenConfig test

* Throw if register gets bad input
  • Loading branch information
jameslittle230 authored May 26, 2020
1 parent 5a99369 commit 187885e
Show file tree
Hide file tree
Showing 8 changed files with 2,365 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ node_modules
/target
/pkg
/wasm-pack.log
coverage

scripts/*_exe.sh
test/federalist/*.txt
Expand Down
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ jobs:
- yarn install
script:
- yarn eslint js/*
- yarn test --coverage
after_success: |
bash <(curl -s https://codecov.io/bash) -cF javascript
- language: rust
rust: stable
Expand Down
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
modulePathIgnorePatterns: ["<rootDir>/target/*", "<rootDir>/pkg/*"],
collectCoverageFrom: ["js/**/*.{js,ts}"]
};
15 changes: 15 additions & 0 deletions js/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defaultConfig, calculateOverriddenConfig } from "./config";

test("correctly overrides default config", () => {
const expected = {
showProgress: true,
printIndexInfo: false,
showScores: true
};

const generated = calculateOverriddenConfig({
showScores: true
});

expect(generated).toMatchObject(expected);
});
4 changes: 2 additions & 2 deletions js/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface Configuration {
showScores: boolean;
}

const defaultConfig: Readonly<Configuration> = {
export const defaultConfig: Readonly<Configuration> = {
showProgress: true,
printIndexInfo: false,
showScores: false
Expand All @@ -16,7 +16,7 @@ export function calculateOverriddenConfig(
const output: Configuration = defaultConfig;

for (const key of Object.keys(defaultConfig) as Array<keyof Configuration>) {
if (overrides[key]) {
if (overrides[key] !== undefined) {
const overrideVal = overrides[key] as boolean;
output[key] = overrideVal;
}
Expand Down
31 changes: 28 additions & 3 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import init, {
import WasmQueue from "./wasmqueue";
import { Entity } from "./entity";
import { loadIndexFromUrl } from "./indexLoader";

import { defaultConfig, calculateOverriddenConfig } from "./config";
import { assert } from "./util";
import { defaultConfig } from "./config";

const prod = process.env.NODE_ENV === "production";
const wasmUrl = prod
Expand Down Expand Up @@ -174,8 +174,33 @@ function handleLoadedIndex(event, entity) {
}
}

function difference(arr1, arr2) {
const set1 = new Set(arr1);
const set2 = new Set(arr2);
const diff = new Set([...set1].filter(x => !set2.has(x)));
return diff;
}

export function register(name, url, config = {}) {
// assert that name is a string, url is a string, and config is a Partial<Configuration>
if (typeof name !== "string") {
throw new Error("Index registration name must be a string.");
}

if (typeof url !== "string") {
throw new Error("URL must be a string.");
}

let configKeyDiff = difference(
Object.keys(config),
Object.keys(defaultConfig)
);
if (configKeyDiff.size > 0) {
throw new Error(
`Invalid key${
configKeyDiff > 1 ? "s" : ""
} in config object: ${JSON.stringify(Array.from(configKeyDiff))}`
);
}

let entity = new Entity(name, url, config);
entities[name] = entity;
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"homepage": "https://stork-search.net",
"devDependencies": {
"@open-wc/webpack-import-meta-loader": "^0.4.1",
"@types/jest": "^25.2.3",
"@typescript-eslint/eslint-plugin": "^3.0.0",
"@typescript-eslint/parser": "^3.0.0",
"@wasm-tool/wasm-pack-plugin": "^1.2.0",
Expand All @@ -33,8 +34,10 @@
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-prettier": "^3.1.2",
"http-server": "^0.12.1",
"jest": "^26.0.1",
"prettier": "^2.0.1",
"source-map-loader": "^0.2.4",
"ts-jest": "^26.0.0",
"typescript": "~3.7.0",
"webpack": "^4.41.4",
"webpack-cli": "^3.3.10"
Expand All @@ -50,6 +53,7 @@
"build:js:prod": "yarn webpack --mode=production",
"build:prod": "yarn install && yarn build:wasm:prod && yarn build:test-index:federalist && yarn build:js:prod",
"lint": "yarn eslint js/*",
"test": "jest",
"serve": "yarn http-server ./dist -o -p 8025 -s -c-1"
},
"//": [
Expand Down
Loading

0 comments on commit 187885e

Please sign in to comment.