Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add unit tests #214

Merged
merged 2 commits into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .babelrc

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: CI
on:
on:
push:
pull_request:
branches:
Expand All @@ -23,5 +23,5 @@ jobs:
run: npm ci
- name: Build binaries
run: npm run build
- name: Run e2e tests
run: npm run test
- name: Run tests
run: npm run test
41 changes: 3 additions & 38 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,15 @@
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Babel compiled directory
lib
Expand All @@ -54,23 +30,12 @@ typings/
# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# vuepress build output
docs/.vuepress/dist

# Serverless directories
.serverless
# Test Artifacts
dest-path
test-dir
8 changes: 4 additions & 4 deletions __e2e__/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ describe('Default behavior', () => {
it('warns the user if an unknown option is passed in', () => {
const { exitCode, stderr } = run(['--invalid'], { reject: false });
expect(exitCode).toBe(1);
expect(stderr).toContain(`error: unknown option`);
expect(stderr).toBe(`error: unknown option '--invalid'`);
});

it('warns the user if an unknown command is passed in', () => {
const { exitCode, stderr } = run(['create'], { reject: false });
expect(exitCode).toBe(1);
expect(stderr).toContain('Unknown command create');
expect(stderr.trim()).toBe('Unknown command create.');
});

it('suggests the closest match for an unknown command', () => {
const { exitCode, stdout } = run(['ini'], { reject: false });
const { exitCode, stderr } = run(['ini'], { reject: false });
expect(exitCode).toBe(1);
expect(stdout).toContain('Did you mean init?');
expect(stderr).toContain('Did you mean init?');
});
});
7 changes: 2 additions & 5 deletions __e2e__/commands/generate.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {
runPromptWithAnswers,
rmTempDir,
fetchProjectConfig,
} from '../../jest/helpers';
import { runPromptWithAnswers, rmTempDir } from '../../jest/helpers';
import { fetchProjectConfig } from '../../src/utils/helpers';

import { DOWN, ENTER } from 'cli-prompts-test';
import fs from 'fs';
Expand Down
8 changes: 2 additions & 6 deletions __e2e__/commands/init.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
'use strict';

import {
run,
rmTempDir,
runPromptWithAnswers,
fetchProjectConfig,
} from '../../jest/helpers';
import { run, rmTempDir, runPromptWithAnswers } from '../../jest/helpers';
import { fetchProjectConfig } from '../../src/utils/helpers';

import { DOWN, ENTER } from 'cli-prompts-test';
import fs from 'fs';
Expand Down
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
presets: [['@babel/preset-env']],
ignore: ['src/templates'],
};
30 changes: 14 additions & 16 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
const gulp = require('gulp');
const del = require('del');
const execa = require('execa');
const gulp = require('gulp');

gulp.task('clean', () => del('lib/**', { force: true }));
gulp.task('clean', () => del('lib/**'));

gulp.task('copy server templates', () =>
gulp
.src('./src/templates/server/**/*')
.pipe(gulp.dest('./lib/templates/server')),
);
gulp.task('build', async () => {
const {
exitCode,
} = await execa.command(
'babel src --out-dir lib --copy-files --include-dotfiles',
{ stdio: 'inherit' },
);
console.log(`The process exited with code ${exitCode}`);
});

gulp.task('copy starter templates', () =>
gulp
.src('./src/templates/starter-templates/**/*')
.pipe(gulp.dest('./lib/templates/starter-templates')),
);
gulp.task('cleanup', () => del('lib/**/__tests__'));

gulp.task(
'copy',
gulp.series('copy server templates', 'copy starter templates'),
);
gulp.task('default', gulp.series('clean', 'build', 'cleanup'));
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@ module.exports = {
'<rootDir>/lib/*',
],
},
{
...common,
displayName: 'unit',
testMatch: ['<rootDir>/**/__tests__/*.test.js'],
modulePathIgnorePatterns: [
'<rootDir>/my-app/*/package.json',
'<rootDir>/lib/*',
],
},
],
};
4 changes: 0 additions & 4 deletions jest/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,3 @@ export const rmTempDir = (tempDirPath) => {
fs.rmdirSync(tempDirPath, { recursive: true });
}
};

