Skip to content

ng build --prod not ignoring .spec.ts in subdirectories  #4673

Closed

Description

@natedanner

Please provide us with the following information:

OS

Mac OSX El Capitan 10.11.6

Versions.

@angular/cli: 1.0.0-beta.30
node: 6.9.1
os: darwin x64
@angular/common: 2.4.6
@angular/compiler: 2.4.6
@angular/core: 2.4.6
@angular/forms: 2.4.6
@angular/http: 2.4.6
@angular/platform-browser: 2.4.6
@angular/platform-browser-dynamic: 2.4.6
@angular/router: 3.4.6
@angular/cli: 1.0.0-beta.30
@angular/compiler-cli: 2.4.6

Repro steps.

  1. Create a test with @component test component declared inside the .spec.ts that is within a directory nested below src. i.e src/app/test
  2. Run ng build --prod

Was this an app that wasn't created using the CLI? What change did you do on your code? etc.
Upgraded from beta 28 to beta 30

The log given by the failure.

ERROR in Cannot determine the module for class TestSelectComponent in /Users/nate.danner/Desktop/aotSpecTest/src/app/test/app.component.spec.ts!
Cannot determine the module for class TestSelectComponent in /Users/nate.danner/Desktop/aotSpecTest/src/testing/test2/test-select.component.ts!

ERROR in ./src/main.ts
Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' in '/Users/nate.danner/Desktop/aotSpecTest/src'
@ ./src/main.ts 4:0-74
@ multi ./src/main.ts

Mention any other details that might be useful.

.spec.ts files lower than one directory from src are not ignored by the
angular-cli/packages/@angular/cli/models/webpack-configs/typescript.ts

// exclude is always set true
if (options.hasOwnProperty('exclude')) {
let exclude: string[] = typeof options.exclude == 'string'
? [options.exclude as string] : (options.exclude as string[]);

  exclude.forEach((pattern: string) => {
    const basePathPattern = '(' + basePath.replace(/\\/g, '/')
        .replace(/[\-\[\]\/{}()+?.\\^$|*]/g, '\\$&') + ')?';
    pattern = pattern
      // Replace windows path separators with forward slashes.
      .replace(/\\/g, '/')
      // Escape characters that are used normally in regexes, except stars.
      .replace(/[\-\[\]{}()+?.\\^$|]/g, '\\$&')
      // Two stars replacement.
      .replace(/\*\*/g, '(?:.*)')
      // One star replacement.
      .replace(/\*/g, '(?:[^/]*)')
      // Escape characters from the basePath and make sure it's forward slashes.
      .replace(/^/, basePathPattern);

    const re = new RegExp('^' + pattern + '$');
    fileNames = fileNames.filter(x => !x.replace(/\\/g, '/').match(re));
  });
} else {
  fileNames = fileNames.filter(fileName => !/\.spec\.ts$/.test(fileName));
}

if I comment out everything and use "fileNames = fileNames.filter(fileName => !/.spec.ts$/.test(fileName));" locally then the spec files are ignored as they were before.

  • .spec.ts files lower than one directory from src are not ignored by the AoT compilation
  • The second issue is components that are only used for testing and not included in the app.module.ts are also picked up by the AoT compilation and throw error.

Example Project
aotSpecTest.zip

Activity

  1. Luftzig commented on Feb 15, 2017

    @Luftzig

    Can confirm, using angular-cli 1.0.0-beta.30 and the project provided by @natedanner
    #goodnessSquad

  2. Luftzig commented on Feb 15, 2017

    @Luftzig

    @natedanner I removed the export keyword from the class declarations in the spec.ts files and your project builds correctly. You do not need to export these components because they are only used in the spec file itself.
    I think that's a real bug, the compiler shouldn't be reading the spec files anyway.
    #goodnessSquad

  3. hccampos commented on Feb 15, 2017

    @hccampos
  4. filipesilva commented on Mar 9, 2017

    @filipesilva
    Contributor

    Heya, this is solved in the latest RCs. You can follow https://github.com/angular/angular-cli/wiki/stories-rc-update to upgrade.

  5. angular-automatic-lock-bot commented on Sep 6, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  6. locked and limited conversation to collaborators on Sep 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions