From 47086c6e7e7da22aa09c0f0f07daba6647150030 Mon Sep 17 00:00:00 2001 From: Sunlight <929746887@qq.com> Date: Tue, 25 Feb 2020 12:45:09 +0800 Subject: [PATCH] test: add syntax error check in test cases --- test/RuntimeTemplateCheck.test.js | 109 ------------------ .../missing-module-syntax-error/errors.js | 3 + .../missing-module-syntax-error/index.js | 11 ++ .../missing-module-syntax-error/module.js | 3 + 4 files changed, 17 insertions(+), 109 deletions(-) delete mode 100644 test/RuntimeTemplateCheck.test.js create mode 100644 test/cases/runtime/missing-module-syntax-error/errors.js create mode 100644 test/cases/runtime/missing-module-syntax-error/index.js create mode 100644 test/cases/runtime/missing-module-syntax-error/module.js diff --git a/test/RuntimeTemplateCheck.test.js b/test/RuntimeTemplateCheck.test.js deleted file mode 100644 index 4ce972e9f..000000000 --- a/test/RuntimeTemplateCheck.test.js +++ /dev/null @@ -1,109 +0,0 @@ -"use strict"; - -const path = require("path"); -const webpack = require(".."); -const fs = require("graceful-fs"); -const rimraf = require("rimraf"); - -const tempFolderPath = path.join(__dirname, "TemplateRuntimeTemp"); -const tempSourceFilePath = path.join(tempFolderPath, "temp-file.js"); -const tempBundleFilePath = path.join(tempFolderPath, "bundle.js"); - -const createSingleCompiler = () => { - return webpack({ - entry: tempSourceFilePath, - context: tempFolderPath, - mode: "development", - output: { - path: tempFolderPath, - filename: "bundle.js" - }, - devtool: "inline-cheap-source-map" - }); -}; - -function cleanup(callback) { - rimraf(tempFolderPath, callback); -} - -function createFiles() { - fs.mkdirSync(tempFolderPath, { recursive: true }); - - fs.writeFileSync( - tempSourceFilePath, - `import { SomeClass } from "./somemodule"; - -const result = new SomeClass(); - -const a = function test(arg) { - console.log(arg); -} - -if (true) a -SomeClass -`, - "utf-8" - ); -} - -const checkOutputFileStatus = outputFilePath => { - const result = { - status: "Ok" - }; - try { - if (!fs.existsSync(outputFilePath)) { - result.status = "CompilerError"; - return result; - } - - const contentStr = fs.readFileSync(outputFilePath, "utf-8"); - - // check syntax - Function(contentStr); - - return result; - } catch (e) { - const error = e.toString(); - - result.status = "error"; - - if (error.indexOf("SyntaxError") === 0) { - result.status = "SyntaxError"; - return result; - } - - result.type = error; - return result; - } -}; - -describe("TemplateRuntimeCheck", () => { - jest.setTimeout(10000); - - beforeEach(done => { - cleanup(err => { - if (err) return done(err); - createFiles(); - setTimeout(done, 1000); - }); - }); - - afterEach(cleanup); - - it("should build syntax correct code", done => { - const compiler = createSingleCompiler(); - compiler.run((err, stats) => { - if (err) throw err; - - compiler.close(err2 => { - if (err2) throw err2; - - const result = checkOutputFileStatus(tempBundleFilePath); - - expect(result.status).toBe("Ok"); - - done(); - }); - }); - }); -}); diff --git a/test/cases/runtime/missing-module-syntax-error/errors.js b/test/cases/runtime/missing-module-syntax-error/errors.js new file mode 100644 index 000000000..4ce4a4dd9 --- /dev/null +++ b/test/cases/runtime/missing-module-syntax-error/errors.js @@ -0,0 +1,3 @@ +module.exports = [ + [/Module not found/, /Can't resolve '\.\/someModule' /], +]; diff --git a/test/cases/runtime/missing-module-syntax-error/index.js b/test/cases/runtime/missing-module-syntax-error/index.js new file mode 100644 index 000000000..d4b37ada9 --- /dev/null +++ b/test/cases/runtime/missing-module-syntax-error/index.js @@ -0,0 +1,11 @@ + + +it("should have correct error code", function() { + + try { + require("./module"); + } catch(e) { + expect(e.code).toBe("MODULE_NOT_FOUND"); + } + +}); diff --git a/test/cases/runtime/missing-module-syntax-error/module.js b/test/cases/runtime/missing-module-syntax-error/module.js new file mode 100644 index 000000000..be691c790 --- /dev/null +++ b/test/cases/runtime/missing-module-syntax-error/module.js @@ -0,0 +1,3 @@ +import { SomeClass } from "./someModule"; + +new SomeClass();