// .mevnrc
export const fetchProjectConfig = (genPath) =>
JSON.parse(fs.readFileSync(path.join(genPath, '.mevnrc'), 'utf8'));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"mevn": "./bin/mevn.js"
},
"scripts": {
"build": "gulp clean && babel src --out-dir lib --copy-files --include-dotfiles && gulp copy",
"build": "gulp",
"build:watch": "babel --watch src --out-dir lib --copy-files --include-dotfiles",
"lint": "eslint ./src __e2e__ jest",
"lint:fix": "eslint --fix ./src __e2e__ jest",
Expand Down
38 changes: 19 additions & 19 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@

// Require Modules.
import '@babel/polyfill';
import program from 'commander';
import chalk from 'chalk';
import leven from 'leven';
import envinfo from 'envinfo';
import leven from 'leven';
import program from 'commander';
import updateNotifier from 'update-notifier';

// Defining action handlers for respective commands.
import initializeProject from './commands/basic/init';
import generateFile from './commands/basic/generate';
import asyncRender from './commands/basic/codesplit';
import addDeps from './commands/basic/add';
import setupProject from './commands/serve/setup';
import dockerize from './commands/basic/docker';
import deployConfig from './commands/deploy/deploy';
import add from './commands/add';
import codesplit from './commands/codesplit';
import deploy from './commands/deploy/';
import dockerize from './commands/dockerize';
import generate from './commands/generate';
import init from './commands/init';
import serve from './commands/serve';

import * as logger from './utils/logger';
import pkg from '../package';

updateNotifier({ pkg }).notify();
Expand All @@ -29,7 +31,7 @@ const suggestCommands = (cmd) => {
(c) => leven(c, cmd) < c.length * 0.4,
);
if (suggestion) {
console.log(` ` + chalk.red(`Did you mean ${chalk.yellow(suggestion)}?`));
logger.error(` Did you mean ${chalk.yellow(suggestion)}?`);
}
};

Expand All @@ -39,30 +41,30 @@ program.version(pkg.version).usage('<command> [options]');
program
.command('init <appname>')
.description('Scaffolds a MEVN stack project in the current path')
.action(initializeProject);
.action(init);

program
.command('codesplit')
.description('Lazy load components as required')
.action(asyncRender);
.action(codesplit);

program
.command('generate')
.description(
'Generates client side component files and server side CRUD boilerplate template',
)
.action(generateFile);
.action(generate);

program
.command('add [deps...]')
.option('-d, --dev', 'install dev-dependencies')
.description('Install dependencies on the go')
.action(addDeps);
.action(add);

program
.command('serve')
.description('Serves client/server locally')
.action(setupProject);
.action(serve);

program
.command('dockerize')
Expand All @@ -72,7 +74,7 @@ program
program
.command('deploy')
.description('Deploys the webapp to a cloud solution of choice')
.action(deployConfig);
.action(deploy);

program
.command('info')
Expand All @@ -92,9 +94,7 @@ program
// Validation for unknown commands
program.on('command:*', ([cmd]) => {
program.outputHelp();
console.log();
console.error(` Unknown command ${chalk.yellow(cmd)}.`);
console.log();
logger.error(`\n Unknown command ${chalk.yellow(cmd)}.\n`);
suggestCommands(cmd);
process.exitCode = 1;
});
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 2 additions & 4 deletions src/commands/serve/setup.js → src/commands/serve/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import fs from 'fs';
import showBanner from 'node-banner';

import serveProject from './launch';
import {
checkIfConfigFileExists,
dirOfChoice,
fetchProjectConfig,
} from '../../utils/helpers';
import serveProject from './launch';

/**
* Prompts the user to choose between client/server side to be served locally
Expand All @@ -20,14 +20,12 @@ export default async () => {
await showBanner('MEVN CLI', 'Light speed setup for MEVN stack based apps.');
checkIfConfigFileExists();

let templateDir;
let templateDir = 'client';

const projectConfig = fetchProjectConfig();

if (fs.existsSync('./server')) {
({ dir: templateDir } = await dirOfChoice());
} else {
templateDir = 'client';
}

// Proceed with further installation
Expand Down
Loading