11"use strict" ;
22Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
33exports . SingleTestTransformLoader = void 0 ;
4+ /**
5+ * @license
6+ * Copyright Google Inc. All Rights Reserved.
7+ *
8+ * Use of this source code is governed by an MIT-style license that can be
9+ * found in the LICENSE file at https://angular.io/license
10+ */
11+ const core_1 = require ( "@angular-devkit/core" ) ;
412const loader_utils_1 = require ( "loader-utils" ) ;
513const path_1 = require ( "path" ) ;
614exports . SingleTestTransformLoader = require . resolve ( path_1 . join ( __dirname , 'single-test-transform' ) ) ;
@@ -18,24 +26,19 @@ exports.SingleTestTransformLoader = require.resolve(path_1.join(__dirname, 'sing
1826 * array to import them directly, and thus run the tests there.
1927 */
2028function loader ( source ) {
21- const options = loader_utils_1 . getOptions ( this ) ;
22- const lineSeparator = process . platform === 'win32' ? '\r\n' : '\n' ;
23- const targettedImports = options . files
24- . map ( path => `require('./${ path . replace ( '.' + path_1 . extname ( path ) , '' ) } ');` )
25- . join ( lineSeparator ) ;
26- // TODO: maybe a documented 'marker/comment' inside test.ts would be nicer?
27- const regex = / r e q u i r e \. c o n t e x t \( .* / ;
28- // signal the user that expected content is not present
29- if ( ! regex . test ( source ) ) {
30- const message = [
31- `The 'include' option requires that the 'main' file for tests include the line below:` ,
32- `const context = require.context('./', true, /\.spec\.ts$/);` ,
33- `Arguments passed to require.context are not strict and can be changed` ,
34- ] ;
35- options . logger . error ( message . join ( lineSeparator ) ) ;
29+ const { files = [ ] , logger = console } = loader_utils_1 . getOptions ( this ) ;
30+ // signal the user that expected content is not present.
31+ if ( ! source . includes ( 'require.context(' ) ) {
32+ logger . error ( core_1 . tags . stripIndent `The 'include' option requires that the 'main' file for tests includes the below line:
33+ const context = require.context('./', true, /\.spec\.ts$/);
34+ Arguments passed to require.context are not strict and can be changed.` ) ;
35+ return source ;
3636 }
37- const mockedRequireContext = 'Object.assign(() => { }, { keys: () => [], resolve: () => undefined });' + lineSeparator ;
38- source = source . replace ( regex , mockedRequireContext + targettedImports ) ;
37+ const targettedImports = files
38+ . map ( path => `require('./${ path . replace ( '.' + path_1 . extname ( path ) , '' ) } ');` )
39+ . join ( '\n' ) ;
40+ const mockedRequireContext = 'Object.assign(() => { }, { keys: () => [], resolve: () => undefined });\n' ;
41+ source = source . replace ( / r e q u i r e \. c o n t e x t \( .* / , mockedRequireContext + targettedImports ) ;
3942 return source ;
4043}
4144exports . default = loader ;
0 commit comments