From 5497ffe0d93e18ddda2e1130b52b282c6d22bdd9 Mon Sep 17 00:00:00 2001 From: Joel Chen Date: Wed, 7 Oct 2020 10:19:18 -0700 Subject: [PATCH] add tests for hapi7 webpack dev plugin --- packages/xarc-app-dev/e1/index.js | 1 + packages/xarc-app-dev/e1/package.json | 15 ++++ packages/xarc-app-dev/package.json | 7 +- .../test/spec/webpack-dev-hapi.spec.ts | 87 +++++++++++++++++++ .../test/spec/webpack-dev-hapi17.spec.ts | 85 ++++++++++++++++++ 5 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 packages/xarc-app-dev/e1/index.js create mode 100644 packages/xarc-app-dev/e1/package.json create mode 100644 packages/xarc-app-dev/test/spec/webpack-dev-hapi.spec.ts create mode 100644 packages/xarc-app-dev/test/spec/webpack-dev-hapi17.spec.ts diff --git a/packages/xarc-app-dev/e1/index.js b/packages/xarc-app-dev/e1/index.js new file mode 100644 index 000000000..d3d2c2786 --- /dev/null +++ b/packages/xarc-app-dev/e1/index.js @@ -0,0 +1 @@ +module.exports = require("electrode-server"); diff --git a/packages/xarc-app-dev/e1/package.json b/packages/xarc-app-dev/e1/package.json new file mode 100644 index 000000000..f6480a700 --- /dev/null +++ b/packages/xarc-app-dev/e1/package.json @@ -0,0 +1,15 @@ +{ + "name": "e1", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "electrode-server": "^1", + "hapi": "^16" + } +} diff --git a/packages/xarc-app-dev/package.json b/packages/xarc-app-dev/package.json index 213abad06..4a2ee8093 100644 --- a/packages/xarc-app-dev/package.json +++ b/packages/xarc-app-dev/package.json @@ -111,6 +111,8 @@ "@xarc/run": "^1.0.3", "babel-eslint": "^10.1.0", "chai": "^4.2.0", + "e1": "./e1", + "electrode-server": "^3.2.0", "eslint": "^6.8.0", "eslint-config-walmart": "^2.2.1", "eslint-plugin-filenames": "^1.1.0", @@ -119,7 +121,7 @@ "mock-require": "^3.0.3", "nyc": "^15.0.0", "prettier": "^1.14.2", - "run-verify": "^1.2.1", + "run-verify": "^1.2.5", "shx": "^0.3.2", "sinon": "^7.2.6", "sinon-chai": "^3.3.0", @@ -139,7 +141,8 @@ "subapp-util": "../subapp-util" }, "devDependencies": { - "@xarc/app": "../xarc-app" + "@xarc/app": "../xarc-app", + "e1": "./e1" } }, "nyc": { diff --git a/packages/xarc-app-dev/test/spec/webpack-dev-hapi.spec.ts b/packages/xarc-app-dev/test/spec/webpack-dev-hapi.spec.ts new file mode 100644 index 000000000..7fe25a861 --- /dev/null +++ b/packages/xarc-app-dev/test/spec/webpack-dev-hapi.spec.ts @@ -0,0 +1,87 @@ +/* eslint-disable @typescript-eslint/no-var-requires, @typescript-eslint/no-empty-function */ +/* eslint-disable @typescript-eslint/ban-ts-ignore, no-invalid-this, @typescript-eslint/class-name-casing */ + +const hapiCompat = require("electrode-hapi-compat"); + +const moduleName = "../../src/lib/webpack-dev-hapi"; + +import { asyncVerify, runFinally } from "run-verify"; +import { expect } from "chai"; +import { before, beforeEach, describe, it, after, afterEach } from "mocha"; +const electrodeServer = require("e1"); + +describe("dev-hapi 16", function() { + this.timeout(10000); + + before(() => { + hapiCompat.hapiVersion = 16; + }); + + beforeEach(() => { + delete require.cache[require.resolve(moduleName)]; + }); + + afterEach(() => { + delete require.cache[require.resolve(moduleName)]; + }); + + after(() => { + // + }); + + const captureRequest = server => { + const data: any = {}; + server.route({ + method: "GET", + path: "/test", + handler: (request, reply) => { + data.request = request; + data.called = true; + debugger; + reply("DONE"); + } + }); + return data; + }; + + const testPlugin16 = options => { + let server, data; + return asyncVerify( + () => electrodeServer(options), + s => { + server = s; + data = captureRequest(server); + + return server.inject("/test"); + }, + resp => { + expect(resp.statusCode).to.equal(200); + expect(data.request.app) + .to.have.key("webpackDev") + .that.is.an("object"); + }, + runFinally(() => server.stop()) + ); + }; + + it.only("should allow registering the webpack dev plugin for hapi <=16", () => { + return testPlugin16({ + plugins: { + "webpack-dev": { + module: moduleName, + requireFromPath: __dirname + } + } + }); + }); + + // it("should allow using the hapi17 register function directly", () => { + // return testPlugin17({ + // plugins: { + // "webpack-dev": { + // register: ver17Register + // } + // } + // }); + // }); +}); diff --git a/packages/xarc-app-dev/test/spec/webpack-dev-hapi17.spec.ts b/packages/xarc-app-dev/test/spec/webpack-dev-hapi17.spec.ts new file mode 100644 index 000000000..5c48afbbe --- /dev/null +++ b/packages/xarc-app-dev/test/spec/webpack-dev-hapi17.spec.ts @@ -0,0 +1,85 @@ +/* eslint-disable @typescript-eslint/no-var-requires, @typescript-eslint/no-empty-function */ +/* eslint-disable @typescript-eslint/ban-ts-ignore, no-invalid-this, @typescript-eslint/class-name-casing */ + +const hapiCompat = require("electrode-hapi-compat"); +const ver17Register = require("../../src/lib/webpack-dev-hapi17"); + +const moduleName = "../../src/lib/webpack-dev-hapi"; + +import { asyncVerify, runFinally } from "run-verify"; +import { expect } from "chai"; +import { before, beforeEach, describe, it, after, afterEach } from "mocha"; +const electrodeServer = require("electrode-server"); + +describe("dev-hapi 17", function() { + this.timeout(10000); + + before(() => { + hapiCompat.hapiVersion = 18; + }); + + beforeEach(() => {}); + + afterEach(() => { + delete require.cache[require.resolve(moduleName)]; + }); + + after(() => { + // + }); + + const captureRequest = server => { + const data: any = {}; + server.route({ + method: "GET", + path: "/test", + handler: (request, h) => { + data.request = request; + data.called = true; + return "DONE"; + } + }); + return data; + }; + + const testPlugin17 = options => { + let server, data; + return asyncVerify( + () => electrodeServer(options), + s => { + server = s; + data = captureRequest(server); + + return server.inject("/test"); + }, + resp => { + expect(resp.statusCode).to.equal(200); + expect(data.request.app) + .to.have.key("webpackDev") + .that.is.an("object"); + }, + runFinally(() => server.stop()) + ); + }; + + it("should allow registering the webpack dev plugin for hapi >=18", () => { + return testPlugin17({ + plugins: { + "webpack-dev": { + module: moduleName, + requireFromPath: __dirname + } + } + }); + }); + + it("should allow using the hapi17 register function directly", () => { + return testPlugin17({ + plugins: { + "webpack-dev": { + register: ver17Register + } + } + }); + }); +});