Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { logging } from '@angular-devkit/core';
import { logging, tags } from '@angular-devkit/core';
import { getOptions } from 'loader-utils';
import { extname, join } from 'path';
import { loader } from 'webpack';

export interface SingleTestTransformLoaderOptions {
files: string[]; // list of paths relative to main
logger: logging.Logger;
/* list of paths relative to the entry-point */
files?: string[];
logger?: logging.Logger;
}

export const SingleTestTransformLoader = require.resolve(join(__dirname, 'single-test-transform'));
Expand All @@ -30,29 +31,24 @@ export const SingleTestTransformLoader = require.resolve(join(__dirname, 'single
* Then it adds import statements for each file in the files options
* array to import them directly, and thus run the tests there.
*/
export default function loader(this: loader.LoaderContext, source: string) {
const options = getOptions(this) as SingleTestTransformLoaderOptions;
const lineSeparator = process.platform === 'win32' ? '\r\n' : '\n';
export default function loader(this: loader.LoaderContext, source: string): string {
const { files = [], logger = console } = getOptions(this) as SingleTestTransformLoaderOptions;
// signal the user that expected content is not present.
if (!source.includes('require.context(')) {
logger.error(tags.stripIndent
`The 'include' option requires that the 'main' file for tests includes the below line:
const context = require.context('./', true, /\.spec\.ts$/);
Arguments passed to require.context are not strict and can be changed.`);

return source;
}

const targettedImports = options.files
const targettedImports = files
.map(path => `require('./${path.replace('.' + extname(path), '')}');`)
.join(lineSeparator);

// TODO: maybe a documented 'marker/comment' inside test.ts would be nicer?
const regex = /require\.context\(.*/;

// signal the user that expected content is not present
if (!regex.test(source)) {
const message = [
`The 'include' option requires that the 'main' file for tests include the line below:`,
`const context = require.context('./', true, /\.spec\.ts$/);`,
`Arguments passed to require.context are not strict and can be changed`,
];
options.logger.error(message.join(lineSeparator));
}
.join('\n');

const mockedRequireContext = 'Object.assign(() => { }, { keys: () => [], resolve: () => undefined });' + lineSeparator;
source = source.replace(regex, mockedRequireContext + targettedImports);
const mockedRequireContext = 'Object.assign(() => { }, { keys: () => [], resolve: () => undefined });\n';
source = source.replace(/require\.context\(.*/, mockedRequireContext + targettedImports);

return source;
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('Karma Builder', () => {
expect(lastErrorLogEntry && lastErrorLogEntry.message)
// tslint:disable-next-line:max-line-length
.toContain(
"The 'include' option requires that the 'main' file for tests include the line below:",
"The 'include' option requires that the 'main' file for tests includes the below line:",
);

await run.stop();
Expand Down