Skip to content

Commit

Permalink
feat: add diag test (#5113)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi authored and ahabhgk committed Jan 2, 2024
1 parent 48a9392 commit 5b88cbe
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 126 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ packages/rspack/tests/fixtures/**/*
packages/rspack/tests/statsCases/**/*
packages/rspack/tests/configCases/errors/**/*
packages/rspack/tests/hotCases/**/*
packages/rspack/tests/diagnostics/**/*
packages/rspack/tests/js/**/*
packages/rspack/tests/copyPlugin/build/**/*
packages/rspack-test-tools/template/**/*
Expand Down
5 changes: 1 addition & 4 deletions packages/rspack/src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ import { FileSystemInfoEntry } from "./FileSystemInfo";
import { RuntimeGlobals } from "./RuntimeGlobals";
import { tryRunOrWebpackError } from "./lib/HookWebpackError";
import { CodeGenerationResult } from "./Module";
import {
HOOKS_CAN_NOT_INHERENT_FROM_PARENT,
canInherentFromParent
} from "./builtin-plugin/base";
import { canInherentFromParent } from "./builtin-plugin/base";

class Compiler {
#_instance?: binding.Rspack;
Expand Down
68 changes: 68 additions & 0 deletions packages/rspack/tests/diagnostics.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import path from "path";
import fs from "fs";
import util from "util";
import { rspack, RspackOptions } from "../src";
import serializer from "jest-serializer-path";
import assert from "assert";
import { ensureRspackConfigNotExist } from "./utils";

expect.addSnapshotSerializer(serializer);

const caseDir = path.resolve(__dirname, "./diagnostics");
const cases = fs.readdirSync(caseDir);

describe("Diagnostics", function () {
cases.forEach(caseName => {
it(`${caseName} should compiled and match snapshot`, async function () {
const casePath = path.resolve(caseDir, caseName);
ensureRspackConfigNotExist(casePath);
const outputPath = path.resolve(casePath, `./dist`);
const configFile = path.resolve(casePath, "webpack.config.js");

let config = {};
if (fs.existsSync(configFile)) {
config = require(configFile);
}

let options: RspackOptions = {
target: "node",
context: casePath,
entry: {
main: "./"
},
mode: "development",
devServer: {
hot: false
},
infrastructureLogging: {
debug: false
},
output: {
// @ts-ignore
...config.output,
path: outputPath
}
};

if (fs.existsSync(outputPath)) {
fs.rmdirSync(outputPath, { recursive: true });
}

const stats = await util.promisify(rspack)(options);
assert(typeof stats !== "undefined");
assert(stats.hasErrors() || stats.hasWarnings());
let output = stats
.toString({ timings: false, version: false })
.replace(/\\/g, "/");
const errorOutputPath = path.resolve(casePath, `./stats.err`);
const updateSnapshot =
process.argv.includes("-u") ||
process.argv.includes("--updateSnapshot");
if (!fs.existsSync(errorOutputPath) || updateSnapshot) {
fs.writeFileSync(errorOutputPath, output);
} else {
expect(output).toBe(fs.readFileSync(errorOutputPath, "utf-8"));
}
});
});
});
5 changes: 5 additions & 0 deletions packages/rspack/tests/diagnostics/parse-failed/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function add(a: number, b: number): number {
return a + b;
}

add(1, 2);
21 changes: 21 additions & 0 deletions packages/rspack/tests/diagnostics/parse-failed/stats.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
PublicPath: (none)
asset main.js 1.21 KiB [emitted] (name: main)
Entrypoint main 1.21 KiB = main.js
./index.js

ERROR in ./index.js ModuleParseError

× Module parse failed:
╰─▶ × JavaScript parsing error
╭─[1:1]
1 │ function add(a: number, b: number): number {
· ┬
· ╰── Expected ',', got ':'
2 │ return a + b;
3 │ }
╰────

help:
You may need an appropriate loader to handle this file type.

Rspack compiled with 1 error (c25b2e90a72c5e9b4a41)
Loading

0 comments on commit 5b88cbe

Please sign in to comment.