Skip to content

Commit

Permalink
improve test case
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jan 20, 2022
1 parent 0670cf5 commit 70da0dd
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions test/configCases/trusted-types/devtool-eval/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
it("should pass TrustedScript to eval", function() {
class TrustedScript {
constructor(script) {
this._script = script;
}
};

it("should pass TrustedScript to eval", function () {
var policy = __webpack_require__.tt();
policy.createScript = jest.fn((script) => {
policy.createScript = jest.fn(script => {
expect(typeof script).toEqual("string");
return new TrustedScript(script);
});

const globalEval = eval;
window.module = {};
window.eval = jest.fn((x) => {
expect(x).toBeInstanceOf(TrustedScript);
return globalEval(x._script);
});

require("./test.js");
expect(window.module.exports).toBeInstanceOf(Object);
expect(window.module.exports.foo).toEqual('bar');
expect(window.module.exports.foo).toEqual("bar");

const testPattern = "var test = {\\s*foo: \'bar\'\\s*};\\s*module.exports = test;";
const testPattern =
"var test = {\\s*foo: 'bar'\\s*};\\s*module.exports = test;";
expect(policy.createScript).toBeCalledWith(
expect.stringMatching(testPattern)
);
Expand All @@ -32,3 +20,26 @@ it("should pass TrustedScript to eval", function() {
})
);
});

class TrustedScript {
constructor(script) {
this._script = script;
}
}

let globalEval;
beforeEach(done => {
globalEval = eval;
window.module = {};
window.eval = jest.fn(x => {
expect(x).toBeInstanceOf(TrustedScript);
return globalEval(x._script);
});
done();
});

afterEach(done => {
delete window.module;
window.eval = globalEval;
done();
});

0 comments on commit 70da0dd

Please sign in to comment.