Skip to content

Commit

Permalink
test: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgeorge007 committed Dec 23, 2020
1 parent 34592cc commit b6eb5d5
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 81 deletions.
7 changes: 0 additions & 7 deletions .babelrc

This file was deleted.

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
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
72 changes: 72 additions & 0 deletions src/utils/__tests__/helpers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { ENTER } from 'cli-prompts-test';
import fs from 'fs';
import path from 'path';

import { copyDirSync, fetchProjectConfig, readFileContent } from '../helpers';
import { runPromptWithAnswers } from '../../../jest/helpers';

const srcPath = path.join(__dirname, 'test-dir');
const destPath = path.join(__dirname, 'dest-path');
const genPath = path.join(__dirname, 'my-app');

beforeAll(() => {
if (!fs.existsSync(srcPath)) {
fs.mkdirSync(srcPath);
fs.writeFileSync(path.join(srcPath, 'index.js'), '// Test');
}
if (!fs.existsSync(destPath)) {
fs.mkdirSync(destPath);
}
});

afterAll(() => {
// copyDirSync()
fs.rmdirSync(srcPath, { recursive: true });
fs.rmdirSync(destPath, { recursive: true });

// fetchProjectConfig()
fs.rmdirSync(genPath, { recursive: true });
});

test('copyDirSync()', () => {
// Copy source directory to destination path
copyDirSync(srcPath, destPath);

// Assertions
expect(fs.existsSync(path.join(destPath, 'test-dir'))).toBeTruthy();
expect(
fs.existsSync(path.join(destPath, 'test-dir', 'index.js')),
).toBeTruthy();
expect(
fs.readFileSync(path.join(destPath, 'test-dir', 'index.js'), 'utf8'),
).toBe('// Test');
});

test('fetchProjectConfig()', async () => {
await runPromptWithAnswers(
['init', 'my-app'],
[
ENTER, // Choose Default as the starter template
ENTER, // Requires server directory
],
__dirname,
);

const projectConfig = {
name: 'my-app',
template: 'Default',
isConfigured: {
client: false,
server: false,
},
};
expect(fetchProjectConfig(genPath)).toEqual(projectConfig);
});

test('not passing in a path to fetchProjectConfig()', () => {
expect(fetchProjectConfig()).toBe(undefined);
});

test('not passing in a file path to readFileContent()', () => {
expect(readFileContent()).toBe(undefined);
});
16 changes: 12 additions & 4 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ export const checkIfConfigFileExists = () => {
* @returns {Object}
*/

export const fetchProjectConfig = () =>
JSON.parse(fs.readFileSync('.mevnrc', 'utf8'));
export const fetchProjectConfig = (genPath) => {
const configFilePath = genPath ? path.join(genPath, '.mevnrc') : '.mevnrc';
if (!fs.existsSync(configFilePath)) {
return;
}
return JSON.parse(fs.readFileSync(configFilePath, 'utf8'));
};

/**
* Copy files
Expand Down Expand Up @@ -66,8 +71,8 @@ export const copyDirSync = (source, target) => {

// copy
if (fs.lstatSync(source).isDirectory()) {
fs.readdirSync(source).forEach(function (file) {
var curSource = path.join(source, file);
fs.readdirSync(source).forEach((file) => {
const curSource = path.join(source, file);
if (fs.lstatSync(curSource).isDirectory()) {
copyDirSync(curSource, targetFolder);
} else {
Expand Down Expand Up @@ -111,6 +116,9 @@ const stripChar = (fileContent) =>
*/

export const readFileContent = (filePath) => {
if (!filePath) {
return;
}
const fileContent = fs.readFileSync(filePath, 'utf8').split('\n');

// Check if the host OS is windows
Expand Down

0 comments on commit b6eb5d5

Please sign in to comment.