Skip to content

Commit 73ea896

Browse files
authored
Core: run all only tests (#1436)
1 parent 95226b7 commit 73ea896

File tree

6 files changed

+89
-34
lines changed

6 files changed

+89
-34
lines changed

docs/QUnit/module.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ The module's callback is invoked with the test environment as its `this` context
5959

6060
## Exclusive tests
6161

62-
When you are debugging your code, you will often need to _only_ run a subset of tests. You can append `only` to `QUnit.module` to specify which module you want to run.
63-
64-
Note that if more than one module was defined using `QUnit.module.only()`, only the first one will run.
62+
When you are debugging your code, you will often need to _only_ run a subset of tests. You can append `only` to `QUnit.module` to specify which module(s) you want to run.
6563

6664
It works the same way `QUnit.only()` does for tests.
6765

docs/QUnit/only.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
layout: default
33
title: only
4-
description: Adds a test to exclusively run, preventing all other tests from running.
4+
description: Adds a test to exclusively run, preventing any other tests not defined with `QUnit.only()` from running.
55
categories:
66
- main
77
---
88

99
## `QUnit.only( name, callback )`
1010

11-
Adds a test to exclusively run, preventing all other tests from running.
11+
Adds a test to exclusively run, preventing any other tests not defined with `QUnit.only()` from running.
1212

1313
| parameter | description |
1414
|-----------|-------------|
@@ -23,9 +23,7 @@ Adds a test to exclusively run, preventing all other tests from running.
2323

2424
### Description
2525

26-
Use this method to focus your test suite on a specific test. `QUnit.only` will cause any other tests in your suite to be ignored.
27-
28-
Note that if more than one `QUnit.only` is present only the first instance will run.
26+
Use this method to focus your test suite on specific tests. `QUnit.only` will cause any other tests in your suite to be ignored.
2927

3028
This is an alternative to filtering tests to run in the HTML reporter. It is especially useful when you use a console reporter or in a codebase with a large set of long running tests.
3129

@@ -48,10 +46,14 @@ QUnit.test( "stomp", function( assert ) {
4846
assert.ok( false, "I'm not quite ready yet" );
4947
});
5048

51-
// You're currently working on the laser feature, so we run only this test
49+
// You're currently working on the laser feature, so we run only these tests
5250
QUnit.only( "laser", function( assert ) {
5351
assert.ok( this.robot.laser() );
5452
});
53+
54+
QUnit.only( "other laser", function( assert ) {
55+
assert.ok( this.robot.otherLaser() );
56+
});
5557
```
5658

5759
Using modern syntax:
@@ -73,8 +75,12 @@ test( "stomp", t => {
7375
t.ok( false, "I'm not quite ready yet" );
7476
});
7577

76-
// You're currently working on the laser feature, so we run only this test
78+
// You're currently working on the laser feature, so we run only these tests
7779
only( "laser", function( t ) {
7880
t.ok( this.robot.laser() );
7981
});
82+
83+
only( "other laser", function( t ) {
84+
t.ok( this.robot.otherLaser() );
85+
});
8086
```

src/module.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ let focused = false;
99

1010
const moduleStack = [];
1111

12+
function isParentModuleInQueue() {
13+
const modulesInQueue = config.modules.map( module => module.moduleId );
14+
return moduleStack.some( module => modulesInQueue.includes( module.moduleId ) );
15+
}
16+
1217
function createModule( name, testEnvironment, modifiers ) {
1318
const parentModule = moduleStack.length ? moduleStack.slice( -1 )[ 0 ] : null;
1419
const moduleName = parentModule !== null ? [ parentModule.name, name ].join( " > " ) : name;
@@ -96,22 +101,20 @@ function processModule( name, options, executeNow, modifiers = {} ) {
96101
}
97102

98103
export default function module( name, options, executeNow ) {
99-
if ( focused ) {
104+
if ( focused && !isParentModuleInQueue() ) {
100105
return;
101106
}
102107

103108
processModule( name, options, executeNow );
104109
}
105110

106111
module.only = function() {
107-
if ( focused ) {
108-
return;
112+
if ( !focused ) {
113+
config.modules.length = 0;
114+
config.queue.length = 0;
109115
}
110116

111-
config.modules.length = 0;
112-
config.queue.length = 0;
113-
114-
module( ...arguments );
117+
processModule( ...arguments );
115118

116119
focused = true;
117120
};

src/test.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -719,13 +719,11 @@ export function skip( testName ) {
719719

720720
// Will be exposed as QUnit.only
721721
export function only( testName, callback ) {
722-
if ( focused ) {
723-
return;
722+
if ( !focused ) {
723+
config.queue.length = 0;
724+
focused = true;
724725
}
725726

726-
config.queue.length = 0;
727-
focused = true;
728-
729727
const newTest = new Test( {
730728
testName: testName,
731729
callback: callback

test/module-only.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ QUnit.done( function() {
3535
"75e1bf3f": {
3636
skipped: false,
3737
todo: false
38+
},
39+
"c7ae85c2": {
40+
skipped: false,
41+
todo: false
42+
},
43+
"74b800d1": {
44+
skipped: false,
45+
todo: false
46+
},
47+
"2f8fb2a2": {
48+
skipped: false,
49+
todo: false
3850
}
3951
} );
4052
} );
@@ -79,3 +91,33 @@ QUnit.module( "module C", function() {
7991
assert.ok( false, "this test should not run" );
8092
} );
8193
} );
94+
95+
QUnit.module.only( "module D", function() {
96+
QUnit.test( "test D", function( assert ) {
97+
assert.ok( true, "this test should run as well" );
98+
} );
99+
} );
100+
101+
QUnit.module.only( "module E", function() {
102+
QUnit.module( "module F", function() {
103+
QUnit.test( "test F", function( assert ) {
104+
assert.ok( true, "this test should run as well" );
105+
} );
106+
} );
107+
108+
QUnit.test( "test E", function( assert ) {
109+
assert.ok( true, "this test should run as well" );
110+
} );
111+
} );
112+
113+
QUnit.module.skip( "module G", function() {
114+
QUnit.module.only( "module H", function() {
115+
QUnit.test( "test H", function( assert ) {
116+
assert.ok( false, "this test should not run" );
117+
} );
118+
} );
119+
120+
QUnit.test( "test G", function( assert ) {
121+
assert.ok( false, "this test should not run" );
122+
} );
123+
} );

test/only.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1-
QUnit.module( "QUnit.only" );
1+
QUnit.module( "QUnit.only", function( hooks ) {
2+
let testsRun = 0;
23

3-
QUnit.test( "implicitly skipped test", function( assert ) {
4-
assert.ok( false, "test should be skipped" );
5-
} );
4+
hooks.after( function( assert ) {
5+
assert.strictEqual( testsRun, 2 );
6+
} );
67

7-
QUnit.only( "only run this test", function( assert ) {
8-
assert.ok( true, "only this test should run" );
9-
} );
8+
QUnit.test( "implicitly skipped test", function( assert ) {
9+
assert.ok( false, "test should be skipped" );
10+
} );
1011

11-
QUnit.test( "another implicitly skipped test", function( assert ) {
12-
assert.ok( false, "test should be skipped" );
13-
} );
12+
QUnit.only( "run this test", function( assert ) {
13+
testsRun += 1;
14+
assert.ok( true, "only this test should run" );
15+
} );
16+
17+
QUnit.test( "another implicitly skipped test", function( assert ) {
18+
assert.ok( false, "test should be skipped" );
19+
} );
1420

15-
QUnit.only( "ignore the subsequent calls to only", function( assert ) {
16-
assert.ok( false, "this test should be skipped" );
21+
QUnit.only( "all tests with only run", function( assert ) {
22+
testsRun += 1;
23+
assert.ok( true, "this test should run as well" );
24+
} );
1725
} );

0 commit comments

Comments
 (0)