diff --git a/apps/web/src/app/app.module.ts b/apps/web/src/app/app.module.ts index 0f29ea14fa..5d1afd2122 100644 --- a/apps/web/src/app/app.module.ts +++ b/apps/web/src/app/app.module.ts @@ -6,7 +6,7 @@ import { BrowserAnimationsModule } from "@angular/platform-browser/animations"; import { InfiniteScrollModule } from "ngx-infinite-scroll"; import { AppComponent } from "./app.component"; -import { CoreModule } from "./core/core.module"; +import { CoreModule } from "./core"; import { OssRoutingModule } from "./oss-routing.module"; import { OssModule } from "./oss.module"; import { WildcardRoutingModule } from "./wildcard-routing.module"; diff --git a/apps/web/src/app/core/index.ts b/apps/web/src/app/core/index.ts index f9a92f361c..80c1a44d50 100644 --- a/apps/web/src/app/core/index.ts +++ b/apps/web/src/app/core/index.ts @@ -1,7 +1,4 @@ -// Do not export this here or it will import MultithreadEncryptService (via JslibServicesModule) into test code. -// MultithreadEncryptService contains ES2020 features (import.meta) which are not supported in Node and Jest. -// Revisit this when Node & Jest get stable support for ESM. -// export * from "./core.module"; +export * from "./core.module"; export * from "./event.service"; export * from "./policy-list.service"; export * from "./router.service"; diff --git a/bitwarden_license/bit-web/src/app/app.module.ts b/bitwarden_license/bit-web/src/app/app.module.ts index 9c8745d6b2..c26b04ff30 100644 --- a/bitwarden_license/bit-web/src/app/app.module.ts +++ b/bitwarden_license/bit-web/src/app/app.module.ts @@ -7,7 +7,7 @@ import { RouterModule } from "@angular/router"; import { InfiniteScrollModule } from "ngx-infinite-scroll"; import { JslibModule } from "@bitwarden/angular/jslib.module"; -import { CoreModule } from "@bitwarden/web-vault/app/core/core.module"; +import { CoreModule } from "@bitwarden/web-vault/app/core"; import { OssRoutingModule } from "@bitwarden/web-vault/app/oss-routing.module"; import { OssModule } from "@bitwarden/web-vault/app/oss.module"; import { WildcardRoutingModule } from "@bitwarden/web-vault/app/wildcard-routing.module"; diff --git a/libs/shared/es2020-transformer.ts b/libs/shared/es2020-transformer.ts new file mode 100644 index 0000000000..3a26e1c0c2 --- /dev/null +++ b/libs/shared/es2020-transformer.ts @@ -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 => { + if (isImportMetaStatement(node)) { + return null; + } + + // Continue searching child nodes + return ts.visitEachChild(node, visitor, ctx); + }; + return visitor; + } + return (ctx: ts.TransformationContext): ts.Transformer => { + return (sf: ts.SourceFile) => ts.visitNode(sf, visitor(ctx, sf)); + }; +}; diff --git a/libs/shared/jest.config.base.js b/libs/shared/jest.config.base.js index 056e54e9a7..e0eda1c4ca 100644 --- a/libs/shared/jest.config.base.js +++ b/libs/shared/jest.config.base.js @@ -19,6 +19,9 @@ module.exports = { // 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: ["/../../libs/shared/es2020-transformer.ts"], + }, }, }, };