Skip to content

Commit

Permalink
Adding unit tests for jest native integration (#464)
Browse files Browse the repository at this point in the history
* Report testing errors in console

* Add todo

* Adding tests for jest-lite

* Test cleanup
  • Loading branch information
gautamarora authored and CompuIves committed Jan 30, 2018
1 parent edd8101 commit a6dc3d1
Show file tree
Hide file tree
Showing 6 changed files with 465 additions and 39 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"lerna run start --parallel --ignore app & lerna run start:dev_api --scope app --stream",
"test": "lerna run test",
"test:integrations": "lerna exec --scope app --stream -- yarn test:integrations",
"test:jest-lite": "lerna exec --scope app --stream -- yarn run test jest-lite --watch --coverage",
"lint": "lerna run lint",
"add-contributor": "all-contributors add",
"generate": "all-contributors generate",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,16 @@ function fetchFromMeta(dependency, version, fetchedPaths) {
return doFetch(`${depUrl}/?meta`)
.then(response => JSON.parse(response))
.then(meta => {
const filterAndFlatten = files => files.reduce((paths, file) => {
if (file.type === 'directory') {
return paths.concat(filterAndFlatten(file.files));
}
if (/\.d\.ts$/.test(file.path)) {
paths.push(file.path);
}
return paths;
}, []);
const filterAndFlatten = files =>
files.reduce((paths, file) => {
if (file.type === 'directory') {
return paths.concat(filterAndFlatten(file.files));
}
if (/\.d\.ts$/.test(file.path)) {
paths.push(file.path);
}
return paths;
}, []);

const dtsFiles = filterAndFlatten(meta.files);

Expand All @@ -187,11 +188,9 @@ function fetchFromMeta(dependency, version, fetchedPaths) {

dtsFiles.forEach(file => {
doFetch(`${depUrl}/${file}`)
.then(dtsFile => addLib(
`node_modules/${dependency}${file}`,
dtsFile,
fetchedPaths
))
.then(dtsFile =>
addLib(`node_modules/${dependency}${file}`, dtsFile, fetchedPaths)
)
.catch(() => {});
});
});
Expand Down Expand Up @@ -238,16 +237,18 @@ function fetchAndAddDependencies(dependencies) {
fetchedPaths
).catch(() => {
// not available in package.json, try checking meta for inline .d.ts files
fetchFromMeta(dep, getVersion(dependencies[dep]), fetchedPaths).catch(() => {
// Not available in package.json or inline from meta, try checking in @types/
fetchFromDefinitelyTyped(
dep,
dependencies[dep],
fetchedPaths
).catch(() => {
// Do nothing if it still can't be fetched
});
});
fetchFromMeta(dep, getVersion(dependencies[dep]), fetchedPaths).catch(
() => {
// Not available in package.json or inline from meta, try checking in @types/
fetchFromDefinitelyTyped(
dep,
dependencies[dep],
fetchedPaths
).catch(() => {
// Do nothing if it still can't be fetched
});
}
);
});

loadedTypings.push(dep);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Console extends React.Component<Props, State> {
this.addMessage('warn', [undefined], 'return');
}
} else {
this.addMessage('error', [aggregatedResults]);
this.addMessage('log', [error]);
}
break;
}
Expand Down
7 changes: 5 additions & 2 deletions packages/app/src/sandbox/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,11 @@ async function compile({
result: transformJSON(aggregatedResults),
});
// End - Testing
} catch (e) {
// Fail silently
} catch (error) {
dispatch({
type: 'test-result',
error: manager.testRunner.reportError(error),
});
}

debug(`Total time: ${Date.now() - startTime}ms`);
Expand Down
22 changes: 10 additions & 12 deletions packages/app/src/sandbox/eval/tests/jest-lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ export default class TestRunner {
});
}

/* istanbul ignore next */
async transpileTests() {
for (let t of this.tests) {
await this.manager.transpileModules(t, true);
}
}

/* istanbul ignore next */
async runTests() {
await this.transpileTests();
this.tests.forEach(t => {
Expand Down Expand Up @@ -157,22 +159,14 @@ export default class TestRunner {
: '👻';
summaryMessage = `Test Summary: ${summaryEmoji}\n\n`;
summaryMessage += 'Test Suites: ';
if (aggregatedResults.failedTestSuites !== null) {
summaryMessage += `${aggregatedResults.failedTestSuites} failed, `;
}
if (aggregatedResults.passedTestSuites !== null) {
summaryMessage += `${aggregatedResults.passedTestSuites} passed, `;
}
summaryMessage += `${aggregatedResults.failedTestSuites} failed, `;
summaryMessage += `${aggregatedResults.passedTestSuites} passed, `;
summaryMessage += `${aggregatedResults.totalTestSuites} total`;
summaryMessage += '\n';

summaryMessage += 'Tests: ';
if (aggregatedResults.failedTests !== null) {
summaryMessage += `${aggregatedResults.failedTests} failed, `;
}
if (aggregatedResults.passedTests !== null) {
summaryMessage += `${aggregatedResults.passedTests} passed, `;
}
summaryMessage += `${aggregatedResults.failedTests} failed, `;
summaryMessage += `${aggregatedResults.passedTests} passed, `;
summaryMessage += `${aggregatedResults.totalTests} total`;
summaryMessage += '\n';

Expand All @@ -186,6 +180,10 @@ export default class TestRunner {
return aggregatedResults;
}

reportError({ message = 'something went wrong' }) {
return `Test Summary: 😢\nError: ${message}`;
}

resetResults() {
this.aggregatedResults = this._makeEmptyAggregatedResults();
}
Expand Down
Loading

0 comments on commit a6dc3d1

Please sign in to comment.