Porting tests over to vitest

This commit is contained in:
Dolan Miu
2023-06-05 00:33:43 +01:00
parent a89919397d
commit e69b063687
256 changed files with 1557 additions and 612 deletions

View File

@ -1,4 +1,4 @@
import { assert, expect } from "chai";
import { assert, beforeEach, describe, expect, it } from "vitest";
import * as sinon from "sinon";
import { Formatter } from "@export/formatter";

View File

@ -1,4 +1,4 @@
import { expect } from "chai";
import { describe, expect, it } from "vitest";
import { Media } from "@file/media";

View File

@ -1,180 +1,189 @@
// /* tslint:disable:typedef space-before-function-paren */
// import { expect } from "chai";
// import * as sinon from "sinon";
/* tslint:disable:typedef space-before-function-paren */
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import * as sinon from "sinon";
// import { File } from "@file/file";
// import { Footer, Header } from "@file/header";
// import { ImageRun, Paragraph } from "@file/paragraph";
// import * as convenienceFunctions from "@util/convenience-functions";
import { File } from "@file/file";
import { Footer, Header } from "@file/header";
import { ImageRun, Paragraph } from "@file/paragraph";
import * as convenienceFunctions from "@util/convenience-functions";
// import { Compiler } from "./next-compiler";
import { Compiler } from "./next-compiler";
// describe("Compiler", () => {
// let compiler: Compiler;
describe("Compiler", () => {
let compiler: Compiler;
// beforeEach(() => {
// compiler = new Compiler();
// });
beforeEach(() => {
compiler = new Compiler();
});
// before(() => {
// sinon.stub(convenienceFunctions, "uniqueId").callsFake(() => "test");
// });
beforeAll(() => {
vi.spyOn(convenienceFunctions, "uniqueId").mockReturnValue("test");
});
// after(() => {
// (convenienceFunctions.uniqueId as sinon.SinonStub).restore();
// });
afterAll(() => {
vi.resetAllMocks();
});
// describe("#compile()", () => {
// it("should pack all the content", async function () {
// this.timeout(99999999);
// const file = new File({
// sections: [],
// comments: {
// children: [],
// },
// });
// const zipFile = await compiler.compile(file);
// const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
describe("#compile()", () => {
it(
"should pack all the content",
async () => {
const file = new File({
sections: [],
comments: {
children: [],
},
});
const zipFile = await compiler.compile(file);
const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
// expect(fileNames).is.an.instanceof(Array);
// expect(fileNames).has.length(17);
// expect(fileNames).to.include("word/document.xml");
// expect(fileNames).to.include("word/styles.xml");
// expect(fileNames).to.include("docProps/core.xml");
// expect(fileNames).to.include("docProps/custom.xml");
// expect(fileNames).to.include("docProps/app.xml");
// expect(fileNames).to.include("word/numbering.xml");
// expect(fileNames).to.include("word/footnotes.xml");
// expect(fileNames).to.include("word/_rels/footnotes.xml.rels");
// expect(fileNames).to.include("word/settings.xml");
// expect(fileNames).to.include("word/comments.xml");
// expect(fileNames).to.include("word/_rels/document.xml.rels");
// expect(fileNames).to.include("[Content_Types].xml");
// expect(fileNames).to.include("_rels/.rels");
// });
expect(fileNames).is.an.instanceof(Array);
expect(fileNames).has.length(17);
expect(fileNames).to.include("word/document.xml");
expect(fileNames).to.include("word/styles.xml");
expect(fileNames).to.include("docProps/core.xml");
expect(fileNames).to.include("docProps/custom.xml");
expect(fileNames).to.include("docProps/app.xml");
expect(fileNames).to.include("word/numbering.xml");
expect(fileNames).to.include("word/footnotes.xml");
expect(fileNames).to.include("word/_rels/footnotes.xml.rels");
expect(fileNames).to.include("word/settings.xml");
expect(fileNames).to.include("word/comments.xml");
expect(fileNames).to.include("word/_rels/document.xml.rels");
expect(fileNames).to.include("[Content_Types].xml");
expect(fileNames).to.include("_rels/.rels");
},
{
timeout: 99999999,
},
);
// it("should pack all additional headers and footers", function () {
// const file = new File({
// sections: [
// {
// headers: {
// default: new Header({
// children: [new Paragraph("test")],
// }),
// },
// footers: {
// default: new Footer({
// children: [new Paragraph("test")],
// }),
// },
// children: [],
// },
// {
// headers: {
// default: new Header({
// children: [new Paragraph("test")],
// }),
// },
// footers: {
// default: new Footer({
// children: [new Paragraph("test")],
// }),
// },
// children: [],
// },
// ],
// });
it(
"should pack all additional headers and footers",
() => {
const file = new File({
sections: [
{
headers: {
default: new Header({
children: [new Paragraph("test")],
}),
},
footers: {
default: new Footer({
children: [new Paragraph("test")],
}),
},
children: [],
},
{
headers: {
default: new Header({
children: [new Paragraph("test")],
}),
},
footers: {
default: new Footer({
children: [new Paragraph("test")],
}),
},
children: [],
},
],
});
// this.timeout(99999999);
const zipFile = compiler.compile(file);
const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
// const zipFile = compiler.compile(file);
// const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
expect(fileNames).is.an.instanceof(Array);
expect(fileNames).has.length(25);
// expect(fileNames).is.an.instanceof(Array);
// expect(fileNames).has.length(25);
expect(fileNames).to.include("word/header1.xml");
expect(fileNames).to.include("word/_rels/header1.xml.rels");
expect(fileNames).to.include("word/header2.xml");
expect(fileNames).to.include("word/_rels/header2.xml.rels");
expect(fileNames).to.include("word/footer1.xml");
expect(fileNames).to.include("word/_rels/footer1.xml.rels");
expect(fileNames).to.include("word/footer2.xml");
expect(fileNames).to.include("word/_rels/footer2.xml.rels");
},
{
timeout: 99999999,
},
);
// expect(fileNames).to.include("word/header1.xml");
// expect(fileNames).to.include("word/_rels/header1.xml.rels");
// expect(fileNames).to.include("word/header2.xml");
// expect(fileNames).to.include("word/_rels/header2.xml.rels");
// expect(fileNames).to.include("word/footer1.xml");
// expect(fileNames).to.include("word/_rels/footer1.xml.rels");
// expect(fileNames).to.include("word/footer2.xml");
// expect(fileNames).to.include("word/_rels/footer2.xml.rels");
// });
it("should call the format method X times equalling X files to be formatted", () => {
// This test is required because before, there was a case where Document was formatted twice, which was inefficient
// This also caused issues such as running prepForXml multiple times as format() was ran multiple times.
const paragraph = new Paragraph("");
const file = new File({
sections: [
{
properties: {},
children: [paragraph],
},
],
});
// it("should call the format method X times equalling X files to be formatted", () => {
// // This test is required because before, there was a case where Document was formatted twice, which was inefficient
// // This also caused issues such as running prepForXml multiple times as format() was ran multiple times.
// const paragraph = new Paragraph("");
// const file = new File({
// sections: [
// {
// properties: {},
// children: [paragraph],
// },
// ],
// });
// tslint:disable-next-line: no-string-literal
const spy = sinon.spy(compiler["formatter"], "format");
// // tslint:disable-next-line: no-string-literal
// const spy = sinon.spy(compiler["formatter"], "format");
compiler.compile(file);
expect(spy.callCount).to.equal(13);
});
// compiler.compile(file);
// expect(spy.callCount).to.equal(13);
// });
it("should work with media datas", () => {
// This test is required because before, there was a case where Document was formatted twice, which was inefficient
// This also caused issues such as running prepForXml multiple times as format() was ran multiple times.
const file = new File({
sections: [
{
headers: {
default: new Header({
children: [new Paragraph("test")],
}),
},
footers: {
default: new Footer({
children: [new Paragraph("test")],
}),
},
children: [
new Paragraph({
children: [
new ImageRun({
data: Buffer.from("", "base64"),
transformation: {
width: 100,
height: 100,
},
}),
],
}),
],
},
],
});
// it("should work with media datas", () => {
// // This test is required because before, there was a case where Document was formatted twice, which was inefficient
// // This also caused issues such as running prepForXml multiple times as format() was ran multiple times.
// const file = new File({
// sections: [
// {
// headers: {
// default: new Header({
// children: [new Paragraph("test")],
// }),
// },
// footers: {
// default: new Footer({
// children: [new Paragraph("test")],
// }),
// },
// children: [
// new Paragraph({
// children: [
// new ImageRun({
// data: Buffer.from("", "base64"),
// transformation: {
// width: 100,
// height: 100,
// },
// }),
// ],
// }),
// ],
// },
// ],
// });
// tslint:disable-next-line: no-string-literal
sinon.stub(compiler["imageReplacer"], "getMediaData").returns([
{
stream: Buffer.from(""),
fileName: "test",
transformation: {
pixels: {
x: 100,
y: 100,
},
emus: {
x: 100,
y: 100,
},
},
},
]);
// // tslint:disable-next-line: no-string-literal
// sinon.stub(compiler["imageReplacer"], "getMediaData").returns([
// {
// stream: Buffer.from(""),
// fileName: "test",
// transformation: {
// pixels: {
// x: 100,
// y: 100,
// },
// emus: {
// x: 100,
// y: 100,
// },
// },
// },
// ]);
// compiler.compile(file);
// });
// });
// });
compiler.compile(file);
});
});
});

