Skip to content

Commit

Permalink
feat(remove): remove the .mocharc.json file when removing mocha fro…
Browse files Browse the repository at this point in the history
…m a project
  • Loading branch information
travi committed Jan 26, 2025
1 parent e65d483 commit 35ec365
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/configuration/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {default as remove} from './remover.js';
export {default as test} from './tester.js';
22 changes: 22 additions & 0 deletions src/configuration/remover-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as td from 'testdouble';
import any from '@travi/any';

suite('mocha config remover', () => {
let remove, fs;

setup(async () => {
fs = await td.replaceEsm('node:fs');

({default: remove} = (await import('./remover.js')));
});

teardown(() => td.reset());

test('that the config file is removed', async () => {
const projectRoot = any.simpleObject();

await remove({projectRoot});

td.verify(fs.promises.unlink(`${projectRoot}/.mocharc.json`));
});
});
5 changes: 5 additions & 0 deletions src/configuration/remover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {promises as fs} from 'fs';

export default async function ({projectRoot}) {
await fs.unlink(`${projectRoot}/.mocharc.json`);
}
28 changes: 28 additions & 0 deletions src/configuration/tester-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as td from 'testdouble';
import {assert} from 'chai';
import any from '@travi/any';

suite('configuration predicate', () => {
let canaryExists, core;
const projectRoot = any.string();

setup(async () => {
core = await td.replaceEsm('@form8ion/core');

({default: canaryExists} = (await import('./tester.js')));
});

teardown(() => td.reset());

test('that `false` is returned if the canary test does not exist', async () => {
td.when(core.fileExists(`${projectRoot}/.mocharc.json`)).thenResolve(false);

assert.isFalse(await canaryExists({projectRoot}));
});

test('that `true` is returned if the canary test does not exist', async () => {
td.when(core.fileExists(`${projectRoot}/.mocharc.json`)).thenResolve(true);

assert.isTrue(await canaryExists({projectRoot}));
});
});
5 changes: 5 additions & 0 deletions src/configuration/tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {fileExists} from '@form8ion/core';

export default function ({projectRoot}) {
return fileExists(`${projectRoot}/.mocharc.json`);
}
13 changes: 12 additions & 1 deletion src/remover-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import * as td from 'testdouble';
import any from '@travi/any';

suite('mocha remover', () => {
let remove, canaryExists, removeCanary;
let remove, canaryExists, removeCanary, configExists, removeConfig;
const projectRoot = any.string();

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

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

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

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

td.verify(removeCanary({projectRoot}));
td.verify(removeConfig({projectRoot}));
assert.deepEqual(dependencies.javascript.remove, ['mocha', 'chai', 'sinon']);
});

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

td.verify(removeCanary({projectRoot}), {times: 0});
});

test('that removing the config file is not attempted if it does not exist', async () => {
td.when(configExists({projectRoot})).thenResolve(false);

await remove({projectRoot});

td.verify(removeConfig({projectRoot}), {times: 0});
});
});
5 changes: 5 additions & 0 deletions src/remover.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import {test as canaryExists, remove as removeCanary} from './canary/index.js';
import {test as configExists, remove as removeConfig} from './configuration/index.js';

export default async function ({projectRoot}) {
if (await canaryExists({projectRoot})) {
await removeCanary({projectRoot});
}

if (await configExists({projectRoot})) {
await removeConfig({projectRoot});
}

return {dependencies: {javascript: {remove: ['mocha', 'chai', 'sinon']}}};
}
2 changes: 2 additions & 0 deletions test/integration/features/remove.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ Feature: Remove Mocha from a project

Scenario: Mocha exists in the project
Given a mocharc file exists
And a setup file exists
When mocha is removed from the project
Then mocha dependencies are removed
And configuration files are removed

Scenario: Mocha exists and the canary test still exists
Given a mocharc file exists
Expand Down
10 changes: 10 additions & 0 deletions test/integration/features/step_definitions/configuration-steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ Given('a mocharc file exists', async function () {
await fs.writeFile(`${this.projectRoot}/.mocharc.json`, JSON.stringify(any.simpleObject()));
});

Given('a setup file exists', async function () {
await fs.mkdir(`${this.projectRoot}/test`);
await fs.writeFile(`${this.projectRoot}/test/mocha-setup.js`, any.string());
});

Then('the testing framework is configured', async function () {
assert.isTrue(await fileExists(`${this.projectRoot}/.mocharc.json`));
assert.isTrue(await fileExists(`${this.projectRoot}/test/mocha-setup.js`));
});

Then('configuration files are removed', async function () {
assert.isFalse(await fileExists(`${this.projectRoot}/.mocharc.json`));
// assert.isFalse(await fileExists(`${this.projectRoot}/test/mocha-setup.js`));
});

0 comments on commit 35ec365

Please sign in to comment.