Skip to content

Commit e31a3da

Browse files
test: resolve failing tests
1 parent dceeae8 commit e31a3da

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

__e2e__/commands/init.test.js

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,50 @@
1-
'use strict';
2-
3-
import { run, rmTempDir, runPromptWithAnswers } from '../../jest/helpers';
1+
import { run, runPromptWithAnswers } from '../../jest/helpers';
42
import { fetchProjectConfig } from '../../src/utils/helpers';
53

64
import { DOWN, ENTER } from 'cli-prompts-test';
75
import fs from 'fs';
86
import path from 'path';
97

8+
// Utility for robust cleanup
9+
const rmDirIfExists = (dirPath) => {
10+
if (fs.existsSync(dirPath)) {
11+
fs.rmSync(dirPath, { recursive: true, force: true });
12+
}
13+
};
14+
1015
describe('mevn init', () => {
1116
const tempDirPath = path.join(__dirname, 'init-cmd');
1217
const genPath = path.join(tempDirPath, 'my-app');
13-
1418
const clientPath = path.join(genPath, 'client');
1519
const serverPath = path.join(genPath, 'server');
1620

17-
// Cleanup
21+
// Cleanup and setup before all tests
1822
beforeAll(() => {
19-
rmTempDir(tempDirPath);
23+
rmDirIfExists(tempDirPath);
2024
fs.mkdirSync(tempDirPath);
2125
});
2226

23-
afterAll(() => rmTempDir(tempDirPath));
27+
// Final cleanup after all tests
28+
afterAll(() => rmDirIfExists(tempDirPath));
29+
30+
// Extra per-test cleanup
31+
afterEach(() => {
32+
// Each test is responsible for its own output, but this catches anything left over.
33+
rmDirIfExists(genPath);
34+
});
2435

2536
it('shows an appropriate warning if multiple arguments were provided with init', () => {
2637
const { exitCode, stderr } = run(['init', 'my-app', 'stray-arg'], {
2738
reject: false,
2839
});
29-
3040
expect(exitCode).toBe(1);
3141
expect(stderr).toContain(
3242
'Error: Kindly provide only one argument as the directory name!!',
3343
);
3444
});
3545

3646
it('creates a new MEVN stack webapp based on the Nuxt.js starter template', async () => {
47+
rmDirIfExists(genPath);
3748
const { exitCode } = await runPromptWithAnswers(
3849
['init', 'my-app'],
3950
[
@@ -45,7 +56,6 @@ describe('mevn init', () => {
4556
],
4657
tempDirPath,
4758
);
48-
4959
expect(exitCode).toBe(0);
5060

5161
// nuxt.config.js
@@ -58,10 +68,7 @@ describe('mevn init', () => {
5868
// .mevnrc
5969
const projectConfigContent = {
6070
deployTarget: 'static',
61-
isConfigured: {
62-
client: false,
63-
server: false,
64-
},
71+
isConfigured: { client: false, server: false },
6572
modules: [],
6673
name: 'my-app',
6774
packageManager: 'npm',
@@ -75,6 +82,9 @@ describe('mevn init', () => {
7582
});
7683

7784
it('shows an appropriate warning if the specified directory already exists in path', () => {
85+
rmDirIfExists(genPath);
86+
fs.mkdirSync(genPath);
87+
7888
const { exitCode, stderr } = run(['init', 'my-app'], {
7989
cwd: tempDirPath,
8090
reject: false,
@@ -85,23 +95,27 @@ describe('mevn init', () => {
8595
});
8696

8797
it('shows an appropriate warning if creating an application within a non-empty path', () => {
98+
rmDirIfExists(genPath);
99+
fs.mkdirSync(genPath); // create target directory
100+
101+
// Add dummy file so it's not empty
102+
fs.writeFileSync(path.join(genPath, 'dummy.txt'), 'not empty');
103+
88104
const { exitCode, stderr } = run(['init', '.'], {
89105
cwd: genPath,
90106
reject: false,
91107
});
92108
expect(exitCode).toBe(1);
93109
expect(stderr).toContain(`It seems the current directory isn't empty.`);
94-
95-
// Delete the generated directory
96-
rmTempDir(genPath);
97110
});
98111

99112
it('creates a new MEVN stack webapp based on the GraphQL starter template', async () => {
113+
rmDirIfExists(genPath);
100114
const { exitCode } = await runPromptWithAnswers(
101115
['init', 'my-app'],
102116
[
103117
`${DOWN}${DOWN}${ENTER}`, // Choose GraphQL as the starter template
104-
ENTER, // Requires server directory,
118+
ENTER, // Requires server directory
105119
ENTER, // Choose npm as the package manager
106120
],
107121
tempDirPath,
@@ -118,12 +132,10 @@ describe('mevn init', () => {
118132

119133
// Check whether if the respective directory have been generated
120134
expect(fs.existsSync(path.join(serverPath, 'graphql'))).toBeTruthy();
121-
122-
// Delete the generated directory
123-
rmTempDir(genPath);
124135
});
125136

126137
it('creates a new MEVN stack webapp based on the PWA starter template', async () => {
138+
rmDirIfExists(genPath);
127139
const { exitCode } = await runPromptWithAnswers(
128140
['init', 'my-app'],
129141
[
@@ -154,13 +166,10 @@ describe('mevn init', () => {
154166
expect(
155167
fs.existsSync(path.join(clientPath, 'src', 'registerServiceWorker.js')),
156168
).toBeTruthy();
157-
158-
// Delete the generated directory
159-
rmTempDir(genPath);
160169
});
161170

162171
it('creates a new MEVN stack webapp based on the Default starter template in current directory', async () => {
163-
// Create my-app directory
172+
rmDirIfExists(genPath);
164173
fs.mkdirSync(genPath);
165174

166175
const { exitCode } = await runPromptWithAnswers(
@@ -183,6 +192,6 @@ describe('mevn init', () => {
183192
expect(fs.existsSync(path.join(clientPath, '.gitignore'))).toBeTruthy();
184193

185194
// Check whether if the respective directory have been generated
186-
expect(fs.existsSync(path.join(serverPath))).toBeTruthy();
195+
expect(fs.existsSync(serverPath)).toBeTruthy();
187196
});
188197
});

0 commit comments

Comments
 (0)