Вопрос для Мокко, используя библиотеку утверждений Чай - PullRequest
0 голосов
/ 27 мая 2019

Я импортирую main () из одного скрипта в мой тестовый файл и выполняю main () как 1 тестовый пример, хотя main состоит из нескольких функций.Мне было интересно, как я могу структурировать свой тест, чтобы каждая функция в main считалась отдельным тестовым случаем, а не 1 как целое

    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');

      });
    });

1 Ответ

0 голосов
/ 29 мая 2019

Возможно, вы можете использовать before() и поместить каждое ожидание в функцию it().

например

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', () => {
    // ...
  });
});

Надеюсь, это поможет

...