Skip to content

Commit

Permalink
tests: add exit code (#212)
Browse files Browse the repository at this point in the history
* test: add exit code for every test cases

* feat: update package-lock.json

* test: refactor code

* test: fix test

* chore: remove extraneous return statements

Co-authored-by: jamesgeorge007 <jamesgeorge998001@gmail.com>
  • Loading branch information
TheSTL and jamesgeorge007 committed Dec 19, 2020
1 parent 59b9771 commit 74970ac
Show file tree
Hide file tree
Showing 12 changed files with 479 additions and 467 deletions.
9 changes: 6 additions & 3 deletions __e2e__/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import { run } from '../jest/helpers';

describe('Default behavior', () => {
it('warns the user if an unknown option is passed in', () => {
const { stderr } = run(['--invalid'], { reject: false });
const { exitCode, stderr } = run(['--invalid'], { reject: false });
expect(exitCode).toBe(1);
expect(stderr).toContain(`error: unknown option`);
});

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

it('suggests the closest match for an unknown command', () => {
const { stdout } = run(['ini']);
const { exitCode, stdout } = run(['ini'], { reject: false });
expect(exitCode).toBe(1);
expect(stdout).toContain('Did you mean init?');
});
});
22 changes: 16 additions & 6 deletions __e2e__/commands/add.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('mevn add', () => {
);

// Invoke the add command
await runPromptWithAnswers(
const { exitCode } = await runPromptWithAnswers(
['add'],
[
ENTER,
Expand All @@ -44,6 +44,8 @@ describe('mevn add', () => {
genPath,
);

expect(exitCode).toBe(0);

// nuxt.config.js
const nuxtConfig = require(path.join(clientPath, 'nuxt.config.js')).default;
expect(nuxtConfig.buildModules).toContain('@nuxtjs/pwa');
Expand Down Expand Up @@ -85,8 +87,12 @@ describe('mevn add', () => {
});

it('installs the respective dependency passed as an arg', async () => {
await runPromptWithAnswers(['add', 'v-tooltip'], [ENTER], genPath);

const { exitCode } = await runPromptWithAnswers(
['add', 'v-tooltip'],
[ENTER],
genPath,
);
expect(exitCode).toBe(0);
// package.json
const pkgJson = JSON.parse(
fs.readFileSync(path.join(clientPath, 'package.json')),
Expand All @@ -95,12 +101,14 @@ describe('mevn add', () => {
});

it('installs the respective devDependency passed as an arg for the server directory', async () => {
await runPromptWithAnswers(
const { exitCode } = await runPromptWithAnswers(
['add', 'husky', '--dev'],
[`${DOWN}${ENTER}`],
genPath,
);

expect(exitCode).toBe(0);

// package.json
const pkgJson = JSON.parse(
fs.readFileSync(path.join(serverPath, 'package.json')),
Expand All @@ -109,11 +117,12 @@ describe('mevn add', () => {
});

it('shows a warning if no args were passed in for server directory', async () => {
const { stdout } = await runPromptWithAnswers(
const { exitCode, stdout } = await runPromptWithAnswers(
['add'],
[`${DOWN}${ENTER}`], // opts for server directory
genPath,
);
expect(exitCode).toBe(1);
expect(stdout).toContain('Please specify the dependencies to install');

// Delete generated directory
Expand All @@ -131,11 +140,12 @@ describe('mevn add', () => {
);

// Invoke the add command
const { stdout } = await runPromptWithAnswers(
const { exitCode, stdout } = await runPromptWithAnswers(
['add'],
[ENTER], // opts for client directory
genPath,
);
expect(exitCode).toBe(1);
expect(stdout).toContain('Please specify the dependencies to install');
});
});
14 changes: 12 additions & 2 deletions __e2e__/commands/codesplit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ describe('mevn codesplit', () => {
);

// Invoke codesplit command
await runPromptWithAnswers(['codesplit'], [`${DOWN}${ENTER}`], genPath);
const { exitCode } = await runPromptWithAnswers(
['codesplit'],
[`${DOWN}${ENTER}`],
genPath,
);
expect(exitCode).toBe(0);

// router.js
const routerConfig = fs
Expand Down Expand Up @@ -71,7 +76,12 @@ describe('mevn codesplit', () => {
);

// Invoke codesplit command
const { stdout } = run(['codesplit'], { cwd: genPath });
const { exitCode, stdout } = run(['codesplit'], {
cwd: genPath,
reject: false,
});

expect(exitCode).toBe(1);
expect(stdout).toContain(`You're having the Nuxt.js boilerplate template`);
});
});
12 changes: 9 additions & 3 deletions __e2e__/commands/generate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('mevn generate', () => {
expect(fetchProjectConfig(genPath).isConfigured.client).toBe(false);

// Invoke generate command
await runPromptWithAnswers(
const { exitCode } = await runPromptWithAnswers(
['generate'],
[
ENTER, // Generate new component
Expand All @@ -51,6 +51,8 @@ describe('mevn generate', () => {
genPath,
);

expect(exitCode).toBe(0);

// Invoking the generate command updates the key
expect(fetchProjectConfig(genPath).isConfigured.client).toBe(true);

Expand All @@ -61,7 +63,7 @@ describe('mevn generate', () => {
});

it('generates a Page component', async () => {
await runPromptWithAnswers(
const { exitCode } = await runPromptWithAnswers(
['generate'],
[
ENTER, // Generate new component
Expand All @@ -71,6 +73,8 @@ describe('mevn generate', () => {
genPath,
);

expect(exitCode).toBe(0);

// Check whether Dashboard.vue is created within the respective path
expect(
fs.existsSync(path.join(pageComponentPath, 'Dashboard.vue')),
Expand All @@ -88,7 +92,7 @@ describe('mevn generate', () => {
});

it('generates CRUD Boilerplate within the server directory', async () => {
await runPromptWithAnswers(
const { exitCode } = await runPromptWithAnswers(
['generate'],
[
`${DOWN}${ENTER}`, // Choose CRUD Boilerplate
Expand All @@ -97,6 +101,8 @@ describe('mevn generate', () => {
genPath,
);

expect(exitCode).toBe(0);

// .mevnrc
expect(fetchProjectConfig(genPath).isConfigured.server).toBe(true);

Expand Down
3 changes: 2 additions & 1 deletion __e2e__/commands/info.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import envinfo from 'envinfo';

describe('mevn info', () => {
it('logs local environment information', async () => {
const { stdout } = run(['info']);
const { exitCode, stdout } = run(['info']);
const data = await envinfo.run({
System: ['OS', 'CPU'],
Binaries: ['Node', 'Yarn', 'npm'],
Browsers: ['Chrome', 'Edge', 'Firefox', 'Safari'],
npmGlobalPackages: ['mevn-cli'],
});
expect(exitCode).toBe(0);
expect(stdout).toContain(data);
});
});
29 changes: 22 additions & 7 deletions __e2e__/commands/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ describe('mevn init', () => {
afterAll(() => rmTempDir(tempDirPath));

it('shows an appropriate warning if multiple arguments were provided with init', () => {
const { stdout } = run(['init', 'my-app', 'stray-arg']);
const { exitCode, stdout } = run(['init', 'my-app', 'stray-arg'], {
reject: false,
});

expect(exitCode).toBe(1);
expect(stdout).toContain(
'Error: Kindly provide only one argument as the directory name!!',
);
});

it('creates a new MEVN stack webapp based on the Nuxt.js starter template', async () => {
await runPromptWithAnswers(
const { exitCode } = await runPromptWithAnswers(
['init', 'my-app'],
[
`${DOWN}${DOWN}${DOWN}${ENTER}`, // Choose Nuxt.js as the starter template
Expand All @@ -45,6 +49,8 @@ describe('mevn init', () => {
tempDirPath,
);

expect(exitCode).toBe(0);

// nuxt.config.js
const nuxtConfig = require(path.join(clientPath, 'nuxt.config.js')).default;

Expand All @@ -71,23 +77,29 @@ describe('mevn init', () => {
});

it('shows an appropriate warning if the specified directory already exists in path', () => {
const { stdout } = run(['init', 'my-app'], { cwd: tempDirPath });
const { exitCode, stdout } = run(['init', 'my-app'], {
cwd: tempDirPath,
reject: false,
});

expect(exitCode).toBe(1);
expect(stdout).toContain('Error: Directory my-app already exists in path!');
});

it('shows an appropriate warning if creating an application within a non-empty path', () => {
const { stdout } = run(['init', '.'], {
const { exitCode, stdout } = run(['init', '.'], {
cwd: genPath,
reject: false,
});
expect(exitCode).toBe(1);
expect(stdout).toContain(`It seems the current directory isn't empty.`);

// Delete the generated directory
rmTempDir(genPath);
});

it('creates a new MEVN stack webapp based on the GraphQL starter template', async () => {
await runPromptWithAnswers(
const { exitCode } = await runPromptWithAnswers(
['init', 'my-app'],
[
`${DOWN}${DOWN}${ENTER}`, // Choose GraphQL as the starter template
Expand All @@ -96,6 +108,7 @@ describe('mevn init', () => {
tempDirPath,
);

expect(exitCode).toBe(0);
expect(fetchProjectConfig(genPath).template).toBe('GraphQL');
expect(fetchProjectConfig(genPath).isConfigured.client).toBe(false);
expect(fetchProjectConfig(genPath).isConfigured.server).toBe(false);
Expand All @@ -112,7 +125,7 @@ describe('mevn init', () => {
});

it('creates a new MEVN stack webapp based on the PWA starter template', async () => {
await runPromptWithAnswers(
const { exitCode } = await runPromptWithAnswers(
['init', 'my-app'],
[
`${DOWN}${ENTER}`, // Choose PWA as the starter template
Expand All @@ -121,6 +134,7 @@ describe('mevn init', () => {
tempDirPath,
);

expect(exitCode).toBe(0);
expect(fetchProjectConfig(genPath).template).toBe('PWA');
expect(fetchProjectConfig(genPath).isConfigured.client).toBe(false);
expect(fetchProjectConfig(genPath).isConfigured.server).toBe(false);
Expand Down Expand Up @@ -149,7 +163,7 @@ describe('mevn init', () => {
// Create my-app directory
fs.mkdirSync(genPath);

await runPromptWithAnswers(
const { exitCode } = await runPromptWithAnswers(
['init', '.'],
[
ENTER, // Choose Default as the starter template
Expand All @@ -158,6 +172,7 @@ describe('mevn init', () => {
genPath,
);

expect(exitCode).toBe(0);
expect(fetchProjectConfig(genPath).template).toBe('Default');
expect(fetchProjectConfig(genPath).isConfigured.client).toBe(false);
expect(fetchProjectConfig(genPath).isConfigured.server).toBe(false);
Expand Down

0 comments on commit 74970ac

Please sign in to comment.