Closed mflorence99 closed 3 years ago
@mflorence99 unfortunately, I have little time left and have been busy with other projects lately, if you are interested, send PR
Any update for this bug?
My apologies -- I had to move on before I could figure which code was responsible for generating the failing code. I satisfied myself with the following patch code that I run as a post-install step. It powers my projects https://github.com/mflorence99/lintel and https://github.com/mflorence99/ternimal.
import * as fs from 'fs';
interface Patch {
from: RegExp;
to: string;
}
interface Patches {
[fileName: string]: Patch[];
}
// @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping
const toRegExp = (str: string, flags = 'g'): RegExp => {
return new RegExp(str.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'), flags);
};
/* eslint-disable @typescript-eslint/naming-convention */
const patches: Patches = {
'node_modules/@ngxs-labs/data/bundles/ngxs-labs-data-decorators.umd.js': [
// @see https://github.com/ngxs-labs/data/issues/622
{
from: toRegExp('var _this = _super.apply(this, __spread(args)) || this;'),
to: 'var _this = args.length ? new _super(...args) : this;'
}
]
};
Object.keys(patches).forEach((fileName) => {
let code = fs.readFileSync(fileName, 'utf8');
patches[fileName].forEach((patch) => {
code = code.replace(patch.from, patch.to);
});
fs.writeFileSync(fileName, code);
});
Problem
State classes decorated with
@Persistence()
throw aTypeError
when instantiated in Jest tests. For example,TestBed.inject(SelectionState)
will trigger:This behavior is new in v4 and did not occur in prior versions.
Analysis
The stacktrace leads to code in the
@Persistence()
decorator:This code is new in v4. It is propagated unchanged into the FESM bundle that Angular itself uses
node_modules/@ngxs-labs/data/fesm2015/ngxs-labs-data-decorators.js
(viavendor.js
) and no issues are observed when running the Angular app.However, the code is translated in the UMD bundle
node_modules/@ngxs-labs/data/bundles/ngxs-labs-data-decorators.umd.js
used by Jest. I've added a comment to show exactly what triggers theTypeError
.Resolution
I found it easiest to hack a post-install patch to change the offending line to
var _this = this
. That's just a hack though and I don't have a good suggestion for a fix, except that the v3 code worked and maybe there's a play to change thereturn
to the prior pattern.