Skip to content

Commit 35ec365

Browse files
committed
feat(remove): remove the .mocharc.json file when removing mocha from a project
1 parent e65d483 commit 35ec365

File tree

9 files changed

+91
-1
lines changed

9 files changed

+91
-1
lines changed

src/configuration/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export {default as remove} from './remover.js';
2+
export {default as test} from './tester.js';

src/configuration/remover-test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as td from 'testdouble';
2+
import any from '@travi/any';
3+
4+
suite('mocha config remover', () => {
5+
let remove, fs;
6+
7+
setup(async () => {
8+
fs = await td.replaceEsm('node:fs');
9+
10+
({default: remove} = (await import('./remover.js')));
11+
});
12+
13+
teardown(() => td.reset());
14+
15+
test('that the config file is removed', async () => {
16+
const projectRoot = any.simpleObject();
17+
18+
await remove({projectRoot});
19+
20+
td.verify(fs.promises.unlink(`${projectRoot}/.mocharc.json`));
21+
});
22+
});

src/configuration/remover.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {promises as fs} from 'fs';
2+
3+
export default async function ({projectRoot}) {
4+
await fs.unlink(`${projectRoot}/.mocharc.json`);
5+
}

src/configuration/tester-test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as td from 'testdouble';
2+
import {assert} from 'chai';
3+
import any from '@travi/any';
4+
5+
suite('configuration predicate', () => {
6+
let canaryExists, core;
7+
const projectRoot = any.string();
8+
9+
setup(async () => {
10+
core = await td.replaceEsm('@form8ion/core');
11+
12+
({default: canaryExists} = (await import('./tester.js')));
13+
});
14+
15+
teardown(() => td.reset());
16+
17+
test('that `false` is returned if the canary test does not exist', async () => {
18+
td.when(core.fileExists(`${projectRoot}/.mocharc.json`)).thenResolve(false);
19+
20+
assert.isFalse(await canaryExists({projectRoot}));
21+
});
22+
23+
test('that `true` is returned if the canary test does not exist', async () => {
24+
td.when(core.fileExists(`${projectRoot}/.mocharc.json`)).thenResolve(true);
25+
26+
assert.isTrue(await canaryExists({projectRoot}));
27+
});
28+
});

src/configuration/tester.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {fileExists} from '@form8ion/core';
2+
3+
export default function ({projectRoot}) {
4+
return fileExists(`${projectRoot}/.mocharc.json`);
5+
}

src/remover-test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import * as td from 'testdouble';
44
import any from '@travi/any';
55

66
suite('mocha remover', () => {
7-
let remove, canaryExists, removeCanary;
7+
let remove, canaryExists, removeCanary, configExists, removeConfig;
88
const projectRoot = any.string();
99

1010
setup(async () => {
1111
({remove: removeCanary, test: canaryExists} = await td.replaceEsm('./canary/index.js'));
12+
({remove: removeConfig, test: configExists} = await td.replaceEsm('./configuration/index.js'));
1213

1314
({default: remove} = (await import('./remover.js')));
1415
});
@@ -17,10 +18,12 @@ suite('mocha remover', () => {
1718

1819
test('that mocha details are removed from the project', async () => {
1920
td.when(canaryExists({projectRoot})).thenResolve(true);
21+
td.when(configExists({projectRoot})).thenResolve(true);
2022

2123
const {dependencies} = await remove({projectRoot});
2224

2325
td.verify(removeCanary({projectRoot}));
26+
td.verify(removeConfig({projectRoot}));
2427
assert.deepEqual(dependencies.javascript.remove, ['mocha', 'chai', 'sinon']);
2528
});
2629

@@ -31,4 +34,12 @@ suite('mocha remover', () => {
3134

3235
td.verify(removeCanary({projectRoot}), {times: 0});
3336
});
37+
38+
test('that removing the config file is not attempted if it does not exist', async () => {
39+
td.when(configExists({projectRoot})).thenResolve(false);
40+
41+
await remove({projectRoot});
42+
43+
td.verify(removeConfig({projectRoot}), {times: 0});
44+
});
3445
});

src/remover.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import {test as canaryExists, remove as removeCanary} from './canary/index.js';
2+
import {test as configExists, remove as removeConfig} from './configuration/index.js';
23

34
export default async function ({projectRoot}) {
45
if (await canaryExists({projectRoot})) {
56
await removeCanary({projectRoot});
67
}
78

9+
if (await configExists({projectRoot})) {
10+
await removeConfig({projectRoot});
11+
}
12+
813
return {dependencies: {javascript: {remove: ['mocha', 'chai', 'sinon']}}};
914
}

test/integration/features/remove.feature

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ Feature: Remove Mocha from a project
22

33
Scenario: Mocha exists in the project
44
Given a mocharc file exists
5+
And a setup file exists
56
When mocha is removed from the project
67
Then mocha dependencies are removed
8+
And configuration files are removed
79

810
Scenario: Mocha exists and the canary test still exists
911
Given a mocharc file exists

test/integration/features/step_definitions/configuration-steps.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,17 @@ Given('a mocharc file exists', async function () {
99
await fs.writeFile(`${this.projectRoot}/.mocharc.json`, JSON.stringify(any.simpleObject()));
1010
});
1111

12+
Given('a setup file exists', async function () {
13+
await fs.mkdir(`${this.projectRoot}/test`);
14+
await fs.writeFile(`${this.projectRoot}/test/mocha-setup.js`, any.string());
15+
});
16+
1217
Then('the testing framework is configured', async function () {
1318
assert.isTrue(await fileExists(`${this.projectRoot}/.mocharc.json`));
1419
assert.isTrue(await fileExists(`${this.projectRoot}/test/mocha-setup.js`));
1520
});
21+
22+
Then('configuration files are removed', async function () {
23+
assert.isFalse(await fileExists(`${this.projectRoot}/.mocharc.json`));
24+
// assert.isFalse(await fileExists(`${this.projectRoot}/test/mocha-setup.js`));
25+
});

0 commit comments

Comments
 (0)