jeudi 28 février 2019

Autotests with webpack-dev-server

I'm quite confused with topic.

I develop a some kind of lazy module assembler using webpack-dev-server. It finally works but sometimes we need more assurance. That's why I need some tests. The task is to make them a kind of autotests.

For now the code of server start looks like this (I omit excessive params).

import webpack from "webpack";
import webpackConfig from "../webpack.config.js";
import webpackCompileConfig from "../webpack-compiler.config.mjs";
import WebpackDevServer from "webpack-dev-server";

webpack(webpackConfig(mode, dirname, masterPath)).run(function(err) {
  if (err) throw err;

  const compileOpt = {
    // pack of compiler options
  };
  const compiler = webpack(webpackCompileConfig(compileOpt));

  const server = new WebpackDevServer(compiler, {
    // pack of server options
  });

  server.listen(port, "0.0.0.0", err => {
    if (err) throw err;
    console.log(`Starting root server on 0.0.0.0:${port}`);
  });
});

It starts and works properly: gets some file requests, bundles necessary modules with webpack and sends them to requester.
Simple test I want to start with are to check are there files after assembling or not.

Supposed logic is:

  • Run some command inside this project, e.g. npm run test
  • It starts server and sends a pack of requests with different logic I want to test (parallel, simultaneous requests etc.)
  • It tests file existence and send me results in console or smth. of that sort

The problem is my very little expirience in any kind of testing so I'll appreciate your help.

===
The way I use it now (spoiler: manually)

The only thing the Internet helps me about.

  • Server starts as usual
  • There is another fully off-site test module (code below)
  • Run mocha
  • See listed test results

test-server.js

var chai = require("chai");
var chaiHttp = require("chai-http");

const should = chai.should();
chai.use(chaiHttp);

describe("Test file existence", function() {
  it("units", done => {
    chai
      .request("http://localhost:9000")
      .get("/units/awesome-project/index.js")
      .end((err, res) => {
        res.should.have.status(200);
        done();
      });
  });
  // another 'it()'s to test other files
});

Yes, it works. But I want more automatisation. Just

  • Run server
  • Send requests
  • Get test results

I'm ready for dialog.

Aucun commentaire:

Enregistrer un commentaire