-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade the jest test suite to match the clients (#379)
* Upgrade the jest test suite to match the clients * Update makeStaticByteArray
- Loading branch information
Showing
24 changed files
with
379 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { pathsToModuleNameMapper } = require("ts-jest"); | ||
|
||
const { compilerOptions } = require("./tsconfig"); | ||
|
||
/** @type {import('jest').Config} */ | ||
module.exports = { | ||
reporters: ["default", "jest-junit"], | ||
|
||
collectCoverage: true, | ||
coverageReporters: ["html", "lcov"], | ||
coverageDirectory: "coverage", | ||
|
||
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", | ||
], | ||
|
||
// 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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { webcrypto } from "crypto"; | ||
import "jest-preset-angular/setup-jest"; | ||
|
||
Object.defineProperty(window, "CSS", { value: null }); | ||
Object.defineProperty(window, "getComputedStyle", { | ||
value: () => { | ||
return { | ||
display: "none", | ||
appearance: ["-webkit-appearance"], | ||
}; | ||
}, | ||
}); | ||
|
||
Object.defineProperty(document, "doctype", { | ||
value: "<!DOCTYPE html>", | ||
}); | ||
Object.defineProperty(document.body.style, "transform", { | ||
value: () => { | ||
return { | ||
enumerable: true, | ||
configurable: true, | ||
}; | ||
}, | ||
}); | ||
|
||
Object.defineProperty(window, "crypto", { | ||
value: webcrypto, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./matchers"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./to-equal-buffer"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { makeStaticByteArray } from "../utils"; | ||
|
||
describe("toEqualBuffer custom matcher", () => { | ||
it("matches identical ArrayBuffers", () => { | ||
const array = makeStaticByteArray(10); | ||
expect(array.buffer).toEqualBuffer(array.buffer); | ||
}); | ||
|
||
it("matches an identical ArrayBuffer and Uint8Array", () => { | ||
const array = makeStaticByteArray(10); | ||
expect(array.buffer).toEqualBuffer(array); | ||
}); | ||
|
||
it("doesn't match different ArrayBuffers", () => { | ||
const array1 = makeStaticByteArray(10); | ||
const array2 = makeStaticByteArray(10, 11); | ||
expect(array1.buffer).not.toEqualBuffer(array2.buffer); | ||
}); | ||
|
||
it("doesn't match a different ArrayBuffer and Uint8Array", () => { | ||
const array1 = makeStaticByteArray(10); | ||
const array2 = makeStaticByteArray(10, 11); | ||
expect(array1.buffer).not.toEqualBuffer(array2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* The inbuilt toEqual() matcher will always return TRUE when provided with 2 ArrayBuffers. | ||
* This is because an ArrayBuffer must be wrapped in a new Uint8Array to be accessible. | ||
* This custom matcher will automatically instantiate a new Uint8Array on the received value | ||
* (and optionally, the expected value) and then call toEqual() on the resulting Uint8Arrays. | ||
*/ | ||
export const toEqualBuffer: jest.CustomMatcher = function ( | ||
received: ArrayBuffer | Uint8Array, | ||
expected: ArrayBuffer | Uint8Array, | ||
) { | ||
received = new Uint8Array(received); | ||
expected = new Uint8Array(expected); | ||
|
||
if (this.equals(received, expected)) { | ||
return { | ||
message: () => `expected | ||
${received} | ||
not to match | ||
${expected}`, | ||
pass: true, | ||
}; | ||
} | ||
|
||
return { | ||
message: () => `expected | ||
${received} | ||
to match | ||
${expected}`, | ||
pass: false, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { webcrypto } from "crypto"; | ||
|
||
import { toEqualBuffer } from "./spec"; | ||
|
||
Object.defineProperty(window, "crypto", { | ||
value: webcrypto, | ||
}); | ||
|
||
// Add custom matchers | ||
|
||
expect.extend({ | ||
toEqualBuffer: toEqualBuffer, | ||
}); | ||
|
||
export interface CustomMatchers<R = unknown> { | ||
toEqualBuffer(expected: Uint8Array | ArrayBuffer): R; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as ts from "typescript"; | ||
|
||
// Custom Typescript AST transformer for use with ts-jest / jest-preset-angular | ||
// Removes specified ES2020 syntax from source code, as node does not support it yet | ||
// Reference: https://kulshekhar.github.io/ts-jest/docs/getting-started/options/astTransformers | ||
// Use this tool to understand how we identify and filter AST nodes: https://ts-ast-viewer.com/ | ||
|
||
/** | ||
* Remember to increase the version whenever transformer's content is changed. This is to inform Jest to not reuse | ||
* the previous cache which contains old transformer's content | ||
*/ | ||
export const version = 1; | ||
export const name = "bit-es2020-transformer"; | ||
|
||
// Returns true for 'import.meta' statements | ||
const isImportMetaStatement = (node: ts.Node) => | ||
ts.isPropertyAccessExpression(node) && | ||
ts.isMetaProperty(node.expression) && | ||
node.expression.keywordToken === ts.SyntaxKind.ImportKeyword; | ||
|
||
export const factory = function (/*opts?: Opts*/) { | ||
function visitor(ctx: ts.TransformationContext, sf: ts.SourceFile) { | ||
const visitor: ts.Visitor = (node: ts.Node): ts.VisitResult<any> => { | ||
if (isImportMetaStatement(node)) { | ||
return null; | ||
} | ||
|
||
// Continue searching child nodes | ||
return ts.visitEachChild(node, visitor, ctx); | ||
}; | ||
return visitor; | ||
} | ||
return (ctx: ts.TransformationContext): ts.Transformer<any> => { | ||
return (sf: ts.SourceFile) => ts.visitNode(sf, visitor(ctx, sf)); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* eslint-env node */ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
const { defaultTransformerOptions } = require("jest-preset-angular/presets"); | ||
|
||
/** @type {import('jest').Config} */ | ||
module.exports = { | ||
testMatch: ["**/+(*.)+(spec).+(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: { | ||
"^.+\\.(ts|js|mjs|svg)$": [ | ||
"jest-preset-angular", | ||
{ | ||
...defaultTransformerOptions, | ||
// Jest does not use tsconfig.spec.json by default | ||
tsconfig: "<rootDir>/tsconfig.spec.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, | ||
astTransformers: { | ||
before: ["<rootDir>/../../jslib/shared/es2020-transformer.ts"], | ||
}, | ||
}, | ||
], | ||
}, | ||
}; |
Oops, something went wrong.