Skip to content

Commit a01e32c

Browse files
alan-agius4clydin
authored andcommitted
refactor(@angular-devkit/build-angular): clean up SingleTestTransformLoader
Minor cleanup of SingleTestTransformLoader and fix interface issue which causes #17823 to be red.
1 parent 22b9cc0 commit a01e32c

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

packages/angular_devkit/build_angular/src/angular-cli-files/plugins/single-test-transform.ts

+19-23
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { logging } from '@angular-devkit/core';
8+
import { logging, tags } from '@angular-devkit/core';
99
import { getOptions } from 'loader-utils';
1010
import { extname, join } from 'path';
1111
import { loader } from 'webpack';
1212

1313
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;
1617
}
1718

1819
export const SingleTestTransformLoader = require.resolve(join(__dirname, 'single-test-transform'));
@@ -30,29 +31,24 @@ export const SingleTestTransformLoader = require.resolve(join(__dirname, 'single
3031
* Then it adds import statements for each file in the files options
3132
* array to import them directly, and thus run the tests there.
3233
*/
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+
}
3645

37-
const targettedImports = options.files
46+
const targettedImports = files
3847
.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 = /require\.context\(.*/;
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');
5349

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(/require\.context\(.*/, mockedRequireContext + targettedImports);
5652

5753
return source;
5854
}

packages/angular_devkit/build_angular/src/karma/selected_spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('Karma Builder', () => {
6262
expect(lastErrorLogEntry && lastErrorLogEntry.message)
6363
// tslint:disable-next-line:max-line-length
6464
.toContain(
65-
"The 'include' option requires that the 'main' file for tests include the line below:",
65+
"The 'include' option requires that the 'main' file for tests includes the below line:",
6666
);
6767

6868
await run.stop();

0 commit comments

Comments
 (0)