简体   繁体   中英

Question for Mocha, using Chai assertion library

I am importing main() from one script into my test file and I'm executing main() as 1 test case although main consists of multiple functions. I was wondering how can I structure my test that each function on main would count as a separate test case instead of 1 as whole

    async function main(config = {}) {
      const url = validateUrl(config.url);
      const origin = validateOrigin(config.origin);
      const bundleId = validateBundleId(config.bundleId);
      const username = validateUsername(config.username);
      const password = validatePassword(config.password);

      let pipeline;
      let message;
      let worker;
      let passthroughWorkerId;

      pipeline = await createPipeline(origin, bundleId, username, password, url);
      if (pipeline.status != "running") {
        throw new Error("Pipeline not started");
      }

      message = await pipelineWait(username, password, url, pipeline);
      if (debug) {
        console.log(`Wait message: ${message}`);
      } 

      worker = await updatePassthroughWorker(username, password, url, pipeline, 0);
      // Do something with worker 0
      pipeline = await retreivePipeline(username, password, url, pipeline);
      if (pipeline.status != "waiting") {
        throw new Error("Pipeline didn't wait");
      }
      pipeline = await restartPipeline(username, password, url, pipeline);
      if (pipeline.status != "running") {
        throw new Error("Pipeline didn't restart");
      }

      message = await cancelPipeline(username, password, url, pipeline);
      if (debug) {
        console.log("Pipeline Status:");
        console.log(pipeline.status);
        console.log(`Cancel message: ${message}`);
      }

      worker = await updatePassthroughWorker(username, password, url, pipeline, 1);

      pipeline = await retreivePipeline(username, password, url, pipeline);
      if (pipeline.status != "cancelled") {
        throw new Error("Pipeline didn't cancel");
      }

      return pipeline
    }

Test

    describe('Pipeline cancellation', () => {

      it('Should trigger pipeline, put it in wait, resume, and the cancel it ', async () => {
        let data = await pipeline.main(config[process.env.TEST_ENV]);
        expect(data).to.be.an('object');
        expect(data).to.have.property('status');
        expect(data.status).to.equal('cancelled');

      });
    });

Perhaps you can use before() and put each expectation in it() function.

eg

describe('Pipeline cancellation', () => {
  let data;

  before(async () => {
    data = await pipeline.main(config[process.env.TEST_ENV]);
  });

  it('Should trigger pipeline', () => {
    expect(data).to.be.an('object');
    expect(data).to.have.property('status');
    expect(data.status).to.equal('cancelled');
  });

  it('should put it in wait', () => {
    // ...
  });

  it('should resume', () => {
    // ...
  });
});

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM