Skip to content

Commit

Permalink
[AC-1743] pt. 2.5 ⮕ Fix the unit tests for good (#398)
Browse files Browse the repository at this point in the history
* Remove uneeded redirects to a tsconfig.spec.json

* Got the tests running again

* Delete webCryptoFunctionService

* Delete unused utils tests

* Add a test workflow
  • Loading branch information
addisonbeck authored Jan 12, 2024
1 parent d1cb92b commit 39ed935
Show file tree
Hide file tree
Showing 27 changed files with 90 additions and 1,300 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
name: Run tests

on:
workflow_dispatch:
push:
branches:
- "main"
- "rc"
- "hotfix-rc-*"
pull_request: {}

defaults:
run:
shell: bash

jobs:
test:
name: Run tests
runs-on: ubuntu-22.04
steps:
- name: Checkout repo
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

- name: Get Node Version
id: retrieve-node-version
run: |
NODE_NVMRC=$(cat .nvmrc)
NODE_VERSION=${NODE_NVMRC/v/''}
echo "node_version=$NODE_VERSION" >> $GITHUB_OUTPUT
- name: Set up Node
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
with:
cache: 'npm'
cache-dependency-path: '**/package-lock.json'
node-version: ${{ steps.retrieve-node-version.outputs.node_version }}

- name: Print environment
run: |
node --version
npm --version
- name: Install Node dependencies
run: npm ci

# We use isolatedModules: true which disables typechecking in tests
# Tests in apps/ are typechecked when their app is built, so we just do it here for libs/
# See https://bitwarden.atlassian.net/browse/EC-497
- name: Run typechecking
run: npm run test:types --coverage

- name: Run tests
run: npm run test
45 changes: 30 additions & 15 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
const { pathsToModuleNameMapper } = require("ts-jest");

const { compilerOptions } = require("./tsconfig");

/** @type {import('jest').Config} */
const tsPreset = require("ts-jest/jest-preset");
const angularPreset = require("jest-preset-angular/jest-preset");
const { defaultTransformerOptions } = require("jest-preset-angular/presets");

/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
reporters: ["default", "jest-junit"],
// ...tsPreset,
// ...angularPreset,
preset: "jest-preset-angular",

collectCoverage: true,
coverageReporters: ["html", "lcov"],
coverageDirectory: "coverage",
testEnvironment: "jsdom",
testMatch: ["**/+(*.)+(spec).+(ts)"],

moduleNameMapper: pathsToModuleNameMapper(compilerOptions?.paths || {}, {
prefix: "<rootDir>/",
}),
projects: [
"<rootDir>/jslib/angular/jest.config.js",
"<rootDir>/jslib/common/jest.config.js",
"<rootDir>/jslib/electron/jest.config.js",
"<rootDir>/jslib/node/jest.config.js",
],
roots: ["<rootDir>"],
modulePaths: [compilerOptions.baseUrl],
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: "<rootDir>/" }),
setupFilesAfterEnv: ["<rootDir>/test.setup.ts"],

// Workaround for a memory leak that crashes tests in CI:
// https://github.com/facebook/jest/issues/9430#issuecomment-1149882002
// Also anecdotally improves performance when run locally
maxWorkers: 3,

transform: {
"^.+\\.tsx?$": [
"jest-preset-angular",
// 'ts-jest',
{
...defaultTransformerOptions,
tsconfig: "./tsconfig.json",
// Further workaround for memory leak, recommended here:
// https://github.com/kulshekhar/ts-jest/issues/1967#issuecomment-697494014
// Makes tests run faster and reduces size/rate of leak, but loses typechecking on test code
// See https://bitwarden.atlassian.net/browse/EC-497 for more info
isolatedModules: true,
},
],
},
};
16 changes: 0 additions & 16 deletions jslib/angular/jest.config.js

This file was deleted.

6 changes: 0 additions & 6 deletions jslib/angular/src/services/jslib-services.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ import { TwoFactorService } from "@/jslib/common/src/services/twoFactor.service"
import { UserVerificationService } from "@/jslib/common/src/services/userVerification.service";
import { UsernameGenerationService } from "@/jslib/common/src/services/usernameGeneration.service";
import { VaultTimeoutService } from "@/jslib/common/src/services/vaultTimeout.service";
import { WebCryptoFunctionService } from "@/jslib/common/src/services/webCryptoFunction.service";

import { AuthGuardService } from "./auth-guard.service";
import { BroadcasterService } from "./broadcaster.service";
Expand Down Expand Up @@ -420,11 +419,6 @@ import { ValidationService } from "./validation.service";
StateServiceAbstraction,
],
},
{
provide: CryptoFunctionServiceAbstraction,
useClass: WebCryptoFunctionService,
deps: ["WINDOW"],
},
{
provide: EventServiceAbstraction,
useClass: EventService,
Expand Down
15 changes: 0 additions & 15 deletions jslib/common/custom-matchers.d.ts

This file was deleted.

17 changes: 0 additions & 17 deletions jslib/common/jest.config.js

This file was deleted.

1 change: 0 additions & 1 deletion jslib/common/spec/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion jslib/common/spec/matchers/index.ts

This file was deleted.

25 changes: 0 additions & 25 deletions jslib/common/spec/matchers/to-equal-buffer.spec.ts

This file was deleted.

31 changes: 0 additions & 31 deletions jslib/common/spec/matchers/to-equal-buffer.ts

This file was deleted.

73 changes: 0 additions & 73 deletions jslib/common/spec/misc/utils.spec.ts

This file was deleted.

Loading

0 comments on commit 39ed935

Please sign in to comment.