testingember.jsqunit

Is there a way for getting a list of all created tests in my emberjs app?


I need a way to get all my ember tests' nameS, since I want to run them in a separate thread by using the --filter "TEST-NAME" flag.

Right now, I am using a hardcoded array.


Solution

  • It's not documented, but you can use the config object on the root QUnit object to get all modules in your test suite, then the tests within each module and their names. Note that any tests not in a module will still appear, but with an empty-named module. Here's a runnable jsfiddle with the example, then the code below.

    QUnit.test('no-module', (assert) => { assert.equal(0,0) })
    
    QUnit.module('foobar', () => {
      QUnit.test('foo', (assert) => { assert.equal(1, 1) })
      QUnit.test('bar', (assert) => { assert.equal(2, 2) })
    })
    
    QUnit.module('batbaz', () => {
      QUnit.test('bat', (assert) => { assert.equal(3, 3) })
      QUnit.test('baz', (assert) => { assert.equal(4, 4) })
    })
    
    const testNames = []
    QUnit.config.modules.forEach((module) => {
      testNames.push(...module.tests.map(test => test.name))
    })
    
    console.log(testNames)