Files
30-seconds-of-code/scripts/tdd.js
Christian C. Salvadó 83a6a6ea32 [ENHANCEMENT] Properly configure eslint to work with jest (#988)
* chore: make aware eslint that we use jest

By setting up the jest environment, we no longer need to declare the
'test' global in the configuration.

* chore: don't need to import expect, it's a jest environment global

* chore: don't need to import expect when creating undefined test
2019-06-18 08:34:45 +03:00

51 lines
1.8 KiB
JavaScript

/*
This is the tdd script that creates & updates your TDD environment .
Run using `npm run tdd`.
*/
// Load modules
const fs = require('fs-extra'),
path = require('path');
const childProcess = require('child_process');
const { green, yellow, red } = require('kleur');
const util = require('./util');
// Declare paths
const SNIPPETS_PATH = './snippets';
const SNIPPETS_ARCHIVE_PATH = './snippets_archive';
const TEST_PATH = './test';
console.time('Tester');
try {
// Read snippets, archive and tests, find which tests are not defined
const snippets = fs.readdirSync(SNIPPETS_PATH).map(v => v.replace('.md', ''));
const archivedSnippets = fs.readdirSync(SNIPPETS_ARCHIVE_PATH).filter(v => v !== 'README.md').map(v => v.replace('.md', ''));
const definedTests = fs.readdirSync(TEST_PATH).map(v => v.replace('.test.js', '')).filter(v => v !== '_30s.js' && v !== 'testlog');
const undefinedTests = [...snippets, ...archivedSnippets].filter(v => !definedTests.includes(v));
const orphanedTests = [...definedTests.filter(v => ![...snippets, ...archivedSnippets].includes(v))];
orphanedTests.forEach(snippet => {
console.log(`${yellow('WARNING!')} Orphaned test: ${snippet}`);
});
// Create files for undefined tests
undefinedTests.forEach(snippet => {
const exportTest = [
`const {${snippet}} = require('./_30s.js');`,
`\ntest('${snippet} is a Function', () => {`,
` expect(${snippet}).toBeInstanceOf(Function);`,
`});\n`
].join('\n');
fs.writeFileSync(path.join(TEST_PATH, `${snippet}.test.js`), exportTest);
});
// Run tests
if (util.isTravisCI()) {
process.exit(0);
}
else {
childProcess.execSync('npm test');
}
console.log(`${green('SUCCESS!')} All tests ran successfully!`);
} catch (err) {
console.log(`${red('ERROR!')} During test runs: ${err}`);
process.exit(1);
}
console.timeEnd('Tester');