5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
- import { logging } from '@angular-devkit/core' ;
8
+ import { logging , tags } from '@angular-devkit/core' ;
9
9
import { getOptions } from 'loader-utils' ;
10
10
import { extname , join } from 'path' ;
11
11
import { loader } from 'webpack' ;
12
12
13
13
export interface SingleTestTransformLoaderOptions {
14
- files : string [ ] ; // list of paths relative to main
15
- logger : logging . Logger ;
14
+ /* list of paths relative to the entry-point */
15
+ files ?: string [ ] ;
16
+ logger ?: logging . Logger ;
16
17
}
17
18
18
19
export const SingleTestTransformLoader = require . resolve ( join ( __dirname , 'single-test-transform' ) ) ;
@@ -30,29 +31,24 @@ export const SingleTestTransformLoader = require.resolve(join(__dirname, 'single
30
31
* Then it adds import statements for each file in the files options
31
32
* array to import them directly, and thus run the tests there.
32
33
*/
33
- export default function loader ( this : loader . LoaderContext , source : string ) {
34
- const options = getOptions ( this ) as SingleTestTransformLoaderOptions ;
35
- const lineSeparator = process . platform === 'win32' ? '\r\n' : '\n' ;
34
+ export default function loader ( this : loader . LoaderContext , source : string ) : string {
35
+ const { files = [ ] , logger = console } = getOptions ( this ) as SingleTestTransformLoaderOptions ;
36
+ // signal the user that expected content is not present.
37
+ if ( ! source . includes ( 'require.context(' ) ) {
38
+ logger . error ( tags . stripIndent
39
+ `The 'include' option requires that the 'main' file for tests includes the below line:
40
+ const context = require.context('./', true, /\.spec\.ts$/);
41
+ Arguments passed to require.context are not strict and can be changed.` ) ;
42
+
43
+ return source ;
44
+ }
36
45
37
- const targettedImports = options . files
46
+ const targettedImports = files
38
47
. map ( path => `require('./${ path . replace ( '.' + extname ( path ) , '' ) } ');` )
39
- . join ( lineSeparator ) ;
40
-
41
- // TODO: maybe a documented 'marker/comment' inside test.ts would be nicer?
42
- const regex = / r e q u i r e \. c o n t e x t \( .* / ;
43
-
44
- // signal the user that expected content is not present
45
- if ( ! regex . test ( source ) ) {
46
- const message = [
47
- `The 'include' option requires that the 'main' file for tests include the line below:` ,
48
- `const context = require.context('./', true, /\.spec\.ts$/);` ,
49
- `Arguments passed to require.context are not strict and can be changed` ,
50
- ] ;
51
- options . logger . error ( message . join ( lineSeparator ) ) ;
52
- }
48
+ . join ( '\n' ) ;
53
49
54
- const mockedRequireContext = 'Object.assign(() => { }, { keys: () => [], resolve: () => undefined });' + lineSeparator ;
55
- source = source . replace ( regex , mockedRequireContext + targettedImports ) ;
50
+ const mockedRequireContext = 'Object.assign(() => { }, { keys: () => [], resolve: () => undefined });\n' ;
51
+ source = source . replace ( / r e q u i r e \. c o n t e x t \( . * / , mockedRequireContext + targettedImports ) ;
56
52
57
53
return source ;
58
54
}
0 commit comments