Skip to content

Commit d037613

Browse files
authored
Make running tests conditional (codesandbox#1080)
* Make running tests conditional * Still show test count in Tests tab * Remove redundant import
1 parent 4f5560c commit d037613

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

packages/app/src/app/components/Preview/DevTools/Tests/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ class Tests extends React.Component<Props, State> {
107107
running: true,
108108
});
109109
}
110+
111+
if (this.props.hidden && !nextProps.hidden) {
112+
this.runAllTests();
113+
}
110114
}
111115

112116
selectFile = (file: File) => {
@@ -117,7 +121,9 @@ class Tests extends React.Component<Props, State> {
117121
};
118122

119123
handleMessage = (data: Object) => {
120-
if (data.type === 'test') {
124+
if (data.type === 'done' && (!this.props.hidden || this.props.standalone)) {
125+
this.runAllTests();
126+
} else if (data.type === 'test') {
121127
switch (data.event) {
122128
case 'initialize_tests': {
123129
this.currentDescribeBlocks = [];
@@ -127,6 +133,14 @@ class Tests extends React.Component<Props, State> {
127133
this.setState(INITIAL_STATE);
128134
break;
129135
}
136+
case 'test_count': {
137+
const { updateStatus } = this.props;
138+
if (updateStatus) {
139+
updateStatus('clear');
140+
updateStatus('info', data.count);
141+
}
142+
break;
143+
}
130144
case 'total_test_start': {
131145
this.currentDescribeBlocks = [];
132146
if (this.props.updateStatus) {

packages/app/src/sandbox/compile.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ export function getHTMLParts(html: string) {
5757
return { head: '', body: html };
5858
}
5959

60+
function sendTestCount(manager: Manager, modules: Array<Module>) {
61+
const testRunner = manager.testRunner;
62+
const tests = testRunner.findTests(modules);
63+
64+
dispatch({
65+
type: 'test',
66+
event: 'test_count',
67+
count: tests.length,
68+
});
69+
}
70+
6071
let firstLoad = true;
6172
let hadError = false;
6273
let lastHeadHTML = null;
@@ -554,23 +565,6 @@ async function compile({
554565
createCodeSandboxOverlay(modules);
555566
}
556567

557-
dispatch({ type: 'status', status: 'running-tests' });
558-
559-
try {
560-
// Testing
561-
const ttt = Date.now();
562-
const testRunner = manager.testRunner;
563-
testRunner.findTests(modules);
564-
await testRunner.runTests();
565-
debug(`Test Evaluation time: ${Date.now() - ttt}ms`);
566-
567-
// End - Testing
568-
} catch (error) {
569-
if (process.env.NODE_ENV === 'development') {
570-
console.error(error);
571-
}
572-
}
573-
574568
debug(`Total time: ${Date.now() - startTime}ms`);
575569

576570
dispatch({
@@ -584,6 +578,16 @@ async function compile({
584578
changedModuleCount,
585579
firstLoad
586580
);
581+
582+
setTimeout(() => {
583+
try {
584+
sendTestCount(manager, modules);
585+
} catch (e) {
586+
if (process.env.NODE_ENV === 'development') {
587+
console.error('Test error', e);
588+
}
589+
}
590+
}, 600);
587591
} catch (e) {
588592
console.log('Error in sandbox:');
589593
console.error(e);

packages/app/src/sandbox/eval/tests/jest-lite.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ export default class TestRunner {
143143
this.tests = Object.keys(modules)
144144
.filter(TestRunner.isTest)
145145
.map(p => modules[p]);
146+
147+
return this.tests;
146148
}
147149

148150
/* istanbul ignore next */

0 commit comments

Comments
 (0)