View File

@ -1,6 +1,4 @@
/* tslint:disable:typedef space-before-function-paren */
import { assert } from "chai";
import { mock, stub } from "sinon";
import { afterEach, assert, beforeEach, describe, expect, it, vi } from "vitest";
import { File } from "@file/file";
import { HeadingLevel, Paragraph } from "@file/paragraph";
@ -46,62 +44,71 @@ describe("Packer", () => {
});
describe("#toBuffer()", () => {
it("should create a standard docx file", async function () {
this.timeout(99999999);
const buffer = await Packer.toBuffer(file);
it(
"should create a standard docx file",
async () => {
const buffer = await Packer.toBuffer(file);
assert.isDefined(buffer);
assert.isTrue(buffer.byteLength > 0);
});
assert.isDefined(buffer);
assert.isTrue(buffer.byteLength > 0);
},
{
timeout: 99999999,
},
);
it("should handle exception if it throws any", () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const compiler = stub((Packer as any).compiler, "compile");
vi.spyOn((Packer as any).compiler, "compile").mockImplementation(() => {
throw new Error();
});
compiler.throwsException();
return Packer.toBuffer(file).catch((error) => {
assert.isDefined(error);
});
});
after(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(Packer as any).compiler.compile.restore();
afterEach(() => {
vi.restoreAllMocks();
});
});
describe("#toBase64String()", () => {
it("should create a standard docx file", async function () {
this.timeout(99999999);
const str = await Packer.toBase64String(file);
assert.isDefined(str);
assert.isTrue(str.length > 0);
});
it(
"should create a standard docx file",
async () => {
const str = await Packer.toBase64String(file);
expect(str).toBeDefined();
expect(str.length).toBeGreaterThan(0);
},
{
timeout: 99999999,
},
);
it("should handle exception if it throws any", () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const compiler = stub((Packer as any).compiler, "compile");
vi.spyOn((Packer as any).compiler, "compile").mockImplementation(() => {
throw new Error();
});
compiler.throwsException();
return Packer.toBase64String(file).catch((error) => {
assert.isDefined(error);
});
});
after(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(Packer as any).compiler.compile.restore();
afterEach(() => {
vi.resetAllMocks();
});
});
describe("#toBlob()", () => {
it("should create a standard docx file", async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
stub((Packer as any).compiler, "compile").callsFake(() => ({
vi.spyOn((Packer as any).compiler, "compile").mockReturnValue({
// tslint:disable-next-line: no-empty
generateAsync: () => mock({}),
}));
generateAsync: () => vi.fn(),
});
const str = await Packer.toBlob(file);
assert.isDefined(str);
@ -109,35 +116,27 @@ describe("Packer", () => {
it("should handle exception if it throws any", () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const compiler = stub((Packer as any).compiler, "compile");
vi.spyOn((Packer as any).compiler, "compile").mockImplementation(() => {
throw new Error();
});
compiler.throwsException();
return Packer.toBlob(file).catch((error) => {
assert.isDefined(error);
});
});
afterEach(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(Packer as any).compiler.compile.restore();
vi.resetAllMocks();
});
});
describe("#toStream()", () => {
it("should create a standard docx file", async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
stub((Packer as any).compiler, "compile").callsFake(() => ({
// tslint:disable-next-line: no-empty
generateNodeStream: () => ({
on: (event: string, cb: () => void) => {
if (event === "end") {
cb();
}
},
}),
}));
const stream = await Packer.toStream(file);
return new Promise((resolve, reject) => {
vi.spyOn((Packer as any).compiler, "compile").mockReturnValue(Promise.resolve(new Uint8Array(255)));
const stream = Packer.toStream(file);
const p = new Promise<void>((resolve, reject) => {
stream.on("error", () => {
reject(new Error());
});
@ -146,20 +145,15 @@ describe("Packer", () => {
resolve();
});
});
await p;
});
it("should handle exception if it throws any", () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const compiler = stub((Packer as any).compiler, "compile").callsFake(() => ({
// tslint:disable-next-line: no-empty
on: (event: string, cb: () => void) => {
if (event === "error") {
cb();
}
},
}));
vi.spyOn((Packer as any).compiler, "compile").mockImplementation(() => {
throw new Error();
});
compiler.throwsException();
try {
Packer.toStream(file);
} catch (error) {
@ -168,8 +162,7 @@ describe("Packer", () => {
});
afterEach(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(Packer as any).compiler.compile.restore();
vi.resetAllMocks();
});
});
});

View File

@ -51,15 +51,12 @@ export class Packer {
public static toStream(file: File, prettify?: boolean | PrettifyType): Stream {
const zip = this.compiler.compile(file, prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify);
const stream = new Stream();
// eslint-disable-next-line functional/immutable-data
stream.pipe = (dest) => {
zip.then((z) => {
dest.write(z);
});
return dest;
};
zip.then((z) => {
stream.emit("data", z);
stream.emit("end");
});
return stream;
}