Skip to content

Latest commit

 

History

History
371 lines (284 loc) · 7.48 KB

File metadata and controls

371 lines (284 loc) · 7.48 KB

convert-tests

Usage

npx ember-mocha-to-qunit-codemod convert-tests path/of/files/ or/some**/*glob.js

# or

yarn global add ember-mocha-to-qunit-codemod
ember-mocha-to-qunit-codemod convert-tests path/of/files/ or/some**/*glob.js

Local Usage

node ./bin/cli.js convert-tests path/of/files/ or/some**/*glob.js

Post execution steps

This codemod only updates js files. You will also need to update your package.json to uninstall both mocha and ember-mocha, as well as any other related packages (e.g. eslinput-plugin-mocha). Install qunit, ember-qunit, qunit-dom, and eslint-plugin-qunit. Update your tests/test-helper.js and tests/index.html as per the output of ember new.

See ember-cli/ember-new-output

Input / Output


ember-mocha-methods

Input (ember-mocha-methods.input.js):

import { setupTest, setupApplicationTest, setupRenderingTest } from 'ember-mocha';
import { describe } from 'mocha';

describe('desc', function() {
  setupTest();
  setupApplicationTest();
  setupRenderingTest();
});

Output (ember-mocha-methods.output.js):

import { setupTest, setupApplicationTest, setupRenderingTest } from 'ember-qunit';
import { module } from 'qunit';

module('desc', hooks => {
  setupTest(hooks);
  setupApplicationTest(hooks);
  setupRenderingTest(hooks);
});

hooks-async

Input (hooks-async.input.js):

import { describe, before, after, beforeEach, afterEach } from 'mocha';

describe('describe', function() {
  let x = 'describe';

  beforeEach(async function() {
    x = 'beforeEach';
  });

  beforeEach(function(done) {
    x = 'beforeEach';
    done();
  });

  afterEach(async function() {
    x = 'afterEach';
  });

  afterEach(function(done) {
    x = 'afterEach';
    done();
  });

  before(async function() {
    x = 'before';
  });

  before(function(done) {
    x = 'before';
    done();
  });

  after(async function() {
    x = 'after';
  });

  after(function(done) {
    x = 'after';
    done();
  });
});

Output (hooks-async.output.js):

import { module } from 'qunit';

module('describe', hooks => {
  let x = 'describe';

  hooks.beforeEach(async function() {
    x = 'beforeEach';
  });

  hooks.beforeEach(function(assert) {
    const done = assert.async();
    x = 'beforeEach';
    done();
  });

  hooks.afterEach(async function() {
    x = 'afterEach';
  });

  hooks.afterEach(function(assert) {
    const done = assert.async();
    x = 'afterEach';
    done();
  });

  hooks.before(async function() {
    x = 'before';
  });

  hooks.before(function(assert) {
    const done = assert.async();
    x = 'before';
    done();
  });

  hooks.after(async function() {
    x = 'after';
  });

  hooks.after(function(assert) {
    const done = assert.async();
    x = 'after';
    done();
  });
});

hooks

Input (hooks.input.js):

import { context, describe, before, after, beforeEach, afterEach } from 'mocha';

describe('describe', function() {
  let x = 'describe';

  context('context', function() {
    x = 'context';

    beforeEach(function() {
      x = 'beforeEach';
    });

    afterEach(function() {
      x = 'afterEach';
    });

    before(function() {
      x = 'before';
    });

    after(function() {
      x = 'after';
    });
  });
});

Output (hooks.output.js):

import { module } from 'qunit';

module('describe', hooks => {
  let x = 'describe';

  module('context', hooks => {
    x = 'context';

    hooks.beforeEach(function() {
      x = 'beforeEach';
    });

    hooks.afterEach(function() {
      x = 'afterEach';
    });

    hooks.before(function() {
      x = 'before';
    });

    hooks.after(function() {
      x = 'after';
    });
  });
});

method-wrapper

Input (method-wrapper.input.js):

import { it, describe, beforeEach, afterEach } from 'mocha';

function wrap(func) {
  return function() {
    beforeEach(function() {
      let x = 'beforeEach';
    });

    func();

    afterEach(function() {
      let x = 'afterEach';
    });
  };
}

describe('desc', wrap(function() {
  it('test', function() {
    let x = 'test';
  });
}));

Output (method-wrapper.output.js):

import { module, test } from 'qunit';

function wrap(func) {
  return function() {
    hooks.beforeEach(function() {
      let x = 'beforeEach';
    });

    func();

    hooks.afterEach(function() {
      let x = 'afterEach';
    });
  };
}

module('desc', wrap(function() {
  test('test', function(assert) {
    //ember-mocha-to-qunit-codemod: migrated from mocha, consider using qunit assertions instead
    assert.expect(0);

    let x = 'test';
  });
}));

test-async

Input (test-async.input.js):

import { it, describe } from 'mocha';

describe('describe', function() {
  it('test 1', async function() {
    if (1 === 3) {
      throw new Error()
    }
  });

  it('test 2', function(done) {
    if (1 === 3) {
      throw new Error()
    }

    done();
  });
});

Output (test-async.output.js):

import { module, test } from 'qunit';

module('describe', hooks => {
  test('test 1', async function(assert) {
    //ember-mocha-to-qunit-codemod: migrated from mocha, consider using qunit assertions instead
    assert.expect(0);

    if (1 === 3) {
      throw new Error()
    }
  });

  test('test 2', function(assert) {
    //ember-mocha-to-qunit-codemod: migrated from mocha, consider using qunit assertions instead
    assert.expect(0);

    const done = assert.async();
    if (1 === 3) {
      throw new Error()
    }

    done();
  });
});

test-basic

Input (test-basic.input.js):

import { it, describe } from 'mocha';

describe('describe', function() {
  it('test', function() {
    if (1 === 3) {
      throw new Error()
    }
  });
});

Output (test-basic.output.js):

import { module, test } from 'qunit';

module('describe', hooks => {
  test('test', function(assert) {
    //ember-mocha-to-qunit-codemod: migrated from mocha, consider using qunit assertions instead
    assert.expect(0);

    if (1 === 3) {
      throw new Error()
    }
  });
});