1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.SingleTestTransformLoader = void 0;
|
---|
11 | const core_1 = require("@angular-devkit/core");
|
---|
12 | const path_1 = require("path");
|
---|
13 | exports.SingleTestTransformLoader = __filename;
|
---|
14 | /**
|
---|
15 | * This loader transforms the default test file to only run tests
|
---|
16 | * for some specs instead of all specs.
|
---|
17 | * It works by replacing the known content of the auto-generated test file:
|
---|
18 | * const context = require.context('./', true, /\.spec\.ts$/);
|
---|
19 | * context.keys().map(context);
|
---|
20 | * with:
|
---|
21 | * const context = { keys: () => ({ map: (_a) => { } }) };
|
---|
22 | * context.keys().map(context);
|
---|
23 | * So that it does nothing.
|
---|
24 | * Then it adds import statements for each file in the files options
|
---|
25 | * array to import them directly, and thus run the tests there.
|
---|
26 | */
|
---|
27 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
28 | function loader(source) {
|
---|
29 | const { files = [], logger = console } = this.getOptions();
|
---|
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;
|
---|
36 | }
|
---|
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(/require\.context\(.*/, mockedRequireContext + targettedImports);
|
---|
42 | return source;
|
---|
43 | }
|
---|
44 | exports.default = loader;
|
---|