Skip to content

[Bug]: type-only imports cause ReferenceError #1199

@Simon-Hayden-Dev

Description

@Simon-Hayden-Dev

Version

11.0.1

Steps to reproduce

@leosvelperez created a repo showing the failure: https://github.com/leosvelperez/ng-jest-type-issue

Just clone the repo, install depdencies and execute the tests using jest.

(For reference, this bug was originally reported to the Nx team here: nrwl/nx#7845)

Expected behavior

Tests which use import type should pass. See the official Typescript documentation.

Actual behavior

An error is thrown:

  ● AppService › should be created

    ReferenceError: Request is not defined

      1 | import { Inject, Injectable, Optional } from '@angular/core';
    > 2 | import type { Request } from 'express';
        |               ^
      3 | import { REQUEST } from './app.tokens';
      4 |
      5 | @Injectable({

Additional context

The context of this issue is, that we are using Angular Universal for Server Side Rendering. Hence we sometimes have to inject the Browser's request/response into a service or similar. Since we do not want to accidentally bundle express into our Angular app, we want to use type-only imports.

Environment

npx: installed 1 in 0.89s

  System:
    OS: Linux 5.14 Fedora Linux 35 (Workstation Edition)
    CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
  Binaries:
    Node: 14.17.6 - ~/.nvm/versions/node/v14.17.6/bin/node
    Yarn: 1.22.15 - ~/.nvm/versions/node/v14.17.6/bin/yarn
    npm: 6.14.15 - ~/.nvm/versions/node/v14.17.6/bin/npm
  npmPackages:
    jest: ^27.3.1 => 27.3.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions