jest-preset-angular 14.2.3 → 14.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/build/compiler/ng-jest-compiler.js +71 -0
- package/build/config/global-setup.js +21 -0
- package/build/config/ng-jest-config.js +29 -0
- package/build/constants.js +10 -0
- package/build/index.js +6 -0
- package/build/ng-jest-transformer.js +66 -0
- package/build/presets/index.js +30 -0
- package/build/resolvers/ng-jest-resolver.js +7 -0
- package/build/serializers/html-comment.js +8 -0
- package/build/serializers/index.js +6 -0
- package/build/serializers/ng-snapshot.js +48 -0
- package/build/serializers/no-ng-attributes.js +57 -0
- package/build/transformers/esm_interop_inject.cjs +4 -0
- package/build/transformers/jit_transform.js +173 -0
- package/build/transformers/replace-resources.js +206 -0
- package/build/utils/ngcc-jest-processor.js +75 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
## [14.2.4](https://github.com/thymikee/jest-preset-angular/compare/v14.2.3...v14.2.4) (2024-09-12)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* build: ensure build step runs before publishing ([3a602db](https://github.com/thymikee/jest-preset-angular/commit/3a602db))
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
1
10
|
## [14.2.3](https://github.com/thymikee/jest-preset-angular/compare/v14.2.2...v14.2.3) (2024-09-12)
|
|
2
11
|
|
|
3
12
|
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.NgJestCompiler = void 0;
|
|
7
|
+
const node_os_1 = __importDefault(require("node:os"));
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const ts_jest_1 = require("ts-jest");
|
|
10
|
+
const jit_transform_1 = require("../transformers/jit_transform");
|
|
11
|
+
const replace_resources_1 = require("../transformers/replace-resources");
|
|
12
|
+
class NgJestCompiler extends ts_jest_1.TsCompiler {
|
|
13
|
+
constructor(configSet, jestCacheFS) {
|
|
14
|
+
super(configSet, jestCacheFS);
|
|
15
|
+
this.configSet = configSet;
|
|
16
|
+
this.jestCacheFS = jestCacheFS;
|
|
17
|
+
this._libSourceFileCache = new Map();
|
|
18
|
+
this._logger.debug('created NgJestCompiler');
|
|
19
|
+
this._defaultLibDirPath = node_path_1.default.dirname(this._ts.getDefaultLibFilePath(this._compilerOptions));
|
|
20
|
+
}
|
|
21
|
+
_transpileOutput(fileContent, filePath) {
|
|
22
|
+
var _a;
|
|
23
|
+
const scriptTarget = (_a = this._compilerOptions.target) !== null && _a !== void 0 ? _a : this._ts.ScriptTarget.Latest;
|
|
24
|
+
const sourceFile = this._ts.createSourceFile(filePath, fileContent, scriptTarget);
|
|
25
|
+
const compilerHost = {
|
|
26
|
+
getSourceFile: (fileName) => {
|
|
27
|
+
var _a;
|
|
28
|
+
if (fileName === node_path_1.default.normalize(filePath)) {
|
|
29
|
+
return sourceFile;
|
|
30
|
+
}
|
|
31
|
+
let libSourceFile = this._libSourceFileCache.get(fileName);
|
|
32
|
+
if (!libSourceFile) {
|
|
33
|
+
const libFilePath = node_path_1.default.join(this._defaultLibDirPath, fileName);
|
|
34
|
+
const libFileContent = (_a = this._ts.sys.readFile(libFilePath)) !== null && _a !== void 0 ? _a : '';
|
|
35
|
+
if (libFileContent) {
|
|
36
|
+
libSourceFile = this._ts.createSourceFile(fileName, libFileContent, scriptTarget);
|
|
37
|
+
this._libSourceFileCache.set(fileName, libSourceFile);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return libSourceFile;
|
|
41
|
+
},
|
|
42
|
+
writeFile: () => { },
|
|
43
|
+
getDefaultLibFileName: () => 'lib.d.ts',
|
|
44
|
+
useCaseSensitiveFileNames: () => false,
|
|
45
|
+
getCanonicalFileName: (fileName) => fileName,
|
|
46
|
+
getCurrentDirectory: () => '',
|
|
47
|
+
getNewLine: () => node_os_1.default.EOL,
|
|
48
|
+
fileExists: (fileName) => fileName === filePath,
|
|
49
|
+
readFile: () => '',
|
|
50
|
+
directoryExists: () => true,
|
|
51
|
+
getDirectories: () => [],
|
|
52
|
+
};
|
|
53
|
+
this.program = this._ts.createProgram([filePath], this._compilerOptions, compilerHost);
|
|
54
|
+
return this._ts.transpileModule(fileContent, {
|
|
55
|
+
fileName: filePath,
|
|
56
|
+
transformers: this._makeTransformers(this.configSet.resolvedTransformers),
|
|
57
|
+
compilerOptions: this._compilerOptions,
|
|
58
|
+
reportDiagnostics: this.configSet.shouldReportDiagnostics(filePath),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
_makeTransformers(customTransformers) {
|
|
62
|
+
var _a;
|
|
63
|
+
const allTransformers = super._makeTransformers(customTransformers);
|
|
64
|
+
return Object.assign(Object.assign(Object.assign({}, allTransformers.after), allTransformers.afterDeclarations), { before: [
|
|
65
|
+
...((_a = allTransformers.before) !== null && _a !== void 0 ? _a : []),
|
|
66
|
+
(0, replace_resources_1.replaceResources)(this.program),
|
|
67
|
+
(0, jit_transform_1.angularJitApplicationTransform)(this.program),
|
|
68
|
+
] });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.NgJestCompiler = NgJestCompiler;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.globalSetup = void 0;
|
|
13
|
+
const ngcc_jest_processor_1 = require("../utils/ngcc-jest-processor");
|
|
14
|
+
const globalSetup = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
|
+
const ngJestConfig = globalThis.ngJest;
|
|
16
|
+
const tsconfig = ngJestConfig === null || ngJestConfig === void 0 ? void 0 : ngJestConfig.tsconfig;
|
|
17
|
+
if (!(ngJestConfig === null || ngJestConfig === void 0 ? void 0 : ngJestConfig.skipNgcc)) {
|
|
18
|
+
(0, ngcc_jest_processor_1.runNgccJestProcessor)(tsconfig);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
exports.globalSetup = globalSetup;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NgJestConfig = void 0;
|
|
4
|
+
const jest_util_1 = require("jest-util");
|
|
5
|
+
const ts_jest_1 = require("ts-jest");
|
|
6
|
+
const defaultProcessWithEsbuildPatterns = ['**/*.mjs'];
|
|
7
|
+
class NgJestConfig extends ts_jest_1.ConfigSet {
|
|
8
|
+
constructor(jestConfig, parentLogger) {
|
|
9
|
+
var _a, _b, _c;
|
|
10
|
+
super(jestConfig, parentLogger);
|
|
11
|
+
const jestGlobalsConfig = (_b = (_a = jestConfig === null || jestConfig === void 0 ? void 0 : jestConfig.globals) === null || _a === void 0 ? void 0 : _a.ngJest) !== null && _b !== void 0 ? _b : Object.create(null);
|
|
12
|
+
this.processWithEsbuild = (0, jest_util_1.globsToMatcher)([
|
|
13
|
+
...((_c = jestGlobalsConfig.processWithEsbuild) !== null && _c !== void 0 ? _c : []),
|
|
14
|
+
...defaultProcessWithEsbuildPatterns,
|
|
15
|
+
]);
|
|
16
|
+
}
|
|
17
|
+
_resolveTsConfig(compilerOptions, resolvedConfigFile) {
|
|
18
|
+
const result = super._resolveTsConfig(compilerOptions, resolvedConfigFile);
|
|
19
|
+
result.options.enableIvy = true;
|
|
20
|
+
result.options.noEmitOnError = false;
|
|
21
|
+
result.options.suppressOutputPathCheck = true;
|
|
22
|
+
result.options.allowEmptyCodegenFiles = false;
|
|
23
|
+
result.options.annotationsAs = 'decorators';
|
|
24
|
+
result.options.enableResourceInlining = false;
|
|
25
|
+
result.options.allowJs = true;
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.NgJestConfig = NgJestConfig;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.COMPONENT = exports.REQUIRE = exports.TEMPLATE = exports.STYLES = exports.STYLE_URL = exports.STYLE_URLS = exports.TEMPLATE_URL = void 0;
|
|
4
|
+
exports.TEMPLATE_URL = 'templateUrl';
|
|
5
|
+
exports.STYLE_URLS = 'styleUrls';
|
|
6
|
+
exports.STYLE_URL = 'styleUrl';
|
|
7
|
+
exports.STYLES = 'styles';
|
|
8
|
+
exports.TEMPLATE = 'template';
|
|
9
|
+
exports.REQUIRE = 'require';
|
|
10
|
+
exports.COMPONENT = 'Component';
|
package/build/index.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _NgJestTransformer_ngJestLogger;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.NgJestTransformer = void 0;
|
|
16
|
+
const bs_logger_1 = require("bs-logger");
|
|
17
|
+
const esbuild_1 = require("esbuild");
|
|
18
|
+
const ts_jest_1 = require("ts-jest");
|
|
19
|
+
const ng_jest_compiler_1 = require("./compiler/ng-jest-compiler");
|
|
20
|
+
const ng_jest_config_1 = require("./config/ng-jest-config");
|
|
21
|
+
class NgJestTransformer extends ts_jest_1.TsJestTransformer {
|
|
22
|
+
constructor(tsJestConfig) {
|
|
23
|
+
var _a;
|
|
24
|
+
super(tsJestConfig);
|
|
25
|
+
_NgJestTransformer_ngJestLogger.set(this, void 0);
|
|
26
|
+
__classPrivateFieldSet(this, _NgJestTransformer_ngJestLogger, (0, bs_logger_1.createLogger)({
|
|
27
|
+
context: {
|
|
28
|
+
[bs_logger_1.LogContexts.package]: 'jest-preset-angular',
|
|
29
|
+
[bs_logger_1.LogContexts.logLevel]: bs_logger_1.LogLevels.trace,
|
|
30
|
+
version: require('../package.json').version,
|
|
31
|
+
},
|
|
32
|
+
targets: (_a = process.env.NG_JEST_LOG) !== null && _a !== void 0 ? _a : undefined,
|
|
33
|
+
}), "f");
|
|
34
|
+
}
|
|
35
|
+
_createConfigSet(config) {
|
|
36
|
+
return new ng_jest_config_1.NgJestConfig(config);
|
|
37
|
+
}
|
|
38
|
+
_createCompiler(configSet, cacheFS) {
|
|
39
|
+
this._compiler = new ng_jest_compiler_1.NgJestCompiler(configSet, cacheFS);
|
|
40
|
+
}
|
|
41
|
+
process(fileContent, filePath, transformOptions) {
|
|
42
|
+
const configSet = super._configsFor(transformOptions);
|
|
43
|
+
if (configSet.processWithEsbuild(filePath)) {
|
|
44
|
+
__classPrivateFieldGet(this, _NgJestTransformer_ngJestLogger, "f").debug({ filePath }, 'process with esbuild');
|
|
45
|
+
const compilerOpts = configSet.parsedTsConfig.options;
|
|
46
|
+
const { code, map } = (0, esbuild_1.transformSync)(fileContent, {
|
|
47
|
+
loader: 'js',
|
|
48
|
+
format: transformOptions.supportsStaticESM && configSet.useESM ? 'esm' : 'cjs',
|
|
49
|
+
target: compilerOpts.target === configSet.compilerModule.ScriptTarget.ES2015 ? 'es2015' : 'es2016',
|
|
50
|
+
sourcemap: compilerOpts.sourceMap,
|
|
51
|
+
sourcefile: filePath,
|
|
52
|
+
sourcesContent: true,
|
|
53
|
+
sourceRoot: compilerOpts.sourceRoot,
|
|
54
|
+
});
|
|
55
|
+
return {
|
|
56
|
+
code,
|
|
57
|
+
map,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
return super.process(fileContent, filePath, transformOptions);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.NgJestTransformer = NgJestTransformer;
|
|
66
|
+
_NgJestTransformer_ngJestLogger = new WeakMap();
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.defaultTransformerOptions = exports.defaultEsmPreset = exports.defaultPreset = void 0;
|
|
7
|
+
const serializers_1 = __importDefault(require("../serializers"));
|
|
8
|
+
const baseConfig = {
|
|
9
|
+
testEnvironment: 'jsdom',
|
|
10
|
+
moduleFileExtensions: ['ts', 'html', 'js', 'json', 'mjs'],
|
|
11
|
+
snapshotSerializers: serializers_1.default,
|
|
12
|
+
};
|
|
13
|
+
const defaultTransformerOptions = {
|
|
14
|
+
tsconfig: '<rootDir>/tsconfig.spec.json',
|
|
15
|
+
stringifyContentPathRegex: '\\.(html|svg)$',
|
|
16
|
+
};
|
|
17
|
+
exports.defaultTransformerOptions = defaultTransformerOptions;
|
|
18
|
+
const defaultPreset = Object.assign(Object.assign({}, baseConfig), { transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'], transform: {
|
|
19
|
+
'^.+\\.(ts|js|mjs|html|svg)$': ['jest-preset-angular', defaultTransformerOptions],
|
|
20
|
+
} });
|
|
21
|
+
exports.defaultPreset = defaultPreset;
|
|
22
|
+
const defaultEsmPreset = Object.assign(Object.assign({}, baseConfig), { extensionsToTreatAsEsm: ['.ts'], moduleNameMapper: {
|
|
23
|
+
tslib: 'tslib/tslib.es6.js',
|
|
24
|
+
}, transform: {
|
|
25
|
+
'^.+\\.(ts|js|html|svg)$': [
|
|
26
|
+
'jest-preset-angular',
|
|
27
|
+
Object.assign(Object.assign({}, defaultTransformerOptions), { useESM: true }),
|
|
28
|
+
],
|
|
29
|
+
}, transformIgnorePatterns: ['node_modules/(?!tslib)'] });
|
|
30
|
+
exports.defaultEsmPreset = defaultEsmPreset;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const ngJestResolver = (path, options) => {
|
|
3
|
+
return options.defaultResolver(path, Object.assign(Object.assign({}, options), { packageFilter(pkg) {
|
|
4
|
+
return Object.assign(Object.assign({}, pkg), { main: pkg.main || pkg.es2015 || pkg.module });
|
|
5
|
+
} }));
|
|
6
|
+
};
|
|
7
|
+
module.exports = ngJestResolver;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const HTML_ELEMENT_REGEXP = /Comment/;
|
|
3
|
+
const test = (value) => (value === null || value === void 0 ? void 0 : value.nodeType) === 8 && value.constructor !== undefined && HTML_ELEMENT_REGEXP.test(value.constructor.name);
|
|
4
|
+
const serialize = () => '';
|
|
5
|
+
module.exports = {
|
|
6
|
+
serialize,
|
|
7
|
+
test,
|
|
8
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const attributesToRemovePatterns = ['__ngContext__'];
|
|
3
|
+
const removeTrailingWhiteSpaces = (serializedComponent) => {
|
|
4
|
+
return serializedComponent.replace(/\n^\s*\n/gm, '\n');
|
|
5
|
+
};
|
|
6
|
+
const print = (fixture, printer, indent, opts, colors) => {
|
|
7
|
+
const { componentRef, componentInstance } = fixture;
|
|
8
|
+
const componentDef = componentRef.componentType.ɵcmp;
|
|
9
|
+
const componentName = componentDef.selectors[0][0];
|
|
10
|
+
const nodes = Array.from(componentRef.location.nativeElement.childNodes).map(printer).join('');
|
|
11
|
+
let serializedComponent = '';
|
|
12
|
+
if (opts.omitAllCompAttrs) {
|
|
13
|
+
serializedComponent = '<' + componentName + '>\n' + indent(nodes) + '\n</' + componentName + '>';
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
const attributes = Object.keys(componentInstance).filter((key) => !attributesToRemovePatterns.includes(key));
|
|
17
|
+
const componentAttrs = attributes
|
|
18
|
+
.sort()
|
|
19
|
+
.map((attribute) => {
|
|
20
|
+
const compAttrVal = componentInstance[attribute];
|
|
21
|
+
return (opts.spacing +
|
|
22
|
+
indent(`${colors.prop.open}${attribute}${colors.prop.close}=`) +
|
|
23
|
+
colors.value.open +
|
|
24
|
+
(compAttrVal && compAttrVal.constructor
|
|
25
|
+
? `{[Function ${compAttrVal.constructor.name}]}`
|
|
26
|
+
: `"${compAttrVal}"`) +
|
|
27
|
+
colors.value.close);
|
|
28
|
+
})
|
|
29
|
+
.join('');
|
|
30
|
+
serializedComponent =
|
|
31
|
+
'<' +
|
|
32
|
+
componentName +
|
|
33
|
+
componentAttrs +
|
|
34
|
+
(componentAttrs.length ? '\n' : '') +
|
|
35
|
+
'>\n' +
|
|
36
|
+
indent(nodes) +
|
|
37
|
+
'\n</' +
|
|
38
|
+
componentName +
|
|
39
|
+
'>';
|
|
40
|
+
}
|
|
41
|
+
serializedComponent = removeTrailingWhiteSpaces(serializedComponent);
|
|
42
|
+
return serializedComponent;
|
|
43
|
+
};
|
|
44
|
+
const test = (val) => !!val && typeof val === 'object' && 'componentRef' in val;
|
|
45
|
+
module.exports = {
|
|
46
|
+
print,
|
|
47
|
+
test,
|
|
48
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const pretty_format_1 = require("pretty-format");
|
|
3
|
+
const jestDOMElementSerializer = pretty_format_1.plugins.DOMElement;
|
|
4
|
+
const attributesToRemovePatterns = ['ng-reflect', '_nghost', '_ngcontent', 'ng-version'];
|
|
5
|
+
const attributesToClean = {
|
|
6
|
+
class: [/^(?:mat|cdk|ng).*-\w*\d+-\d+$/, /^ng-star-inserted$/],
|
|
7
|
+
id: [/^(?:mat|cdk|ng).*-\d+$/],
|
|
8
|
+
for: [/^(?:mat|cdk|ng).*-\d+$/],
|
|
9
|
+
'aria-owns': [/^(?:mat|cdk|ng).*-\d+$/],
|
|
10
|
+
'aria-labelledby': [/^(?:mat|cdk|ng).*-\d+$/],
|
|
11
|
+
'aria-controls': [/^(?:mat|cdk|ng).*-\d+$/],
|
|
12
|
+
};
|
|
13
|
+
const hasAttributesToRemove = (attribute) => attributesToRemovePatterns.some((removePattern) => attribute.name.startsWith(removePattern));
|
|
14
|
+
const hasAttributesToClean = (attribute) => Object.keys(attributesToClean).some((removePatternKey) => attribute.name === removePatternKey);
|
|
15
|
+
const removeAngularAttributes = (node) => {
|
|
16
|
+
const nodeCopy = node.cloneNode(true);
|
|
17
|
+
Object.values(nodeCopy.attributes)
|
|
18
|
+
.filter(hasAttributesToRemove)
|
|
19
|
+
.forEach((attribute) => nodeCopy.attributes.removeNamedItem(attribute.name));
|
|
20
|
+
return nodeCopy;
|
|
21
|
+
};
|
|
22
|
+
const cleanAngularClasses = (node) => {
|
|
23
|
+
const nodeCopy = node.cloneNode(true);
|
|
24
|
+
Object.values(nodeCopy.attributes)
|
|
25
|
+
.filter(hasAttributesToClean)
|
|
26
|
+
.forEach((attribute) => {
|
|
27
|
+
attribute.value = attribute.value
|
|
28
|
+
.split(' ')
|
|
29
|
+
.filter((attrValue) => !attributesToClean[attribute.name].some((attributeCleanRegex) => attributeCleanRegex.test(attrValue)))
|
|
30
|
+
.join(' ');
|
|
31
|
+
if (attribute.value === '') {
|
|
32
|
+
nodeCopy.attributes.removeNamedItem(attribute.name);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
nodeCopy.attributes.setNamedItem(attribute);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return nodeCopy;
|
|
39
|
+
};
|
|
40
|
+
const shouldSerializeElement = (val) => {
|
|
41
|
+
return Object.values(val.attributes).some((attribute) => hasAttributesToRemove(attribute) || hasAttributesToClean(attribute));
|
|
42
|
+
};
|
|
43
|
+
const serialize = (node, ...rest) => {
|
|
44
|
+
let nodeCopy = removeAngularAttributes(node);
|
|
45
|
+
nodeCopy = cleanAngularClasses(nodeCopy);
|
|
46
|
+
return jestDOMElementSerializer.serialize(nodeCopy, ...rest);
|
|
47
|
+
};
|
|
48
|
+
const test = (val) => {
|
|
49
|
+
if (!val || !(val instanceof Element)) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
return jestDOMElementSerializer.test(val) && shouldSerializeElement(val);
|
|
53
|
+
};
|
|
54
|
+
module.exports = {
|
|
55
|
+
serialize,
|
|
56
|
+
test,
|
|
57
|
+
};
|