Skip to content
Merged
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
176 changes: 88 additions & 88 deletions sri-plugin/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,32 +83,32 @@ describe("sri-plugin/unit", () => {

// CHANGED: throw error when not standard hash function because it can not be supported by rust
// test("warns when no standard hash function name is specified", async () => {
// test("throw error when not standard hash function name is specified", async () => {
// const plugin = new SubresourceIntegrityPlugin({
// hashFuncNames: ["md5" as any],
// });
test("throw error when not standard hash function name is specified", async () => {
const plugin = new SubresourceIntegrityPlugin({
hashFuncNames: ["md5" as any],
});

// const compilation = await runCompilation(
// rspack({
// ...defaultOptions,
// plugins: [plugin],
// })
// );
const compilation = await runCompilation(
rspack({
...defaultOptions,
plugins: [plugin],
})
);

// // expect(compilation.errors).toEqual([]);
// // expect(compilation.warnings[0]?.message).toMatch(
// // new RegExp(
// // "It is recommended that at least one hash function is part of " +
// // "the set for which support is mandated by the specification"
// // )
// // );
// // expect(compilation.warnings[1]).toBeUndefined();
// expect(compilation.warnings.length).toEqual(0);
// expect(compilation.errors[0]?.message).toMatch(
// /Expected value to be one of \"sha256\", \"sha384\" or \"sha512\"/
// );
// expect(compilation.warnings[1]).toBeUndefined();
// });
// expect(compilation.errors).toEqual([]);
// expect(compilation.warnings[0]?.message).toMatch(
// new RegExp(
// "It is recommended that at least one hash function is part of " +
// "the set for which support is mandated by the specification"
// )
// );
// expect(compilation.warnings[1]).toBeUndefined();
expect(compilation.warnings.length).toEqual(0);
expect(compilation.errors[0]?.message).toMatch(
/Expect SRI hash function to be 'sha256', 'sha384' or 'sha512', but got 'md5'/
);
expect(compilation.warnings[1]).toBeUndefined();
});

test("supports new constructor with array of hash function names", async () => {
const plugin = new SubresourceIntegrityPlugin({
Expand All @@ -126,81 +126,81 @@ describe("sri-plugin/unit", () => {
expect(compilation.warnings.length).toBe(0);
});

// test("errors if hash function names is not an array", async () => {
// const plugin = new SubresourceIntegrityPlugin({
// hashFuncNames: "sha256" as any,
// });
test("errors if hash function names is not an array", async () => {
const plugin = new SubresourceIntegrityPlugin({
hashFuncNames: "sha256" as any,
});

// const compilation = await runCompilation(
// rspack({
// ...defaultOptions,
// plugins: [plugin, disableOutputPlugin],
// })
// );
const compilation = await runCompilation(
rspack({
...defaultOptions,
plugins: [plugin, disableOutputPlugin],
})
);

// expect(compilation.errors.length).toBe(1);
// expect(compilation.warnings.length).toBe(0);
// expect(compilation.errors[0]?.message).toMatch(
// /Expected tuple at \"hashFuncNames\"/
// );
// });
expect(compilation.errors.length).toBe(1);
expect(compilation.warnings.length).toBe(0);
expect(compilation.errors[0]?.message).toMatch(
/InvalidArg, Given napi value is not an array on RawSubresourceIntegrityPluginOptions.hashFuncNames/
);
});

// test("errors if hash function names contains non-string", async () => {
// const plugin = new SubresourceIntegrityPlugin({
// hashFuncNames: [1234] as any,
// });
test("errors if hash function names contains non-string", async () => {
const plugin = new SubresourceIntegrityPlugin({
hashFuncNames: [1234] as any,
});

// const compilation = await runCompilation(
// rspack({
// ...defaultOptions,
// plugins: [plugin, disableOutputPlugin],
// })
// );
const compilation = await runCompilation(
rspack({
...defaultOptions,
plugins: [plugin, disableOutputPlugin],
})
);

// expect(compilation.errors.length).toBe(1);
// expect(compilation.warnings.length).toBe(0);
// expect(compilation.errors[0]?.message).toMatch(
// /Expected value to be one of \"sha256\", \"sha384\" or \"sha512\"/
// );
// });
expect(compilation.errors.length).toBe(1);
expect(compilation.warnings.length).toBe(0);
expect(compilation.errors[0]?.message).toMatch(
/StringExpected, Failed to convert JavaScript value `Number 1234 ` into rust type `String` on RawSubresourceIntegrityPluginOptions.hashFuncNames/
);
});

// test("errors if hash function names are empty", async () => {
// const plugin = new SubresourceIntegrityPlugin({
// hashFuncNames: [] as any,
// });
test("errors if hash function names are empty", async () => {
const plugin = new SubresourceIntegrityPlugin({
hashFuncNames: [] as any,
});

// const compilation = await runCompilation(
// rspack({
// ...defaultOptions,
// plugins: [plugin, disableOutputPlugin],
// })
// );
const compilation = await runCompilation(
rspack({
...defaultOptions,
plugins: [plugin, disableOutputPlugin],
})
);

// expect(compilation.errors.length).toBe(1);
// expect(compilation.warnings.length).toBe(0);
// expect(compilation.errors[0]?.message).toMatch(
// /Expected value to be one of \"sha256\", \"sha384\" or \"sha512\"/
// );
// });
expect(compilation.errors.length).toBe(1);
expect(compilation.warnings.length).toBe(0);
expect(compilation.errors[0]?.message).toMatch(
/Expect at least one SRI hash function name/
);
});

// test("errors if hash function names contains unsupported digest", async () => {
// const plugin = new SubresourceIntegrityPlugin({
// hashFuncNames: ["frobnicate"] as any,
// });
test("errors if hash function names contains unsupported digest", async () => {
const plugin = new SubresourceIntegrityPlugin({
hashFuncNames: ["frobnicate"] as any,
});

// const compilation = await runCompilation(
// rspack({
// ...defaultOptions,
// plugins: [plugin, disableOutputPlugin],
// })
// );
const compilation = await runCompilation(
rspack({
...defaultOptions,
plugins: [plugin, disableOutputPlugin],
})
);

// expect(compilation.errors.length).toBe(1);
// expect(compilation.warnings.length).toBe(0);
// expect(compilation.errors[0]?.message).toMatch(
// /xpected value to be one of \"sha256\", \"sha384\" or \"sha512\"/
// );
// });
expect(compilation.errors.length).toBe(1);
expect(compilation.warnings.length).toBe(0);
expect(compilation.errors[0]?.message).toMatch(
/Expect SRI hash function to be 'sha256', 'sha384' or 'sha512', but got 'frobnicate'/
);
});

// TODO: support hashLoading option
// test("errors if hashLoading option uses unknown value", async () => {
Expand Down
Loading