@samitouri / QOS-React-2 / commits / 0a6d3b31de

Add compilation failure test for Logger

Sathya Gunasekaran committed Nov 28, 2023 at 15:35 UTC 0a6d3b31deb8201c5c965d0624923fb79f76fad1
1 file changed +36
compiler/packages/babel-plugin-react-forget/src/__tests__/Logger-test.ts
+36
@@ -34,3 +34,39 @@ it("logs succesful compilation", () => {
34 start: { column: 0, line: 1 },
35 });
36 });
37 +
38 +it("logs failed compilation", () => {
39 + const logs: [string | null, LoggerEvent][] = [];
40 + const logger: Logger = {
41 + logEvent(filename, event) {
42 + logs.push([filename, event]);
43 + },
44 + };
45 +
46 + expect(() => {
47 + runReactForgetBabelPlugin(
48 + "function Component(props) { props.foo = 1; return <div>{props}</div> }",
49 + "test.js",
50 + "flow",
51 + { logger, panicThreshold: "ALL_ERRORS" } as any
52 + );
53 + }).toThrow();
54 +
55 + const [filename, event] = logs.at(0)!;
56 + expect(filename).toContain("test.js");
57 + expect(event.kind).toEqual("CompileError");
58 + invariant(event.kind === "CompileError", "typescript be smarter");
59 +
60 + expect(event.detail.severity).toEqual("InvalidReact");
61 + expect(event.detail.loc).toEqual({
62 + end: { column: 33, line: 1 },
63 + identifierName: "props",
64 + start: { column: 28, line: 1 },
65 + });
66 +
67 + // Make sure event.fnLoc is different from event.detail.loc
68 + expect(event.fnLoc).toEqual({
69 + end: { column: 70, line: 1 },
70 + start: { column: 0, line: 1 },
71 + });
72 +});